Updated Rector to commit a87a9d8e026c3499c980facf1bf24bd51b69c924

a87a9d8e02 Remove NEXT_NODE from ChangeIfElseValueAssignToEarlyReturnRector (#3914)
This commit is contained in:
Tomas Votruba 2023-05-21 15:17:01 +00:00
parent 63128d63b6
commit c1e94593a2
8 changed files with 68 additions and 120 deletions

View File

@ -4,18 +4,12 @@ declare (strict_types=1);
namespace Rector\NodeNestingScope; namespace Rector\NodeNestingScope;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\FunctionLike; use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_; use PhpParser\Node\Stmt\Switch_;
use PHPStan\Type\ObjectType;
use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNestingScope\ValueObject\ControlStructure; use Rector\NodeNestingScope\ValueObject\ControlStructure;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
final class ContextAnalyzer final class ContextAnalyzer
{ {
/** /**
@ -28,15 +22,9 @@ final class ContextAnalyzer
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder * @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/ */
private $betterNodeFinder; private $betterNodeFinder;
/** public function __construct(BetterNodeFinder $betterNodeFinder)
* @readonly
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
public function __construct(BetterNodeFinder $betterNodeFinder, NodeTypeResolver $nodeTypeResolver)
{ {
$this->betterNodeFinder = $betterNodeFinder; $this->betterNodeFinder = $betterNodeFinder;
$this->nodeTypeResolver = $nodeTypeResolver;
} }
public function isInLoop(Node $node) : bool public function isInLoop(Node $node) : bool
{ {
@ -72,27 +60,4 @@ final class ContextAnalyzer
} }
return $previousNode instanceof If_; return $previousNode instanceof If_;
} }
public function hasAssignWithIndirectReturn(Node $node, If_ $if) : bool
{
foreach (ControlStructure::LOOP_NODES as $loopNode) {
$loopObjectType = new ObjectType($loopNode);
$parentType = $this->nodeTypeResolver->getType($node);
$superType = $parentType->isSuperTypeOf($loopObjectType);
if (!$superType->yes()) {
continue;
}
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
if ($nextNode instanceof Node) {
if ($nextNode instanceof Return_ && !$nextNode->expr instanceof Expr) {
continue;
}
$hasAssign = (bool) $this->betterNodeFinder->findFirstInstanceOf($if->stmts, Assign::class);
if (!$hasAssign) {
continue;
}
return \true;
}
}
return \false;
}
} }

View File

@ -6,15 +6,18 @@ namespace Rector\EarlyReturn\Rector\If_;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr; use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Continue_; use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\ElseIf_; use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Return_;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\IfManipulator; use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\EarlyReturn\NodeAnalyzer\IfAndAnalyzer; use Rector\EarlyReturn\NodeAnalyzer\IfAndAnalyzer;
use Rector\EarlyReturn\NodeAnalyzer\SimpleScalarAnalyzer; use Rector\EarlyReturn\NodeAnalyzer\SimpleScalarAnalyzer;
@ -107,10 +110,10 @@ CODE_SAMPLE
*/ */
public function getNodeTypes() : array public function getNodeTypes() : array
{ {
return [StmtsAwareInterface::class]; return [ClassMethod::class, Function_::class, Foreach_::class, Closure::class, FileWithoutNamespace::class, Namespace_::class];
} }
/** /**
* @param StmtsAwareInterface $node * @param Stmt\ClassMethod|Stmt\Function_|Stmt\Foreach_|Expr\Closure|FileWithoutNamespace|Stmt\Namespace_ $node
*/ */
public function refactor(Node $node) : ?Node public function refactor(Node $node) : ?Node
{ {
@ -126,7 +129,7 @@ CODE_SAMPLE
continue; continue;
} }
$nextStmt = $stmts[$key + 1] ?? null; $nextStmt = $stmts[$key + 1] ?? null;
if ($this->shouldSkip($node, $stmt, $nextStmt)) { if ($this->shouldSkip($stmt, $nextStmt)) {
$newStmts[] = $stmt; $newStmts[] = $stmt;
continue; continue;
} }
@ -192,7 +195,7 @@ CODE_SAMPLE
} }
return \array_merge($result, [$ifNextReturnClone]); return \array_merge($result, [$ifNextReturnClone]);
} }
private function shouldSkip(StmtsAwareInterface $stmtsAware, If_ $if, ?Stmt $nexStmt) : bool private function shouldSkip(If_ $if, ?Stmt $nexStmt) : bool
{ {
if (!$this->ifManipulator->isIfWithOnlyOneStmt($if)) { if (!$this->ifManipulator->isIfWithOnlyOneStmt($if)) {
return \true; return \true;
@ -203,12 +206,6 @@ CODE_SAMPLE
if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($if)) { if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($if)) {
return \true; return \true;
} }
if ($this->isParentIfReturnsVoidOrParentIfHasNextNode($stmtsAware)) {
return \true;
}
if ($this->isNestedIfInLoop($if, $stmtsAware)) {
return \true;
}
// is simple return? skip it // is simple return? skip it
$onlyStmt = $if->stmts[0]; $onlyStmt = $if->stmts[0];
if ($onlyStmt instanceof Return_ && $onlyStmt->expr instanceof Expr && $this->simpleScalarAnalyzer->isSimpleScalar($onlyStmt->expr)) { if ($onlyStmt instanceof Return_ && $onlyStmt->expr instanceof Expr && $this->simpleScalarAnalyzer->isSimpleScalar($onlyStmt->expr)) {
@ -219,26 +216,6 @@ CODE_SAMPLE
} }
return !$this->isLastIfOrBeforeLastReturn($if, $nexStmt); return !$this->isLastIfOrBeforeLastReturn($if, $nexStmt);
} }
private function isParentIfReturnsVoidOrParentIfHasNextNode(StmtsAwareInterface $stmtsAware) : bool
{
if (!$stmtsAware instanceof If_) {
$parent = $stmtsAware->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof If_) {
$node = $parent->getAttribute(AttributeKey::NEXT_NODE);
return !$node instanceof Return_;
}
return \false;
}
$nextNode = $stmtsAware->getAttribute(AttributeKey::NEXT_NODE);
return $nextNode instanceof Node;
}
private function isNestedIfInLoop(If_ $if, StmtsAwareInterface $stmtsAware) : bool
{
if (!$this->contextAnalyzer->isInLoop($if)) {
return \false;
}
return $stmtsAware instanceof If_ || $stmtsAware instanceof Else_ || $stmtsAware instanceof ElseIf_;
}
private function isLastIfOrBeforeLastReturn(If_ $if, ?Stmt $nextStmt) : bool private function isLastIfOrBeforeLastReturn(If_ $if, ?Stmt $nextStmt) : bool
{ {
if ($nextStmt instanceof Node) { if ($nextStmt instanceof Node) {
@ -251,6 +228,6 @@ CODE_SAMPLE
if ($parentNode instanceof If_) { if ($parentNode instanceof If_) {
return $this->isLastIfOrBeforeLastReturn($parentNode, $nextStmt); return $this->isLastIfOrBeforeLastReturn($parentNode, $nextStmt);
} }
return !$this->contextAnalyzer->hasAssignWithIndirectReturn($parentNode, $if); return \true;
} }
} }

