mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-25 20:32:35 +00:00
use BetterNodeFinder instead of Printer magic
This commit is contained in:
parent
fbe86bf9b2
commit
af68eaf925
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user