From 3212b81d9a9a122d9bbe81f143d5bba259389f74 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Thu, 23 Jan 2020 12:33:18 +0100 Subject: [PATCH] decopule FindAll methdo call manipulator --- .../FindRepositoryMethodFactoryInterface.php | 9 ------ ...toryFindMethodCallManipulatorInterface.php | 14 +++++++++ ...ctrineRepositoryClassMethodManipulator.php | 30 ++++++++++++------- ...actRepositoryFindMethodCallManipulator.php | 19 ++++++++++++ ...AllRepositoryFindMethodCallManipulator.php | 25 ++++++++++++++++ 5 files changed, 77 insertions(+), 20 deletions(-) delete mode 100644 packages/CakePHPToSymfony/src/Contract/FindRepositoryMethodFactoryInterface.php create mode 100644 packages/CakePHPToSymfony/src/Contract/NodeManipulator/RepositoryFindMethodCallManipulatorInterface.php create mode 100644 packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/AbstractRepositoryFindMethodCallManipulator.php create mode 100644 packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/FindAllRepositoryFindMethodCallManipulator.php diff --git a/packages/CakePHPToSymfony/src/Contract/FindRepositoryMethodFactoryInterface.php b/packages/CakePHPToSymfony/src/Contract/FindRepositoryMethodFactoryInterface.php deleted file mode 100644 index 7034a975ff4..00000000000 --- a/packages/CakePHPToSymfony/src/Contract/FindRepositoryMethodFactoryInterface.php +++ /dev/null @@ -1,9 +0,0 @@ -callableNodeTraverser = $callableNodeTraverser; $this->nameResolver = $nameResolver; $this->valueResolver = $valueResolver; + $this->repositoryFindMethodCallManipulators = $repositoryFindMethodCallManipulators; } public function createFromCakePHPClassMethod(ClassMethod $classMethod, string $entityClass): ClassMethod @@ -75,9 +86,13 @@ final class DoctrineRepositoryClassMethodManipulator private function refactorClassMethodByKind(MethodCall $methodCall, string $entityClass): Node { $findKind = $this->valueResolver->getValue($methodCall->args[0]->value); - if ($findKind === 'all') { - $this->refactorFindAll($methodCall); - return $methodCall; + + foreach ($this->repositoryFindMethodCallManipulators as $repositoryFindMethodCallManipulator) { + if ($findKind !== $repositoryFindMethodCallManipulator->getKeyName()) { + continue; + } + + return $repositoryFindMethodCallManipulator->processMethodCall($methodCall); } if ($findKind === 'first') { @@ -176,13 +191,6 @@ final class DoctrineRepositoryClassMethodManipulator $methodCall->args = $this->createFindOneByArgs($entityClass, $methodCall); } - private function refactorFindAll(MethodCall $methodCall): void - { - $this->refactorToRepositoryMethod($methodCall, 'findAll'); - - $methodCall->args = []; - } - private function refactorToRepositoryMethod(MethodCall $methodCall, string $methodName): void { $methodCall->var = new PropertyFetch(new Variable('this'), 'repository'); diff --git a/packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/AbstractRepositoryFindMethodCallManipulator.php b/packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/AbstractRepositoryFindMethodCallManipulator.php new file mode 100644 index 00000000000..cf60ea22b4a --- /dev/null +++ b/packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/AbstractRepositoryFindMethodCallManipulator.php @@ -0,0 +1,19 @@ +var = new PropertyFetch(new Variable('this'), 'repository'); + $methodCall->name = new Identifier($methodName); + } +} diff --git a/packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/FindAllRepositoryFindMethodCallManipulator.php b/packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/FindAllRepositoryFindMethodCallManipulator.php new file mode 100644 index 00000000000..cdfaa409ed6 --- /dev/null +++ b/packages/CakePHPToSymfony/src/NodeManipulator/RepositoryFindClassMethodManipulator/FindAllRepositoryFindMethodCallManipulator.php @@ -0,0 +1,25 @@ +refactorToRepositoryMethod($methodCall, 'findAll'); + + $methodCall->args = []; + + return $methodCall; + } +}