Updated Rector to commit c2ade0583c

c2ade0583c [DX] Refactor ChangeNestedIfsToEarlyReturnRector to direct return (#2250)
This commit is contained in:
Tomas Votruba 2022-05-07 09:28:12 +00:00
parent 755d463439
commit 2786be52f6
7 changed files with 59 additions and 41 deletions

View File

@ -50,7 +50,7 @@ final class NodesToAddCollector implements \Rector\PostRector\Contract\Collector
return $this->nodesToAddAfter !== [] || $this->nodesToAddBefore !== [];
}
/**
* @internal Try to avoid using this magic added. Better return directly array of nodes in the Rector rule.
* @deprecated Return created nodes right in refactor() method to keep context instead.
*/
public function addNodeBeforeNode(\PhpParser\Node $addedNode, \PhpParser\Node $positionNode) : void
{
@ -64,6 +64,7 @@ final class NodesToAddCollector implements \Rector\PostRector\Contract\Collector
}
/**
* @param Node[] $addedNodes
* @deprecated Return created nodes right in refactor() method to keep context instead.
*/
public function addNodesAfterNode(array $addedNodes, \PhpParser\Node $positionNode) : void
{
@ -112,6 +113,7 @@ final class NodesToAddCollector implements \Rector\PostRector\Contract\Collector
unset($this->nodesToAddBefore[$objectHash]);
}
/**
* @deprecated Return created nodes right in refactor() method to keep context instead.
* @param Node[] $newNodes
*/
public function addNodesBeforeNode(array $newNodes, \PhpParser\Node $positionNode) : void

View File