View File

@ -10,11 +10,10 @@ use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\IfManipulator; use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\NodeManipulator\StmtsManipulator; use Rector\Core\NodeManipulator\StmtsManipulator;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/** /**
@ -75,46 +74,49 @@ CODE_SAMPLE
*/ */
public function getNodeTypes() : array public function getNodeTypes() : array
{ {
return [If_::class]; return [StmtsAwareInterface::class];
} }
/** /**
* @param If_ $node * @param StmtsAwareInterface $node
* @return Stmt[]|null
*/ */
public function refactor(Node $node) : ?array public function refactor(Node $node) : ?StmtsAwareInterface
{ {
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE); if ($node->stmts === null) {
if (!$nextNode instanceof Return_) {
return null; return null;
} }
if (!$nextNode->expr instanceof Expr) { foreach ($node->stmts as $key => $stmt) {
return null; if (!$stmt instanceof Return_) {
continue;
}
if (!$stmt->expr instanceof Expr) {
continue;
}
$previousStmt = $node->stmts[$key - 1] ?? null;
if (!$previousStmt instanceof If_) {
continue;
}
$if = $previousStmt;
if (!$this->ifManipulator->isIfAndElseWithSameVariableAssignAsLastStmts($if, $stmt->expr)) {
continue;
}
\end($if->stmts);
$lastIfStmtKey = \key($if->stmts);
/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($if->stmts);
$returnLastIf = new Return_($assign->expr);
$this->mirrorComments($returnLastIf, $assign);
$if->stmts[$lastIfStmtKey] = $returnLastIf;
/** @var Else_ $else */
$else = $if->else;
/** @var array<int, Stmt> $elseStmts */
$elseStmts = $else->stmts;
/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($elseStmts);
$this->mirrorComments($stmt, $assign);
$if->else = null;
$stmt->expr = $assign->expr;
return $node;
} }
if (!$this->ifManipulator->isIfAndElseWithSameVariableAssignAsLastStmts($node, $nextNode->expr)) { return null;
return null;
}
\end($node->stmts);
$lastIfStmtKey = \key($node->stmts);
/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($node->stmts);
$returnLastIf = new Return_($assign->expr);
$this->mirrorComments($returnLastIf, $assign);
$node->stmts[$lastIfStmtKey] = $returnLastIf;
$else = $node->else;
if (!$else instanceof Else_) {
throw new ShouldNotHappenException();
}
/** @var array<int, Stmt> $elseStmts */
$elseStmts = $else->stmts;
/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($elseStmts);
\end($elseStmts);
$lastElseStmtKey = \key($elseStmts);
$returnLastElse = new Return_($assign->expr);
$this->mirrorComments($returnLastElse, $assign);
$elseStmts[$lastElseStmtKey] = $returnLastElse;
$node->else = null;
$this->removeNode($nextNode);
return \array_merge([$node], $elseStmts);
} }
} }

