From 9f0865e97d850a4bc5652206902205a8fd314f4e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 11 Jun 2025 10:19:11 +0200 Subject: [PATCH] [typed-collections] Add AssertNullOnCollectionToAssertEmptyRector --- config/sets/typed-collections.php | 2 + ...ullOnCollectionToAssertEmptyRectorTest.php | 28 ++++++ .../Fixture/assert_null_on_collection.php.inc | 37 +++++++ .../skip_assert_null_on_anything_else.php.inc | 16 +++ .../config/configured_rule.php | 10 ++ ...ertNullOnCollectionToAssertEmptyRector.php | 98 +++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/AssertNullOnCollectionToAssertEmptyRectorTest.php create mode 100644 rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/assert_null_on_collection.php.inc create mode 100644 rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/skip_assert_null_on_anything_else.php.inc create mode 100644 rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/config/configured_rule.php create mode 100644 rules/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector.php diff --git a/config/sets/typed-collections.php b/config/sets/typed-collections.php index 0f1b6775..f2082896 100644 --- a/config/sets/typed-collections.php +++ b/config/sets/typed-collections.php @@ -26,6 +26,7 @@ use Rector\Doctrine\TypedCollections\Rector\FuncCall\InArrayOnCollectionToContainsCallRector; use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector; use Rector\Doctrine\TypedCollections\Rector\If_\RemoveIsArrayOnCollectionRector; +use Rector\Doctrine\TypedCollections\Rector\MethodCall\AssertNullOnCollectionToAssertEmptyRector; use Rector\Doctrine\TypedCollections\Rector\MethodCall\SetArrayToNewCollectionRector; use Rector\Doctrine\TypedCollections\Rector\New_\RemoveNewArrayCollectionWrapRector; use Rector\Doctrine\TypedCollections\Rector\NullsafeMethodCall\RemoveNullsafeOnCollectionRector; @@ -80,5 +81,6 @@ // cleanup RemoveNullsafeOnCollectionRector::class, + AssertNullOnCollectionToAssertEmptyRector::class, ]); }; diff --git a/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/AssertNullOnCollectionToAssertEmptyRectorTest.php b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/AssertNullOnCollectionToAssertEmptyRectorTest.php new file mode 100644 index 00000000..752f9df2 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/AssertNullOnCollectionToAssertEmptyRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/assert_null_on_collection.php.inc b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/assert_null_on_collection.php.inc new file mode 100644 index 00000000..739e69c2 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/assert_null_on_collection.php.inc @@ -0,0 +1,37 @@ +assertNull($this->items); + } +} + +?> +----- +assertEmpty($this->items); + } +} + +?> diff --git a/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/skip_assert_null_on_anything_else.php.inc b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/skip_assert_null_on_anything_else.php.inc new file mode 100644 index 00000000..319bd36e --- /dev/null +++ b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/Fixture/skip_assert_null_on_anything_else.php.inc @@ -0,0 +1,16 @@ +assertNull($this->items); + } +} diff --git a/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/config/configured_rule.php b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/config/configured_rule.php new file mode 100644 index 00000000..981053d9 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(AssertNullOnCollectionToAssertEmptyRector::class); +}; diff --git a/rules/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector.php b/rules/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector.php new file mode 100644 index 00000000..fd4074b7 --- /dev/null +++ b/rules/TypedCollections/Rector/MethodCall/AssertNullOnCollectionToAssertEmptyRector.php @@ -0,0 +1,98 @@ +assertNull() on Collection object to $this->assertEmpty() in tests', + [ + new CodeSample( + <<<'CODE_SAMPLE' +use Doctrine\Common\Collections\Collection; + +final class SomeClass extends \PHPUnit\Framework\TestCase +{ + private Collection $items; + + public function test(): void + { + $this->assertNull($this->items); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +use Doctrine\Common\Collections\Collection; + +final class SomeClass extends \PHPUnit\Framework\TestCase +{ + private Collection $items; + + public function test(): void + { + $this->assertEmpty($this->items); + } +} +CODE_SAMPLE + )] + ); + } + + public function getNodeTypes(): array + { + return [MethodCall::class]; + + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): MethodCall|null + { + if ($node->isFirstClassCallable()) { + return null; + } + + if (! $this->isName($node->name, 'assertNull')) { + return null; + } + + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { + return null; + } + + $firstArg = $node->getArgs()[0]; + + if (! $this->collectionTypeDetector->isCollectionType($firstArg->value)) { + return null; + } + + // rename + $node->name = new Identifier('assertEmpty'); + + return $node; + } +}