decopule FindAll methdo call manipulator

This commit is contained in:
TomasVotruba 2020-01-23 12:33:18 +01:00
parent a9261d9357
commit 3212b81d9a
5 changed files with 77 additions and 20 deletions

View File

@ -1,9 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\CakePHPToSymfony\Contract;
interface FindRepositoryMethodFactoryInterface
{
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Rector\CakePHPToSymfony\Contract\NodeManipulator;
use PhpParser\Node\Expr\MethodCall;
interface RepositoryFindMethodCallManipulatorInterface
{
public function getKeyName(): string;
public function processMethodCall(MethodCall $methodCall): MethodCall;
}

View File

@ -19,6 +19,7 @@ use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\CakePHPToSymfony\Contract\NodeManipulator\RepositoryFindMethodCallManipulatorInterface;
use Rector\Exception\NotImplementedException;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\Node\Resolver\NameResolver;
@ -42,14 +43,24 @@ final class DoctrineRepositoryClassMethodManipulator
*/
private $valueResolver;
/**
* @var RepositoryFindMethodCallManipulatorInterface[]
*/
private $repositoryFindMethodCallManipulators = [];
/**
* @param RepositoryFindMethodCallManipulatorInterface[] $repositoryFindMethodCallManipulators
*/
public function __construct(
CallableNodeTraverser $callableNodeTraverser,
NameResolver $nameResolver,
ValueResolver $valueResolver
ValueResolver $valueResolver,
array $repositoryFindMethodCallManipulators
) {
$this->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');

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Rector\CakePHPToSymfony\NodeManipulator\RepositoryFindClassMethodManipulator;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
abstract class AbstractRepositoryFindMethodCallManipulator
{
protected function refactorToRepositoryMethod(MethodCall $methodCall, string $methodName): void
{
$methodCall->var = new PropertyFetch(new Variable('this'), 'repository');
$methodCall->name = new Identifier($methodName);
}
}

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Rector\CakePHPToSymfony\NodeManipulator\RepositoryFindClassMethodManipulator;
use PhpParser\Node\Expr\MethodCall;
use Rector\CakePHPToSymfony\Contract\NodeManipulator\RepositoryFindMethodCallManipulatorInterface;
final class FindAllRepositoryFindMethodCallManipulator extends AbstractRepositoryFindMethodCallManipulator implements RepositoryFindMethodCallManipulatorInterface
{
public function getKeyName(): string
{
return 'all';
}
public function processMethodCall(MethodCall $methodCall): MethodCall
{
$this->refactorToRepositoryMethod($methodCall, 'findAll');
$methodCall->args = [];
return $methodCall;
}
}