diff --git a/config/rector/sets/cakephp50.php b/config/rector/sets/cakephp50.php index b6e92e9..04f08aa 100644 --- a/config/rector/sets/cakephp50.php +++ b/config/rector/sets/cakephp50.php @@ -3,6 +3,7 @@ use Cake\Upgrade\Rector\Rector\MethodCall\OptionsArrayToNamedParametersRector; use Cake\Upgrade\Rector\Rector\MethodCall\RemoveMethodCallRector; +use Cake\Upgrade\Rector\Rector\MethodCall\TableRegistryLocatorRector; use Cake\Upgrade\Rector\ValueObject\OptionsArrayToNamedParameters; use Cake\Upgrade\Rector\ValueObject\RemoveMethodCall; use PHPStan\Type\ArrayType; @@ -113,4 +114,6 @@ new RemoveMethodCall('Cake\TestSuite\TestCase', 'useCommandRunner'), new RemoveMethodCall('Cake\TestSuite\TestCase', 'useHttpServer'), ]); + + $rectorConfig->rule(TableRegistryLocatorRector::class); }; diff --git a/src/Rector/Rector/MethodCall/TableRegistryLocatorRector.php b/src/Rector/Rector/MethodCall/TableRegistryLocatorRector.php new file mode 100644 index 0000000..999fd69 --- /dev/null +++ b/src/Rector/Rector/MethodCall/TableRegistryLocatorRector.php @@ -0,0 +1,59 @@ +get()`', [ + new ConfiguredCodeSample( + <<<'CODE_SAMPLE' +TableRegistry::get('something'); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +TableRegistry::getTableLocator()->get('something'); +CODE_SAMPLE + ) + ]); + } + + public function getNodeTypes(): array + { + return [StaticCall::class]; + } + + public function refactor(Node $node): ?Node + { + if(! $node instanceof StaticCall) { + return null; + } + + // Ensure it's a static call we're looking for: TableRegistry::get(...) + if (! $this->isStaticCallMatch($node, 'Cake\ORM\TableRegistry', 'get')) { + return null; + } + + // Create new static call TableRegistry::getTableLocator()->get(...) + return $this->nodeFactory->createMethodCall( + $this->nodeFactory->createStaticCall('Cake\ORM\TableRegistry', 'getTableLocator'), + 'get', + $node->args + ); + } + + private function isStaticCallMatch(StaticCall $staticCall, string $className, string $methodName): bool + { + // Check if the static call is `TableRegistry::get` + return $this->isName($staticCall->class, $className) && $this->isName($staticCall->name, $methodName); + } +} diff --git a/tests/test_apps/original/RectorCommand-testApply50/src/SomeComponent.php b/tests/test_apps/original/RectorCommand-testApply50/src/SomeComponent.php index 111e25c..1bc7055 100644 --- a/tests/test_apps/original/RectorCommand-testApply50/src/SomeComponent.php +++ b/tests/test_apps/original/RectorCommand-testApply50/src/SomeComponent.php @@ -6,4 +6,9 @@ class SomeComponent extends \Cake\Controller\Component protected $components; protected $_defaultConfig = []; + + public function tableRegistryTest(): void + { + \Cake\ORM\TableRegistry::get('MyTable'); + } } diff --git a/tests/test_apps/upgraded/RectorCommand-testApply50/src/SomeComponent.php b/tests/test_apps/upgraded/RectorCommand-testApply50/src/SomeComponent.php index dc06932..e81a668 100644 --- a/tests/test_apps/upgraded/RectorCommand-testApply50/src/SomeComponent.php +++ b/tests/test_apps/upgraded/RectorCommand-testApply50/src/SomeComponent.php @@ -6,4 +6,9 @@ class SomeComponent extends \Cake\Controller\Component protected array $components; protected array $_defaultConfig = []; + + public function tableRegistryTest(): void + { + \Cake\ORM\TableRegistry::getTableLocator()->get('MyTable'); + } }