diff --git a/config/rector/cakephp53.php b/config/rector/cakephp53.php new file mode 100644 index 00000000..bdf4b523 --- /dev/null +++ b/config/rector/cakephp53.php @@ -0,0 +1,9 @@ +sets([CakePHPSetList::CAKEPHP_53]); +}; diff --git a/config/rector/sets/cakephp53.php b/config/rector/sets/cakephp53.php new file mode 100644 index 00000000..743c554f --- /dev/null +++ b/config/rector/sets/cakephp53.php @@ -0,0 +1,10 @@ +rule(EntityIsEmptyRector::class); +}; diff --git a/src/Rector/Rector/MethodCall/EntityIsEmptyRector.php b/src/Rector/Rector/MethodCall/EntityIsEmptyRector.php new file mode 100644 index 00000000..593c2a84 --- /dev/null +++ b/src/Rector/Rector/MethodCall/EntityIsEmptyRector.php @@ -0,0 +1,64 @@ +isEmpty() with !$entity->hasValue() for \Cake\ORM\Entity descendants', + [ + new CodeSample( + '$entity->isEmpty();', + '!$entity->hasValue();', + ), + ], + ); + } + + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + public function refactor(Node $node): ?Node + { + if (!$node instanceof MethodCall) { + return null; + } + + if (!$this->isName($node->name, 'isEmpty')) { + return null; + } + + $objectType = $this->getType($node->var); + if (!$objectType instanceof ObjectType) { + return null; + } + + if (!$objectType->isInstanceOf(Entity::class)->yes()) { + return null; + } + + $newMethodCall = new MethodCall( + $node->var, + new Identifier('hasValue'), + $node->args, + ); + + // Replace with !$entity->hasValue($args) + return new BooleanNot($newMethodCall); + } +} diff --git a/src/Rector/Set/CakePHPSetList.php b/src/Rector/Set/CakePHPSetList.php index ee3e77ee..65b1d975 100644 --- a/src/Rector/Set/CakePHPSetList.php +++ b/src/Rector/Set/CakePHPSetList.php @@ -80,6 +80,11 @@ final class CakePHPSetList */ public const CAKEPHP_52 = __DIR__ . '/../../../config/rector/sets/cakephp52.php'; + /** + * @var string + */ + public const CAKEPHP_53 = __DIR__ . '/../../../config/rector/sets/cakephp53.php'; + /** * @var string */ diff --git a/tests/TestCase/Command/RectorCommandTest.php b/tests/TestCase/Command/RectorCommandTest.php index 3da8850c..6925df2c 100644 --- a/tests/TestCase/Command/RectorCommandTest.php +++ b/tests/TestCase/Command/RectorCommandTest.php @@ -106,6 +106,13 @@ public function testApply52() $this->assertTestAppUpgraded(); } + public function testApply53() + { + $this->setupTestApp(__FUNCTION__); + $this->exec('upgrade rector --rules cakephp53 ' . TEST_APP); + $this->assertTestAppUpgraded(); + } + public function testApplyMigrations45() { $this->setupTestApp(__FUNCTION__); diff --git a/tests/test_apps/original/RectorCommand-testApply53/src/SomeTest.php b/tests/test_apps/original/RectorCommand-testApply53/src/SomeTest.php new file mode 100644 index 00000000..a6879e22 --- /dev/null +++ b/tests/test_apps/original/RectorCommand-testApply53/src/SomeTest.php @@ -0,0 +1,15 @@ +isEmpty('test'); + } +} diff --git a/tests/test_apps/upgraded/RectorCommand-testApply53/src/SomeTest.php b/tests/test_apps/upgraded/RectorCommand-testApply53/src/SomeTest.php new file mode 100644 index 00000000..00172c06 --- /dev/null +++ b/tests/test_apps/upgraded/RectorCommand-testApply53/src/SomeTest.php @@ -0,0 +1,15 @@ +hasValue('test'); + } +}