@ -6,12 +6,15 @@ namespace Rector\EarlyReturn\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\CodeQuality\NodeTypeGroup;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -75,59 +78,72 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\If_::class];
return \Rector\CodeQuality\NodeTypeGroup::STMTS_AWARE;
}
/**
* @param If_ $node
* @param Function_|ClassMethod $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
// A. next node is return
$nextNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE);
if (!$nextNode instanceof \PhpParser\Node\Stmt\Return_) {
$stmts = $node->stmts;
if ($stmts === null) {
return null;
}
$nestedIfsWithOnlyReturn = $this->ifManipulator->collectNestedIfsWithOnlyReturn($node);
if ($nestedIfsWithOnlyReturn === []) {
return null;
foreach ($stmts as $key => $stmt) {
$nextStmt = $stmts[$key + 1] ?? null;
if (!$nextStmt instanceof \PhpParser\Node\Stmt\Return_) {
continue;
}
if (!$stmt instanceof \PhpParser\Node\Stmt\If_) {
continue;
}
$nestedIfsWithOnlyReturn = $this->ifManipulator->collectNestedIfsWithOnlyReturn($stmt);
if ($nestedIfsWithOnlyReturn === []) {
continue;
}
$node->stmts = $this->processNestedIfsWithOnlyReturn($nestedIfsWithOnlyReturn, $nextStmt);
return $node;
}
$this->processNestedIfsWithOnlyReturn($node, $nestedIfsWithOnlyReturn, $nextNode);
$this->removeNode($node);
return null;
}
/**
* @param If_[] $nestedIfsWithOnlyReturn
* @return Stmt[]
*/
private function processNestedIfsWithOnlyReturn(\PhpParser\Node\Stmt\If_ $if, array $nestedIfsWithOnlyReturn, \PhpParser\Node\Stmt\Return_ $nextReturn) : void
private function processNestedIfsWithOnlyReturn(array $nestedIfsWithOnlyReturn, \PhpParser\Node\Stmt\Return_ $nextReturn) : array
{
// add nested if openly after this
$nestedIfsWithOnlyReturnCount = \count($nestedIfsWithOnlyReturn);
$newStmts = [];
/** @var int $key */
foreach ($nestedIfsWithOnlyReturn as $key => $nestedIfWithOnlyReturn) {
// last item → the return node
if ($nestedIfsWithOnlyReturnCount === $key + 1) {
$this->nodesToAddCollector->addNodeAfterNode($nestedIfWithOnlyReturn, $if);
$newStmts[] = $nestedIfWithOnlyReturn;
} else {
$this->addStandaloneIfsWithReturn($nestedIfWithOnlyReturn, $if, $nextReturn);
$standaloneIfs = $this->createStandaloneIfsWithReturn($nestedIfWithOnlyReturn, $nextReturn);
$newStmts = \array_merge($newStmts, $standaloneIfs);
}
}
$newStmts[] = $nextReturn;
return $newStmts;
}
private function addStandaloneIfsWithReturn(\PhpParser\Node\Stmt\If_ $nestedIfWithOnlyReturn, \PhpParser\Node\Stmt\If_ $if, \PhpParser\Node\Stmt\Return_ $return) : void
/**
* @return Stmt[]
*/
private function createStandaloneIfsWithReturn(\PhpParser\Node\Stmt\If_ $nestedIfWithOnlyReturn, \PhpParser\Node\Stmt\Return_ $return) : array
{
$return = clone $return;
$invertedCondition = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);
// special case
if ($invertedCondition instanceof \PhpParser\Node\Expr\BooleanNot && $invertedCondition->expr instanceof \PhpParser\Node\Expr\BinaryOp\BooleanAnd) {
$booleanNotPartIf = new \PhpParser\Node\Stmt\If_(new \PhpParser\Node\Expr\BooleanNot($invertedCondition->expr->left));
$booleanNotPartIf->stmts = [clone $return];
$this->nodesToAddCollector->addNodeAfterNode($booleanNotPartIf, $if);
$secondBooleanNotPartIf = new \PhpParser\Node\Stmt\If_(new \PhpParser\Node\Expr\BooleanNot($invertedCondition->expr->right));
$secondBooleanNotPartIf->stmts = [clone $return];
$this->nodesToAddCollector->addNodeAfterNode($secondBooleanNotPartIf, $if);
return;
return [$booleanNotPartIf, $secondBooleanNotPartIf];
}
$nestedIfWithOnlyReturn->cond = $invertedCondition;
$nestedIfWithOnlyReturn->stmts = [clone $return];
$this->nodesToAddCollector->addNodeAfterNode($nestedIfWithOnlyReturn, $if);
$nestedIfWithOnlyReturn->stmts = [$return];
return [$nestedIfWithOnlyReturn];
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '1f6c7a6a20563e0b808da14688098c4c0ed592fb';
public const PACKAGE_VERSION = 'c2ade0583ccd8fe1413be641924f5b1576ffad8b';
/**
* @var string
*/
public const RELEASE_DATE = '2022-05-07 08:41:36';
public const RELEASE_DATE = '2022-05-07 09:20:29';
/**
* @var string
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587
class ComposerAutoloaderInit408b7589446e352802303607cb19c324
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit408b7589446e352802303607cb19c324', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit408b7589446e352802303607cb19c324', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit575da8f25431377f12c8120ae5fdc587::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit408b7589446e352802303607cb19c324::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit575da8f25431377f12c8120ae5fdc587::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit408b7589446e352802303607cb19c324::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire575da8f25431377f12c8120ae5fdc587($fileIdentifier, $file);
composerRequire408b7589446e352802303607cb19c324($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587
* @param string $file
* @return void
*/
function composerRequire575da8f25431377f12c8120ae5fdc587($fileIdentifier, $file)
function composerRequire408b7589446e352802303607cb19c324($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 ComposerStaticInit575da8f25431377f12c8120ae5fdc587
class ComposerStaticInit408b7589446e352802303607cb19c324
{
public static $files = array (
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
@ -3883,9 +3883,9 @@ class ComposerStaticInit575da8f25431377f12c8120ae5fdc587
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit575da8f25431377f12c8120ae5fdc587::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit575da8f25431377f12c8120ae5fdc587::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit575da8f25431377f12c8120ae5fdc587::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit408b7589446e352802303607cb19c324::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit408b7589446e352802303607cb19c324::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit408b7589446e352802303607cb19c324::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20220507\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587', false) && !interface_exists('ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587', false) && !trait_exists('ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587', false)) {
spl_autoload_call('RectorPrefix20220507\ComposerAutoloaderInit575da8f25431377f12c8120ae5fdc587');
if (!class_exists('ComposerAutoloaderInit408b7589446e352802303607cb19c324', false) && !interface_exists('ComposerAutoloaderInit408b7589446e352802303607cb19c324', false) && !trait_exists('ComposerAutoloaderInit408b7589446e352802303607cb19c324', false)) {
spl_autoload_call('RectorPrefix20220507\ComposerAutoloaderInit408b7589446e352802303607cb19c324');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20220507\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220507\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire575da8f25431377f12c8120ae5fdc587')) {
function composerRequire575da8f25431377f12c8120ae5fdc587() {
return \RectorPrefix20220507\composerRequire575da8f25431377f12c8120ae5fdc587(...func_get_args());
if (!function_exists('composerRequire408b7589446e352802303607cb19c324')) {
function composerRequire408b7589446e352802303607cb19c324() {
return \RectorPrefix20220507\composerRequire408b7589446e352802303607cb19c324(...func_get_args());
}
}
if (!function_exists('scanPath')) {