diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php similarity index 86% rename from rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php rename to rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php index 0a94c042..ebf2bfaf 100644 --- a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php +++ b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/AssertClassToThisAssertRectorTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AssertClassToThisAssertRector; +namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AssertClassToThisAssertRector; use Iterator; use PHPUnit\Framework\Attributes\DataProvider; diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/basic.php.inc similarity index 65% rename from rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc rename to rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/basic.php.inc index 19f03b86..afb7aa4d 100644 --- a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/basic.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/basic.php.inc @@ -1,6 +1,6 @@ createMock(SomeEntity::class); + $entityMock->method('getId') + ->willReturn(1); + $entities = [$entityMock]; + + $parameters = [ + ['id' => 1, 'foo' => 'bar'], + ]; + + $helper = new SomeHelper($parameters); + $orderedEntities = $helper->orderByOriginalKey($entities); + + $this->assertSame([0], array_keys($orderedEntities)); + + foreach ($parameters as $key => $contact) { + Assert::assertEquals($orderedEntities[$key]->getId(), $entities[$key]->getId()); + } + } +} + +?> +----- +createMock(SomeEntity::class); + $entityMock->method('getId') + ->willReturn(1); + $entities = [$entityMock]; + + $parameters = [ + ['id' => 1, 'foo' => 'bar'], + ]; + + $helper = new SomeHelper($parameters); + $orderedEntities = $helper->orderByOriginalKey($entities); + + $this->assertSame([0], array_keys($orderedEntities)); + + foreach ($parameters as $key => $contact) { + $this->assertEquals($orderedEntities[$key]->getId(), $entities[$key]->getId()); + } + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc similarity index 58% rename from rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc rename to rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc index b9a43220..308500ef 100644 --- a/rules-tests/CodeQuality/Rector/Class_/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector/Fixture/skip_non_test_class.php.inc @@ -1,6 +1,6 @@ rule(AssertClassToThisAssertRector::class); diff --git a/rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php b/rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php similarity index 84% rename from rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php rename to rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php index 09d94fa6..ccde1581 100644 --- a/rules/CodeQuality/Rector/Class_/AssertClassToThisAssertRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/AssertClassToThisAssertRector.php @@ -2,14 +2,13 @@ declare(strict_types=1); -namespace Rector\PHPUnit\CodeQuality\Rector\Class_; +namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; use PhpParser\Node; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\NodeVisitor; use Rector\PHPUnit\Enum\PHPUnitClassName; @@ -19,7 +18,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\AssertClassToThisAssertRector\AssertClassToThisAssertRectorTest + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AssertClassToThisAssertRector\AssertClassToThisAssertRectorTest */ final class AssertClassToThisAssertRector extends AbstractRector { @@ -66,14 +65,18 @@ public function run() */ public function getNodeTypes(): array { - return [Class_::class]; + return [ClassMethod::class]; } /** - * @param Class_ $node + * @param ClassMethod $node */ public function refactor(Node $node): ?Node { + if ($node->isStatic()) { + return null; + } + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { return null; } @@ -81,8 +84,7 @@ public function refactor(Node $node): ?Node $hasChanged = false; $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): int|null|MethodCall { - $isInsideStaticFunctionLike = ($node instanceof ClassMethod && $node->isStatic()) || (($node instanceof Closure || $node instanceof ArrowFunction) && $node->static); - if ($isInsideStaticFunctionLike) { + if (($node instanceof Closure || $node instanceof ArrowFunction) && $node->static) { return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN; }