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,33 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;

use stdClass;

final class GetClass extends \PHPUnit\Framework\TestCase
{
public function test()
{
$something = new stdClass();
self::assertSame('stdClass', get_class($something));
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;

use stdClass;

final class GetClass extends \PHPUnit\Framework\TestCase
{
public function test()
{
$something = new stdClass();
self::assertInstanceOf('stdClass', $something);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;

final class SkipFirstParam extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertSame(get_class($something), 'stdClass');
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertInstanceOfComparisonRector\Fixture;

final class SkipFirstParam extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertSame(get_class($something), 'stdClass');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Rector\Exception\ShouldNotHappenException;
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
Expand Down Expand Up @@ -49,6 +51,10 @@ public function getRuleDefinition(): RuleDefinition
'$this->assertFalse($foo instanceof Foo, "message");',
'$this->assertNotInstanceOf("Foo", $foo, "message");',
),
new CodeSample(
'$this->assertNotEquals(SomeInstance::class, get_class($value));',
'$this->assertNotInstanceOf(SomeInstance::class, $value);'
),
],
);
}
Expand All @@ -66,12 +72,19 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$oldMethodNames = array_keys(self::RENAME_METHODS_MAP);
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) {
if ($node->isFirstClassCallable()) {
return null;
}

if ($node->isFirstClassCallable()) {
if ($this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
$node,
['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals']
)) {
return $this->refactorGetClass($node);
}

$oldMethodNames = array_keys(self::RENAME_METHODS_MAP);
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, $oldMethodNames)) {
return null;
}

Expand All @@ -87,6 +100,34 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param MethodCall|StaticCall $node
*/
private function refactorGetClass(Node $node): ?Node
{
// we need 2 args
if (! isset($node->args[1])) {
return null;
}

$secondArgument = $node->getArgs()[1];
$secondArgumentValue = $secondArgument->value;

if ($secondArgumentValue instanceof FuncCall && $this->isName($secondArgumentValue->name, 'get_class')) {
$node->args[1] = $secondArgumentValue->getArgs()[0];

if ($this->isNames($node->name, ['assertSame', 'assertEquals'])) {
$node->name = new Identifier('assertInstanceOf');
} elseif ($this->isNames($node->name, ['assertNotSame', 'assertNotEquals'])) {
$node->name = new Identifier('assertNotInstanceOf');
}

return $node;
}

return null;
}

private function changeArgumentsOrder(MethodCall|StaticCall $node): void
{
$oldArguments = $node->getArgs();
Expand Down
Loading