View File

@ -29,9 +29,13 @@ final class SwitchExprsResolver
if (!$this->isValidCase($case)) { if (!$this->isValidCase($case)) {
return []; return [];
} }
if ($case->stmts === [] && $case->cond instanceof Expr) { if ($case->stmts !== []) {
$collectionEmptyCasesCond[$key] = $case->cond; continue;
} }
if (!$case->cond instanceof Expr) {
continue;
}
$collectionEmptyCasesCond[$key] = $case->cond;
} }
foreach ($newSwitch->cases as $key => $case) { foreach ($newSwitch->cases as $key => $case) {
if ($case->stmts === []) { if ($case->stmts === []) {

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '26ca0e49ec38adfbff5432e3e0184a8f64c6176d'; public const PACKAGE_VERSION = 'a87a9d8e026c3499c980facf1bf24bd51b69c924';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2023-05-21 14:42:50'; public const RELEASE_DATE = '2023-05-21 15:12:55';
/** /**
* @var int * @var int
*/ */

2
vendor/autoload.php vendored
View File

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

View File

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

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInit250a25aef81e8841646e46deac33cbe4 class ComposerStaticInit561dd799166882480228bd04f30412e7
{ {
public static $files = array ( public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3115,9 +3115,9 @@ class ComposerStaticInit250a25aef81e8841646e46deac33cbe4
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInit561dd799166882480228bd04f30412e7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInit561dd799166882480228bd04f30412e7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$classMap; $loader->classMap = ComposerStaticInit561dd799166882480228bd04f30412e7::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }