use BetterNodeFinder instead of Printer magic

This commit is contained in:
TomasVotruba 2017-10-26 21:20:05 +02:00
parent fbe86bf9b2
commit af68eaf925
3 changed files with 21 additions and 26 deletions

View File

@ -50,4 +50,13 @@ final class BetterNodeFinder
{
return $this->nodeFinder->findFirstInstanceOf($nodes, $type);
}
/**
* @param Node|Node[] $nodes
* @return Node[]
*/
public function find($nodes, callable $filter): array
{
return $this->nodeFinder->find($nodes, $filter);
}
}

View File

@ -5,21 +5,10 @@ namespace Rector\NodeAnalyzer\Contrib\Symfony;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\PrettyPrinter\Standard;
use Rector\Node\Attribute;
final class ControllerMethodAnalyzer
{
/**
* @var Standard
*/
private $standardPrinter;
public function __construct(Standard $standardPrinter)
{
$this->standardPrinter = $standardPrinter;
}
/**
* Detect if is <some>Action() in Controller
*/
@ -38,12 +27,4 @@ final class ControllerMethodAnalyzer
return Strings::endsWith($node->name->toString(), 'Action');
}
public function doesNodeContain(ClassMethod $classMethodNode, string $part): bool
{
// @todo try NodeFinder
$methodInString = $this->standardPrinter->prettyPrint([$classMethodNode]);
return Strings::contains($methodInString, $part);
}
}

View File

@ -9,6 +9,7 @@ use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\Contrib\Symfony\ControllerMethodAnalyzer;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTraverserQueue\BetterNodeFinder;
use Rector\Rector\AbstractRector;
/**
@ -40,14 +41,21 @@ final class GetRequestRector extends AbstractRector
*/
private $methodCallAnalyzer;
/**
* @var BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(
ControllerMethodAnalyzer $controllerMethodAnalyzer,
MethodCallAnalyzer $methodCallAnalyzer,
NodeFactory $nodeFactory
NodeFactory $nodeFactory,
BetterNodeFinder $betterNodeFinder
) {
$this->controllerMethodAnalyzer = $controllerMethodAnalyzer;
$this->nodeFactory = $nodeFactory;
$this->methodCallAnalyzer = $methodCallAnalyzer;
$this->betterNodeFinder = $betterNodeFinder;
}
public function isCandidate(Node $node): bool
@ -85,12 +93,9 @@ final class GetRequestRector extends AbstractRector
return false;
}
/** @var ClassMethod $node */
if (! $this->controllerMethodAnalyzer->doesNodeContain($node, '$this->getRequest()')) {
return false;
}
return true;
return (bool) $this->betterNodeFinder->find($node, function (Node $node) {
return $this->methodCallAnalyzer->isMethod($node, 'getRequest');
});
}
private function isGetRequestInAction(Node $node): bool