Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateMockInTest extends TestCase
{
public function test($params)
{
$tmp = $this->createMock('SomeClass');
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector\Fixture;

use PHPUnit\Framework\TestCase;

final class CreateMockInTest extends TestCase
{
public function test($params)
{
}
}

?>
20 changes: 20 additions & 0 deletions rules/DeadCode/SideEffect/SideEffectNodeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\BetterNodeFinder;

final readonly class SideEffectNodeDetector
Expand All @@ -35,6 +37,8 @@
public function __construct(
private PureFunctionDetector $pureFunctionDetector,
private BetterNodeFinder $betterNodeFinder,
private NodeTypeResolver $nodeTypeResolver,
private NodeNameResolver $nodeNameResolver
) {
}

Expand Down Expand Up @@ -64,6 +68,10 @@ public function detectCallExpr(Node $node): bool
return false;
}

if (($node instanceof MethodCall || $node instanceof StaticCall) && $this->isTestMock($node)) {
return false;
}

$exprClass = $node::class;
if (in_array($exprClass, self::CALL_EXPR_SIDE_EFFECT_NODE_TYPES, true)) {
return true;
Expand All @@ -82,6 +90,18 @@ public function detectCallExpr(Node $node): bool
return false;
}

private function isTestMock(MethodCall|StaticCall $node): bool
{
$objectType = new ObjectType('PHPUnit\Framework\TestCase');
$nodeCaller = $node instanceof MethodCall ? $node->var : $node->class;

if (! $this->nodeTypeResolver->isObjectType($nodeCaller, $objectType)) {
return false;
}

return $this->nodeNameResolver->isName($node->name, 'createMock');
}

private function isPhpParser(New_ $new): bool
{
if (! $new->class instanceof FullyQualified) {
Expand Down