Skip to content

Commit a67e274

Browse files
Improve AssertInstanceOfComparisonRector
1 parent c7ac374 commit a67e274

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;
4+
5+
use stdClass;
6+
7+
final class GetClass extends \PHPUnit\Framework\TestCase
8+
{
9+
public function test()
10+
{
11+
$something = new stdClass();
12+
self::assertSame('stdClass', get_class($something));
13+
}
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;
21+
22+
use stdClass;
23+
24+
final class GetClass extends \PHPUnit\Framework\TestCase
25+
{
26+
public function test()
27+
{
28+
$something = new stdClass();
29+
self::assertInstanceOf('stdClass', $something);
30+
}
31+
}
32+
33+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;
4+
5+
final class SkipFirstParam extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test()
8+
{
9+
$this->assertSame(get_class($something), 'stdClass');
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;
18+
19+
final class SkipFirstParam extends \PHPUnit\Framework\TestCase
20+
{
21+
public function test()
22+
{
23+
$this->assertSame(get_class($something), 'stdClass');
24+
}
25+
}
26+
27+
?>

rules/CodeQuality/Rector/MethodCall/AssertInstanceOfComparisonRector.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\FuncCall;
910
use PhpParser\Node\Expr\Instanceof_;
1011
use PhpParser\Node\Expr\MethodCall;
1112
use PhpParser\Node\Expr\StaticCall;
1213
use PhpParser\Node\Expr\Variable;
14+
use PhpParser\Node\Identifier;
1315
use Rector\Exception\ShouldNotHappenException;
1416
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
1517
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
@@ -49,6 +51,10 @@ public function getRuleDefinition(): RuleDefinition
4951
'$this->assertFalse($foo instanceof Foo, "message");',
5052
'$this->assertNotInstanceOf("Foo", $foo, "message");',
5153
),
54+
new CodeSample(
55+
'$this->assertNotEquals(SomeInstance::class, get_class($value));',
56+
'$this->assertNotInstanceOf(SomeInstance::class, $value);'
57+
),
5258
],
5359
);
5460
}
@@ -66,12 +72,19 @@ public function getNodeTypes(): array
6672
*/
6773
public function refactor(Node $node): ?Node
6874
{
69-
$oldMethodNames = array_keys(self::RENAME_METHODS_MAP);
70-
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) {
75+
if ($node->isFirstClassCallable()) {
7176
return null;
7277
}
7378

74-
if ($node->isFirstClassCallable()) {
79+
if ($this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
80+
$node,
81+
['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals']
82+
)) {
83+
return $this->refactorGetClass($node);
84+
}
85+
86+
$oldMethodNames = array_keys(self::RENAME_METHODS_MAP);
87+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) {
7588
return null;
7689
}
7790

@@ -87,6 +100,37 @@ public function refactor(Node $node): ?Node
87100
return $node;
88101
}
89102

103+
/**
104+
* @param MethodCall|StaticCall $node
105+
*/
106+
private function refactorGetClass(Node $node): ?Node
107+
{
108+
// we need 2 args
109+
if (! isset($node->args[1])) {
110+
return null;
111+
}
112+
113+
$secondArgument = $node->getArgs()[1];
114+
$secondArgumentValue = $secondArgument->value;
115+
116+
if ($secondArgumentValue instanceof FuncCall && $this->isName($secondArgumentValue->name, 'get_class')) {
117+
$countArg = $secondArgumentValue->getArgs()[0];
118+
$assertArgs[1] = new Arg($countArg->value);
119+
120+
$node->args = $assertArgs;
121+
122+
if ($this->isNames($node->name, ['assertSame', 'assertEquals'])) {
123+
$node->name = new Identifier('assertInstanceOf');
124+
} elseif ($this->isNames($node->name, ['assertNotSame', 'assertNotEquals'])) {
125+
$node->name = new Identifier('assertNotInstanceOf');
126+
}
127+
128+
return $node;
129+
}
130+
131+
return null;
132+
}
133+
90134
private function changeArgumentsOrder(MethodCall|StaticCall $node): void
91135
{
92136
$oldArguments = $node->getArgs();

0 commit comments

Comments
 (0)