Updated Rector to commit 89bc4d80f5dd4c7e81a616c8b58a921a1470a47b

89bc4d80f5 Make use of StmtsAwareInterface (#3781)
This commit is contained in:
Tomas Votruba 2023-05-10 05:18:05 +00:00
parent bf29cbeb9e
commit 6da6d1d775
10 changed files with 159 additions and 171 deletions

View File

@ -7,7 +7,6 @@ use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Analyser\Scope;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @implements NodeNameResolverInterface<Function_>
*/

View File

@ -67,12 +67,16 @@ final class AttributeKey
public const PARENT_NODE = 'parent';
/**
* @internal of php-parser, do not change
* @deprecated Use StmtsAwareInterface instead
*
* @see https://github.com/nikic/PHP-Parser/pull/681/files
* @var string
*/
public const PREVIOUS_NODE = 'previous';
/**
* @internal of php-parser, do not change
* @deprecated Use StmtsAwareInterface instead
*
* @see https://github.com/nikic/PHP-Parser/pull/681/files
* @var string
*/

View File

@ -14,10 +14,9 @@ use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\BinaryOpManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php71\ValueObject\TwoNodeMatch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -31,15 +30,9 @@ final class ForeachToInArrayRector extends AbstractRector
* @var \Rector\Core\NodeManipulator\BinaryOpManipulator
*/
private $binaryOpManipulator;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
*/
private $commentsMerger;
public function __construct(BinaryOpManipulator $binaryOpManipulator, CommentsMerger $commentsMerger)
public function __construct(BinaryOpManipulator $binaryOpManipulator)
{
$this->binaryOpManipulator = $binaryOpManipulator;
$this->commentsMerger = $commentsMerger;
}
public function getRuleDefinition() : RuleDefinition
{
@ -62,55 +55,70 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [Foreach_::class];
return [StmtsAwareInterface::class];
}
/**
* @param Foreach_ $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node) : ?Node
{
if ($this->shouldSkipForeach($node)) {
if ($node->stmts === null) {
return null;
}
/** @var If_ $firstNodeInsideForeach */
$firstNodeInsideForeach = $node->stmts[0];
if ($this->shouldSkipIf($firstNodeInsideForeach)) {
return null;
foreach ($node->stmts as $key => $stmt) {
if (!$stmt instanceof Return_) {
continue;
}
$prevStmt = $node->stmts[$key - 1] ?? null;
if (!$prevStmt instanceof Foreach_) {
continue;
}
$return = $stmt;
$foreach = $prevStmt;
if ($this->shouldSkipForeach($foreach)) {
return null;
}
/** @var If_ $firstNodeInsideForeach */
$firstNodeInsideForeach = $foreach->stmts[0];
if ($this->shouldSkipIf($firstNodeInsideForeach)) {
return null;
}
/** @var Identical|Equal $ifCondition */
$ifCondition = $firstNodeInsideForeach->cond;
$twoNodeMatch = $this->matchNodes($ifCondition, $foreach->valueVar);
if (!$twoNodeMatch instanceof TwoNodeMatch) {
return null;
}
$comparedExpr = $twoNodeMatch->getSecondExpr();
if (!$this->isIfBodyABoolReturnNode($firstNodeInsideForeach)) {
return null;
}
$foreachReturn = $firstNodeInsideForeach->stmts[0];
if (!$foreachReturn instanceof Return_) {
return null;
}
if (!$return->expr instanceof Expr) {
return null;
}
if (!$this->valueResolver->isTrueOrFalse($return->expr)) {
return null;
}
if (!$foreachReturn->expr instanceof Expr) {
return null;
}
// cannot be "return true;" + "return true;"
if ($this->nodeComparator->areNodesEqual($return, $foreachReturn)) {
return null;
}
// 1. remove foreach
unset($node->stmts[$key - 1]);
// 2. make return of in_array()
$funcCall = $this->createInArrayFunction($comparedExpr, $ifCondition, $foreach);
$return = $this->createReturn($foreachReturn->expr, $funcCall);
$node->stmts[$key] = $return;
return $node;
}
/** @var Identical|Equal $ifCondition */
$ifCondition = $firstNodeInsideForeach->cond;
$foreachValueVar = $node->valueVar;
$twoNodeMatch = $this->matchNodes($ifCondition, $foreachValueVar);
if (!$twoNodeMatch instanceof TwoNodeMatch) {
return null;
}
$comparedExpr = $twoNodeMatch->getSecondExpr();
if (!$this->isIfBodyABoolReturnNode($firstNodeInsideForeach)) {
return null;
}
$funcCall = $this->createInArrayFunction($comparedExpr, $ifCondition, $node);
/** @var Return_ $returnToRemove */
$returnToRemove = $node->getAttribute(AttributeKey::NEXT_NODE);
/** @var Return_ $return */
$return = $firstNodeInsideForeach->stmts[0];
if (!$returnToRemove->expr instanceof Expr) {
return null;
}
if (!$this->valueResolver->isTrueOrFalse($returnToRemove->expr)) {
return null;
}
$returnedExpr = $return->expr;
if (!$returnedExpr instanceof Expr) {
return null;
}
// cannot be "return true;" + "return true;"
if ($this->nodeComparator->areNodesEqual($return, $returnToRemove)) {
return null;
}
$this->removeNode($returnToRemove);
$return = $this->createReturn($returnedExpr, $funcCall);
$this->commentsMerger->keepChildren($return, $node);
return $return;
return null;
}
private function shouldSkipForeach(Foreach_ $foreach) : bool
{
@ -123,20 +131,6 @@ CODE_SAMPLE
if (!$foreach->stmts[0] instanceof If_) {
return \true;
}
$nextNode = $foreach->getAttribute(AttributeKey::NEXT_NODE);
if (!$nextNode instanceof Node) {
return \true;
}
if (!$nextNode instanceof Return_) {
return \true;
}
$returnExpression = $nextNode->expr;
if (!$returnExpression instanceof Expr) {
return \true;
}
if (!$this->valueResolver->isTrueOrFalse($returnExpression)) {
return \true;
}
$foreachValueStaticType = $this->getType($foreach->expr);
return $foreachValueStaticType instanceof ObjectType;
}

View File

@ -9,14 +9,14 @@ use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\NodeManipulator\ForeachManipulator;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -27,98 +27,123 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class SimplifyForeachToCoalescingRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @var \PhpParser\Node\Stmt\Return_|null
*/
private $return;
/**
* @readonly
* @var \Rector\Core\NodeManipulator\ForeachManipulator
*/
private $foreachManipulator;
public function __construct(ForeachManipulator $foreachManipulator)
{
$this->foreachManipulator = $foreachManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes foreach that returns set value to ??', [new CodeSample(<<<'CODE_SAMPLE'
foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
if ($currentFunction === $oldFunction) {
return $newFunction;
innerForeachReturn $newFunction;
}
}
return null;
innerForeachReturn null;
CODE_SAMPLE
, <<<'CODE_SAMPLE'
return $this->oldToNewFunctions[$currentFunction] ?? null;
innerForeachReturn $this->oldToNewFunctions[$currentFunction] ?? null;
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
* @innerForeachReturn array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Foreach_::class];
return [StmtsAwareInterface::class];
}
/**
* @param Foreach_ $node
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node) : ?Node
{
$this->return = null;
if (!$node->keyVar instanceof Expr) {
if ($node->stmts === null) {
return null;
}
/** @var Return_|Assign|null $returnOrAssignNode */
$returnOrAssignNode = $this->matchReturnOrAssignNode($node);
if ($returnOrAssignNode === null) {
return null;
$hasChanged = \false;
foreach ($node->stmts as $key => $stmt) {
if (!$stmt instanceof Foreach_) {
continue;
}
$foreach = $stmt;
if (!$foreach->keyVar instanceof Expr) {
continue;
}
$foreachReturnOrAssign = $this->matchForeachReturnOrAssign($foreach);
if ($foreachReturnOrAssign instanceof Expression) {
/** @var Assign $innerAssign */
$innerAssign = $foreachReturnOrAssign->expr;
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $innerAssign->expr)) {
return null;
}
$assign = $this->processForeachNodeWithAssignInside($foreach, $innerAssign);
if (!$assign instanceof Assign) {
return null;
}
$node->stmts[$key] = new Expression($assign);
$hasChanged = \true;
continue;
}
if ($foreachReturnOrAssign instanceof Return_) {
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $foreachReturnOrAssign->expr)) {
return null;
}
$nextStmt = $node->stmts[$key + 1] ?? null;
$return = $this->processForeachNodeWithReturnInside($foreach, $foreachReturnOrAssign, $nextStmt);
$node->stmts[$key] = $return;
// cleanup next return
if ($nextStmt instanceof Return_) {
unset($node->stmts[$key + 1]);
}
$hasChanged = \true;
}
}
// return $newValue;
// we don't return the node value
if (!$this->nodeComparator->areNodesEqual($node->valueVar, $returnOrAssignNode->expr)) {
return null;
if ($hasChanged) {
return $node;
}
if ($returnOrAssignNode instanceof Return_) {
return $this->processForeachNodeWithReturnInside($node, $returnOrAssignNode);
}
return $this->processForeachNodeWithAssignInside($node, $returnOrAssignNode);
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NULL_COALESCE;
}
/**
* @return Assign|Return_|null
* @todo make assign expr generic
* @return Expression<Assign>|Return_|null
*/
private function matchReturnOrAssignNode(Foreach_ $foreach) : ?Node
private function matchForeachReturnOrAssign(Foreach_ $foreach)
{
return $this->foreachManipulator->matchOnlyStmt($foreach, static function (Node $node) : ?Node {
if (!$node instanceof If_) {
return null;
}
if (!$node->cond instanceof Identical) {
return null;
}
if (\count($node->stmts) !== 1) {
return null;
}
$innerNode = $node->stmts[0] instanceof Expression ? $node->stmts[0]->expr : $node->stmts[0];
if ($innerNode instanceof Assign || $innerNode instanceof Return_) {
if ($innerNode instanceof Assign && $innerNode->var instanceof ArrayDimFetch) {
return null;
}
return $innerNode;
}
if (\count($foreach->stmts) !== 1) {
return null;
});
}
$onlyForeachStmt = $foreach->stmts[0];
if (!$onlyForeachStmt instanceof If_) {
return null;
}
$if = $onlyForeachStmt;
if (!$if->cond instanceof Identical) {
return null;
}
if (\count($if->stmts) !== 1) {
return null;
}
$innerStmt = $if->stmts[0];
if ($innerStmt instanceof Return_) {
return $innerStmt;
}
if (!$innerStmt instanceof Expression) {
return null;
}
$innerNode = $innerStmt->expr;
if ($innerNode instanceof Assign) {
if ($innerNode->var instanceof ArrayDimFetch) {
return null;
}
return $innerStmt;
}
return null;
}
private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $return) : ?\PhpParser\Node\Stmt\Return_
private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $innerForeachReturn, ?Stmt $nextStmt) : ?\PhpParser\Node\Stmt\Return_
{
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $return->expr)) {
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $innerForeachReturn->expr)) {
return null;
}
/** @var If_ $ifNode */
@ -132,19 +157,10 @@ CODE_SAMPLE
} else {
return null;
}
$nextNode = $foreach->getAttribute(AttributeKey::NEXT_NODE);
// is next node Return?
if ($nextNode instanceof Return_) {
$this->return = $nextNode;
$this->removeNode($this->return);
}
$coalesce = new Coalesce(new ArrayDimFetch($foreach->expr, $checkedNode), $this->return instanceof Return_ && $this->return->expr instanceof Expr ? $this->return->expr : $checkedNode);
if ($this->return instanceof Return_) {
return new Return_($coalesce);
}
return null;
$coalesce = new Coalesce(new ArrayDimFetch($foreach->expr, $checkedNode), $nextStmt instanceof Return_ && $nextStmt->expr instanceof Expr ? $nextStmt->expr : $checkedNode);
return new Return_($coalesce);
}
private function processForeachNodeWithAssignInside(Foreach_ $foreach, Assign $assign) : ?Node
private function processForeachNodeWithAssignInside(Foreach_ $foreach, Assign $assign) : ?Assign
{
/** @var If_ $ifNode */
$ifNode = $foreach->stmts[0];

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '376f6cbd4a51290c63a42d77ac9fc9a3a5d0ab67';
public const PACKAGE_VERSION = '89bc4d80f5dd4c7e81a616c8b58a921a1470a47b';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-09 19:08:35';
public const RELEASE_DATE = '2023-05-10 05:14:15';
/**
* @var int
*/

View File

@ -1,23 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Core\NodeManipulator;
use PhpParser\Node;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
final class ForeachManipulator
{
/**
* @param callable(Node $node, Foreach_ $foreach): ?Node $callable
*/
public function matchOnlyStmt(Foreach_ $foreach, callable $callable) : ?Node
{
if (\count($foreach->stmts) !== 1) {
return null;
}
$innerNode = $foreach->stmts[0];
$innerNode = $innerNode instanceof Expression ? $innerNode->expr : $innerNode;
return $callable($innerNode, $foreach);
}
}

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit5e0cc74cd5afe070b0f4c42a0e5dbb58::getLoader();
return ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2::getLoader();

View File

@ -1483,7 +1483,6 @@ return array(
'Rector\\Core\\NodeManipulator\\ClassMethodManipulator' => $baseDir . '/src/NodeManipulator/ClassMethodManipulator.php',
'Rector\\Core\\NodeManipulator\\ClassMethodPropertyFetchManipulator' => $baseDir . '/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php',
'Rector\\Core\\NodeManipulator\\Dependency\\DependencyClassMethodDecorator' => $baseDir . '/src/NodeManipulator/Dependency/DependencyClassMethodDecorator.php',
'Rector\\Core\\NodeManipulator\\ForeachManipulator' => $baseDir . '/src/NodeManipulator/ForeachManipulator.php',
'Rector\\Core\\NodeManipulator\\FuncCallManipulator' => $baseDir . '/src/NodeManipulator/FuncCallManipulator.php',
'Rector\\Core\\NodeManipulator\\FunctionLikeManipulator' => $baseDir . '/src/NodeManipulator/FunctionLikeManipulator.php',
'Rector\\Core\\NodeManipulator\\IfManipulator' => $baseDir . '/src/NodeManipulator/IfManipulator.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit5e0cc74cd5afe070b0f4c42a0e5dbb58
class ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit5e0cc74cd5afe070b0f4c42a0e5dbb58
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit5e0cc74cd5afe070b0f4c42a0e5dbb58', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit5e0cc74cd5afe070b0f4c42a0e5dbb58', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInited13d2d55a7019dfa6e7e16a14385fd2', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58
class ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1725,7 +1725,6 @@ class ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58
'Rector\\Core\\NodeManipulator\\ClassMethodManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/ClassMethodManipulator.php',
'Rector\\Core\\NodeManipulator\\ClassMethodPropertyFetchManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/ClassMethodPropertyFetchManipulator.php',
'Rector\\Core\\NodeManipulator\\Dependency\\DependencyClassMethodDecorator' => __DIR__ . '/../..' . '/src/NodeManipulator/Dependency/DependencyClassMethodDecorator.php',
'Rector\\Core\\NodeManipulator\\ForeachManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/ForeachManipulator.php',
'Rector\\Core\\NodeManipulator\\FuncCallManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/FuncCallManipulator.php',
'Rector\\Core\\NodeManipulator\\FunctionLikeManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/FunctionLikeManipulator.php',
'Rector\\Core\\NodeManipulator\\IfManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/IfManipulator.php',
@ -3113,9 +3112,9 @@ class ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5e0cc74cd5afe070b0f4c42a0e5dbb58::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInited13d2d55a7019dfa6e7e16a14385fd2::$classMap;
}, null, ClassLoader::class);
}