From c1e94593a2c82a346e9f9196e6ac2067901c4bff Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 21 May 2023 15:17:01 +0000 Subject: [PATCH] Updated Rector to commit a87a9d8e026c3499c980facf1bf24bd51b69c924 https://github.com/rectorphp/rector-src/commit/a87a9d8e026c3499c980facf1bf24bd51b69c924 Remove NEXT_NODE from ChangeIfElseValueAssignToEarlyReturnRector (#3914) --- packages/NodeNestingScope/ContextAnalyzer.php | 37 +--------- .../If_/ChangeAndIfToEarlyReturnRector.php | 45 +++-------- ...geIfElseValueAssignToEarlyReturnRector.php | 74 ++++++++++--------- .../NodeResolver/SwitchExprsResolver.php | 8 +- src/Application/VersionResolver.php | 4 +- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 10 +-- vendor/composer/autoload_static.php | 8 +- 8 files changed, 68 insertions(+), 120 deletions(-) diff --git a/packages/NodeNestingScope/ContextAnalyzer.php b/packages/NodeNestingScope/ContextAnalyzer.php index 48df791aa7d..7784df5f212 100644 --- a/packages/NodeNestingScope/ContextAnalyzer.php +++ b/packages/NodeNestingScope/ContextAnalyzer.php @@ -4,18 +4,12 @@ declare (strict_types=1); namespace Rector\NodeNestingScope; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\Assign; use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\If_; -use PhpParser\Node\Stmt\Return_; use PhpParser\Node\Stmt\Switch_; -use PHPStan\Type\ObjectType; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\NodeNestingScope\ValueObject\ControlStructure; -use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\NodeTypeResolver\NodeTypeResolver; final class ContextAnalyzer { /** @@ -28,15 +22,9 @@ final class ContextAnalyzer * @var \Rector\Core\PhpParser\Node\BetterNodeFinder */ private $betterNodeFinder; - /** - * @readonly - * @var \Rector\NodeTypeResolver\NodeTypeResolver - */ - private $nodeTypeResolver; - public function __construct(BetterNodeFinder $betterNodeFinder, NodeTypeResolver $nodeTypeResolver) + public function __construct(BetterNodeFinder $betterNodeFinder) { $this->betterNodeFinder = $betterNodeFinder; - $this->nodeTypeResolver = $nodeTypeResolver; } public function isInLoop(Node $node) : bool { @@ -72,27 +60,4 @@ final class ContextAnalyzer } 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; - } } diff --git a/rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php b/rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php index 99026d49ed7..b398af15dca 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php @@ -6,15 +6,18 @@ namespace Rector\EarlyReturn\Rector\If_; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; +use PhpParser\Node\Expr\Closure; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; +use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Continue_; -use PhpParser\Node\Stmt\Else_; -use PhpParser\Node\Stmt\ElseIf_; +use PhpParser\Node\Stmt\Foreach_; +use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; +use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Return_; -use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Core\NodeManipulator\IfManipulator; +use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Core\Rector\AbstractRector; use Rector\EarlyReturn\NodeAnalyzer\IfAndAnalyzer; use Rector\EarlyReturn\NodeAnalyzer\SimpleScalarAnalyzer; @@ -107,10 +110,10 @@ CODE_SAMPLE */ 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 { @@ -126,7 +129,7 @@ CODE_SAMPLE continue; } $nextStmt = $stmts[$key + 1] ?? null; - if ($this->shouldSkip($node, $stmt, $nextStmt)) { + if ($this->shouldSkip($stmt, $nextStmt)) { $newStmts[] = $stmt; continue; } @@ -192,7 +195,7 @@ CODE_SAMPLE } 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)) { return \true; @@ -203,12 +206,6 @@ CODE_SAMPLE if (!$this->ifManipulator->isIfWithoutElseAndElseIfs($if)) { return \true; } - if ($this->isParentIfReturnsVoidOrParentIfHasNextNode($stmtsAware)) { - return \true; - } - if ($this->isNestedIfInLoop($if, $stmtsAware)) { - return \true; - } // is simple return? skip it $onlyStmt = $if->stmts[0]; if ($onlyStmt instanceof Return_ && $onlyStmt->expr instanceof Expr && $this->simpleScalarAnalyzer->isSimpleScalar($onlyStmt->expr)) { @@ -219,26 +216,6 @@ CODE_SAMPLE } 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 { if ($nextStmt instanceof Node) { @@ -251,6 +228,6 @@ CODE_SAMPLE if ($parentNode instanceof If_) { return $this->isLastIfOrBeforeLastReturn($parentNode, $nextStmt); } - return !$this->contextAnalyzer->hasAssignWithIndirectReturn($parentNode, $if); + return \true; } } diff --git a/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php b/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php index 212a8aced2b..325a895be2c 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php @@ -10,11 +10,10 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\If_; 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\StmtsManipulator; use Rector\Core\Rector\AbstractRector; -use Rector\NodeTypeResolver\Node\AttributeKey; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** @@ -75,46 +74,49 @@ CODE_SAMPLE */ public function getNodeTypes() : array { - return [If_::class]; + return [StmtsAwareInterface::class]; } /** - * @param If_ $node - * @return Stmt[]|null + * @param StmtsAwareInterface $node */ - public function refactor(Node $node) : ?array + public function refactor(Node $node) : ?StmtsAwareInterface { - $nextNode = $node->getAttribute(AttributeKey::NEXT_NODE); - if (!$nextNode instanceof Return_) { + if ($node->stmts === null) { return null; } - if (!$nextNode->expr instanceof Expr) { - return null; + foreach ($node->stmts as $key => $stmt) { + 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 $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; - } - \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 $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); + return null; } } diff --git a/rules/Php80/NodeResolver/SwitchExprsResolver.php b/rules/Php80/NodeResolver/SwitchExprsResolver.php index 9cb719ac7ec..ccde57f7fc3 100644 --- a/rules/Php80/NodeResolver/SwitchExprsResolver.php +++ b/rules/Php80/NodeResolver/SwitchExprsResolver.php @@ -29,9 +29,13 @@ final class SwitchExprsResolver if (!$this->isValidCase($case)) { return []; } - if ($case->stmts === [] && $case->cond instanceof Expr) { - $collectionEmptyCasesCond[$key] = $case->cond; + if ($case->stmts !== []) { + continue; } + if (!$case->cond instanceof Expr) { + continue; + } + $collectionEmptyCasesCond[$key] = $case->cond; } foreach ($newSwitch->cases as $key => $case) { if ($case->stmts === []) { diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 11d756ccec2..93b9ba12515 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '26ca0e49ec38adfbff5432e3e0184a8f64c6176d'; + public const PACKAGE_VERSION = 'a87a9d8e026c3499c980facf1bf24bd51b69c924'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-05-21 14:42:50'; + public const RELEASE_DATE = '2023-05-21 15:12:55'; /** * @var int */ diff --git a/vendor/autoload.php b/vendor/autoload.php index bdbe2167cba..b090f465f79 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit250a25aef81e8841646e46deac33cbe4::getLoader(); +return ComposerAutoloaderInit561dd799166882480228bd04f30412e7::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 3f750d6c451..4020194f1c1 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit250a25aef81e8841646e46deac33cbe4 +class ComposerAutoloaderInit561dd799166882480228bd04f30412e7 { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit250a25aef81e8841646e46deac33cbe4 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__)); - spl_autoload_unregister(array('ComposerAutoloaderInit250a25aef81e8841646e46deac33cbe4', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit561dd799166882480228bd04f30412e7', 'loadClassLoader')); 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->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit561dd799166882480228bd04f30412e7::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 0374fce0c18..5ee900ad33d 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit250a25aef81e8841646e46deac33cbe4 +class ComposerStaticInit561dd799166882480228bd04f30412e7 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3115,9 +3115,9 @@ class ComposerStaticInit250a25aef81e8841646e46deac33cbe4 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit250a25aef81e8841646e46deac33cbe4::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit561dd799166882480228bd04f30412e7::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit561dd799166882480228bd04f30412e7::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit561dd799166882480228bd04f30412e7::$classMap; }, null, ClassLoader::class); }