From a2fb5090f7b116957058bca48b2708227ff7280f Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 8 Feb 2025 17:18:17 +0000 Subject: [PATCH 1/4] Adds AssertCountWithZeroToAssertEmptyRector rule --- ...rtCountWithZeroToAssertEmptyRectorTest.php | 28 ++++++ .../Fixture/count_to_empty.php.inc | 39 ++++++++ .../skip_on_first_class_callable.php.inc | 16 ++++ .../Fixture/skip_on_non_test_class.php.inc | 16 ++++ .../Source/Collection.php | 11 +++ .../config/configured_rule.php | 10 +++ ...AssertCountWithZeroToAssertEmptyRector.php | 88 +++++++++++++++++++ 7 files changed, 208 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/AssertCountWithZeroToAssertEmptyRectorTest.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/count_to_empty.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_first_class_callable.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Source/Collection.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/config/configured_rule.php create mode 100644 rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/AssertCountWithZeroToAssertEmptyRectorTest.php b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/AssertCountWithZeroToAssertEmptyRectorTest.php new file mode 100644 index 00000000..9d7bb53c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/AssertCountWithZeroToAssertEmptyRectorTest.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/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/count_to_empty.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/count_to_empty.php.inc new file mode 100644 index 00000000..1885955f --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/count_to_empty.php.inc @@ -0,0 +1,39 @@ +assertCount(0, $collection); + $this->assertCount(0, $collection, 'message here!'); + $this->assertNotCount(0, $collection); + $this->assertNotCount(0, $collection, 'message here!'); + } +} + +?> +----- +assertEmpty($collection); + $this->assertEmpty($collection, 'message here!'); + $this->assertNotEmpty($collection); + $this->assertNotEmpty($collection, 'message here!'); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_first_class_callable.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_first_class_callable.php.inc new file mode 100644 index 00000000..ffc45b0e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_first_class_callable.php.inc @@ -0,0 +1,16 @@ +assertCount(...); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc new file mode 100644 index 00000000..e7aa6509 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc @@ -0,0 +1,16 @@ +assertCount(5, $collection); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Source/Collection.php b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Source/Collection.php new file mode 100644 index 00000000..498aef34 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Source/Collection.php @@ -0,0 +1,11 @@ +rule(AssertCountWithZeroToAssertEmptyRector::class); +}; diff --git a/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php new file mode 100644 index 00000000..16cb96f3 --- /dev/null +++ b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php @@ -0,0 +1,88 @@ +assertCount(0, ...) to $this->assertEmpty(...)', + [ + new CodeSample( + <<<'CODE_SAMPLE' + $this->assertCount(0, $countable); + $this->assertNotCount(0, $countable); + CODE_SAMPLE + , + <<<'CODE_SAMPLE' + $this->assertEmpty($countable); + $this->assertNotEmpty($countable); + CODE_SAMPLE + ), + ], + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [MethodCall::class, StaticCall::class]; + } + + /** + * @param MethodCall|StaticCall $node + */ + public function refactor(Node $node): MethodCall|StaticCall|null + { + if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertCount', 'assertNotCount'])) { + return null; + } + + if ($node->isFirstClassCallable()) { + return null; + } + + if (count($node->getArgs()) < 2) { + return null; + } + + $type = $this->getType($node->getArgs()[0]->value); + $value = ($type->getConstantScalarValues()[0] ?? null); + if ($value === 0) { + $args = $node->getArgs(); + if ($this->isName($node->name, 'assertNotCount')) { + $node->name = new Name('assertNotEmpty'); + } else { + $node->name = new Name('assertEmpty'); + } + + array_shift($args); + $node->args = $args; + return $node; + } + + return null; + } +} From ef6a707b031ac9864d7d9100edd521d89e45229c Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 8 Feb 2025 17:26:50 +0000 Subject: [PATCH 2/4] Added test --- .../Fixture/skip_on_non_zero_argument.php.inc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_zero_argument.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_zero_argument.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_zero_argument.php.inc new file mode 100644 index 00000000..bb9e643e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_zero_argument.php.inc @@ -0,0 +1,16 @@ +assertCount(5, $collection); + } +} + +?> From a79efadf412d079f2386305fff2b31aab670cdae Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 8 Feb 2025 17:29:15 +0000 Subject: [PATCH 3/4] correct test --- .../Fixture/skip_on_non_test_class.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc index e7aa6509..4181e428 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc @@ -9,7 +9,7 @@ final class SkipOnNonTestClass public function test() { $collection = new Collection(); - $this->assertCount(5, $collection); + $this->assertCount(0, $collection); } } From 8fdeba4e4abe34d04b40241e9cca85064b98490c Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 8 Feb 2025 18:31:40 +0000 Subject: [PATCH 4/4] Adds rule to code quality set --- config/sets/phpunit-code-quality.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/sets/phpunit-code-quality.php b/config/sets/phpunit-code-quality.php index bbbc7ad9..3eea0839 100644 --- a/config/sets/phpunit-code-quality.php +++ b/config/sets/phpunit-code-quality.php @@ -15,6 +15,7 @@ use Rector\PHPUnit\CodeQuality\Rector\Foreach_\SimplifyForeachInstanceOfRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertComparisonToSpecificMethodRector; +use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertCountWithZeroToAssertEmptyRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector; use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector; @@ -72,6 +73,7 @@ UseSpecificWillMethodRector::class, UseSpecificWithMethodRector::class, AssertEmptyNullableObjectToAssertInstanceofRector::class, + AssertCountWithZeroToAssertEmptyRector::class, /** * Improve direct testing of your code, without mock creep. Make it simple, clear and easy to maintain: