Updated Rector to commit a87a9d8e026c3499c980facf1bf24bd51b69c924

a87a9d8e02 Remove NEXT_NODE from ChangeIfElseValueAssignToEarlyReturnRector (#3914)
This commit is contained in:
Tomas Votruba 2023-05-21 15:26:41 +00:00
parent fce66262b4
commit 0693ebbfc2
5 changed files with 50 additions and 48 deletions

View File

@ -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<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;
}
\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);
return null;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '46f5209563e77fb36fe1b04749419fb258e9d710';
public const PACKAGE_VERSION = 'a87a9d8e026c3499c980facf1bf24bd51b69c924';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-21 15:12:34';
public const RELEASE_DATE = '2023-05-21 15:12:55';
/**
* @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';
return ComposerAutoloaderInit8e410524ee1772dba8cd058380b56638::getLoader();
return ComposerAutoloaderInit1abb730cf791a93437dfd16b3894cb75::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit8e410524ee1772dba8cd058380b56638
class ComposerAutoloaderInit1abb730cf791a93437dfd16b3894cb75
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit8e410524ee1772dba8cd058380b56638
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit8e410524ee1772dba8cd058380b56638', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit1abb730cf791a93437dfd16b3894cb75', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit8e410524ee1772dba8cd058380b56638', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit1abb730cf791a93437dfd16b3894cb75', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit8e410524ee1772dba8cd058380b56638::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit1abb730cf791a93437dfd16b3894cb75::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit8e410524ee1772dba8cd058380b56638::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit1abb730cf791a93437dfd16b3894cb75::$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 ComposerStaticInit8e410524ee1772dba8cd058380b56638
class ComposerStaticInit1abb730cf791a93437dfd16b3894cb75
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3115,9 +3115,9 @@ class ComposerStaticInit8e410524ee1772dba8cd058380b56638
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit8e410524ee1772dba8cd058380b56638::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8e410524ee1772dba8cd058380b56638::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8e410524ee1772dba8cd058380b56638::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit1abb730cf791a93437dfd16b3894cb75::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit1abb730cf791a93437dfd16b3894cb75::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit1abb730cf791a93437dfd16b3894cb75::$classMap;
}, null, ClassLoader::class);
}