mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-30 07:50:53 +00:00
28f5dbde13
c14f8d4517
[Php72][TypeDeclaration] Rollback tweak infinite loop handling on DeclareStrictTypesRector (#5700)
77 lines
2.4 KiB
PHP
77 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\NodeManipulator;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PhpParser\Node\Stmt\Expression;
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
|
|
use Rector\PhpParser\Comparing\NodeComparator;
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
final class StmtsManipulator
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
|
|
*/
|
|
private $simpleCallableNodeTraverser;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
|
*/
|
|
private $betterNodeFinder;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PhpParser\Comparing\NodeComparator
|
|
*/
|
|
private $nodeComparator;
|
|
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, BetterNodeFinder $betterNodeFinder, NodeComparator $nodeComparator)
|
|
{
|
|
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
$this->nodeComparator = $nodeComparator;
|
|
}
|
|
/**
|
|
* @param Stmt[] $stmts
|
|
*/
|
|
public function getUnwrappedLastStmt(array $stmts) : ?Node
|
|
{
|
|
\end($stmts);
|
|
$lastStmtKey = \key($stmts);
|
|
\reset($stmts);
|
|
$lastStmt = $stmts[$lastStmtKey];
|
|
if ($lastStmt instanceof Expression) {
|
|
return $lastStmt->expr;
|
|
}
|
|
return $lastStmt;
|
|
}
|
|
/**
|
|
* @param Stmt[] $stmts
|
|
* @return Stmt[]
|
|
*/
|
|
public function filterOutExistingStmts(ClassMethod $classMethod, array $stmts) : array
|
|
{
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use(&$stmts) {
|
|
foreach ($stmts as $key => $assign) {
|
|
if (!$this->nodeComparator->areNodesEqual($node, $assign)) {
|
|
continue;
|
|
}
|
|
unset($stmts[$key]);
|
|
}
|
|
return null;
|
|
});
|
|
return $stmts;
|
|
}
|
|
public function isVariableUsedInNextStmt(StmtsAwareInterface $stmtsAware, int $jumpToKey, string $variableName) : bool
|
|
{
|
|
if ($stmtsAware->stmts === null) {
|
|
return \false;
|
|
}
|
|
$stmts = \array_slice($stmtsAware->stmts, $jumpToKey, null, \true);
|
|
return (bool) $this->betterNodeFinder->findVariableOfName($stmts, $variableName);
|
|
}
|
|
}
|