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: 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..4181e428 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector/Fixture/skip_on_non_test_class.php.inc @@ -0,0 +1,16 @@ +assertCount(0, $collection); + } +} + +?> 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); + } +} + +?> 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; + } +}