Updated Rector to commit a813b1747dc78a40a22c993b1d8ebbc47e261298

a813b1747d Remove ChangeOrIfReturnToEarlyReturnRector as makes code harder to read by duplication, rather use PHSPtan and extract method (#3916)
This commit is contained in:
Tomas Votruba 2023-05-22 07:55:47 +00:00
parent 060e167ec1
commit e59b9e0910
11 changed files with 79 additions and 252 deletions

View File

@ -9,12 +9,11 @@ use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\ChangeNestedIfsToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector;
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([ChangeNestedForeachIfsToEarlyContinueRector::class, ChangeAndIfToEarlyReturnRector::class, ChangeIfElseValueAssignToEarlyReturnRector::class, ChangeNestedIfsToEarlyReturnRector::class, RemoveAlwaysElseRector::class, ReturnBinaryAndToEarlyReturnRector::class, ChangeOrIfReturnToEarlyReturnRector::class, ChangeOrIfContinueToMultiContinueRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class]);
$rectorConfig->rules([ChangeNestedForeachIfsToEarlyContinueRector::class, ChangeAndIfToEarlyReturnRector::class, ChangeIfElseValueAssignToEarlyReturnRector::class, ChangeNestedIfsToEarlyReturnRector::class, RemoveAlwaysElseRector::class, ReturnBinaryAndToEarlyReturnRector::class, ChangeOrIfContinueToMultiContinueRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class]);
};

View File

@ -1,4 +1,4 @@
# 405 Rules Overview
# 404 Rules Overview
<br>
@ -16,7 +16,7 @@
- [DependencyInjection](#dependencyinjection) (2)
- [EarlyReturn](#earlyreturn) (11)
- [EarlyReturn](#earlyreturn) (10)
- [MysqlToMysqli](#mysqltomysqli) (4)
@ -3835,32 +3835,6 @@ Changes if || to early return
<br>
### ChangeOrIfReturnToEarlyReturnRector
Changes if || with return to early return
- class: [`Rector\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector`](../rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php)
```diff
class SomeClass
{
public function run($a, $b)
{
- if ($a || $b) {
+ if ($a) {
+ return null;
+ }
+ if ($b) {
return null;
}
return 'another';
}
}
```
<br>
### PreparedValueToEarlyReturnRector
Return early prepared value in ifs

View File

@ -1,144 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\EarlyReturn\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector\ChangeOrIfReturnToEarlyReturnRectorTest
*/
final class ChangeOrIfReturnToEarlyReturnRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\IfManipulator
*/
private $ifManipulator;
public function __construct(IfManipulator $ifManipulator)
{
$this->ifManipulator = $ifManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes if || with return to early return', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run($a, $b)
{
if ($a || $b) {
return null;
}
return 'another';
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run($a, $b)
{
if ($a) {
return null;
}
if ($b) {
return null;
}
return 'another';
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
* @return null|If_[]
*/
public function refactor(Node $node) : ?array
{
if (!$this->ifManipulator->isIfWithOnly($node, Return_::class)) {
return null;
}
if (!$node->cond instanceof BooleanOr) {
return null;
}
if ($this->isInstanceofCondOnly($node->cond)) {
return null;
}
/** @var Return_ $return */
$return = $node->stmts[0];
// same return? skip
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
if ($nextNode instanceof Return_ && $this->nodeComparator->areNodesEqual($return, $nextNode)) {
return null;
}
$ifs = $this->createMultipleIfs($node->cond, $return, []);
// ensure ifs not removed by other rules
if ($ifs === []) {
return null;
}
$this->mirrorComments($ifs[0], $node);
return $ifs;
}
/**
* @param If_[] $ifs
* @return If_[]
*/
private function createMultipleIfs(BooleanOr $booleanOr, Return_ $return, array $ifs) : array
{
while ($booleanOr instanceof BooleanOr) {
$ifs = \array_merge($ifs, $this->collectLeftBooleanOrToIfs($booleanOr, $return, $ifs));
$ifs[] = $this->createIf($booleanOr->right, $return);
$booleanOr = $booleanOr->right;
}
return $ifs + [$this->createIf($booleanOr, $return)];
}
/**
* @param If_[] $ifs
* @return If_[]
*/
private function collectLeftBooleanOrToIfs(BooleanOr $booleanOr, Return_ $return, array $ifs) : array
{
$left = $booleanOr->left;
if (!$left instanceof BooleanOr) {
return [$this->createIf($left, $return)];
}
return $this->createMultipleIfs($left, $return, $ifs);
}
private function createIf(Expr $expr, Return_ $return) : If_
{
return new If_($expr, ['stmts' => [$return]]);
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\BooleanOr|\PhpParser\Node\Expr\BinaryOp $booleanOr
*/
private function isInstanceofCondOnly($booleanOr) : bool
{
if ($booleanOr->left instanceof BinaryOp) {
return $this->isInstanceofCondOnly($booleanOr->left);
}
if ($booleanOr->right instanceof BinaryOp) {
return $this->isInstanceofCondOnly($booleanOr->right);
}
return $booleanOr->left instanceof Instanceof_ || $booleanOr->right instanceof Instanceof_;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'edc60fc2e9b431a69b66eac9318e9af3eb340d84';
public const PACKAGE_VERSION = 'a813b1747dc78a40a22c993b1d8ebbc47e261298';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-22 08:30:59';
public const RELEASE_DATE = '2023-05-22 07:51:50';
/**
* @var int
*/

View File

@ -15,7 +15,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v17';
private const CACHE_KEY = 'v18';
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
*/

View File

@ -479,6 +479,68 @@ final class BetterNodeFinder
}
return null;
}
/**
* @api
*
* Resolve previous node from any Node, eg: Expr, Identifier, Name, etc
*/
public function resolvePreviousNode(Node $node) : ?Node
{
$currentStmt = $this->resolveCurrentStatement($node);
// just added
if (!$currentStmt instanceof Stmt) {
return null;
}
// just added
$startTokenPos = $node->getStartTokenPos();
if ($startTokenPos < 0) {
return null;
}
$nodes = $currentStmt->getStartTokenPos() === $startTokenPos ? [] : $this->find($currentStmt, static function (Node $subNode) use($startTokenPos) : bool {
return $subNode->getEndTokenPos() < $startTokenPos;
});
if ($nodes === []) {
$parentNode = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
if (!$this->isAllowedParentNode($parentNode)) {
return null;
}
$currentStmtKey = $currentStmt->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
return $parentNode->stmts[$currentStmtKey - 1] ?? null;
}
return \end($nodes);
}
/**
* @api
*
* Resolve next node from any Node, eg: Expr, Identifier, Name, etc
*/
public function resolveNextNode(Node $node) : ?Node
{
$currentStmt = $this->resolveCurrentStatement($node);
// just added
if (!$currentStmt instanceof Stmt) {
return null;
}
// just added
$endTokenPos = $node->getEndTokenPos();
if ($endTokenPos < 0) {
return null;
}
$nextNode = $currentStmt->getEndTokenPos() === $endTokenPos ? null : $this->findFirst($currentStmt, static function (Node $subNode) use($endTokenPos) : bool {
return $subNode->getStartTokenPos() > $endTokenPos;
});
if (!$nextNode instanceof Node) {
$parentNode = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
if (!$this->isAllowedParentNode($parentNode)) {
return null;
}
$currentStmtKey = $currentStmt->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
return $this->resolveNeighborNextStmt($parentNode, $currentStmt, $currentStmtKey);
}
return $nextNode;
}
private function isAllowedParentNode(?Node $node) : bool
{
return $node instanceof StmtsAwareInterface || $node instanceof ClassLike || $node instanceof Declare_;
@ -617,68 +679,6 @@ final class BetterNodeFinder
}
return $newStmts[$currentStmtKey];
}
/**
* @api
*
* Resolve previous node from any Node, eg: Expr, Identifier, Name, etc
*/
public function resolvePreviousNode(Node $node) : ?Node
{
$currentStmt = $this->resolveCurrentStatement($node);
// just added
if (!$currentStmt instanceof Stmt) {
return null;
}
// just added
$startTokenPos = $node->getStartTokenPos();
if ($startTokenPos < 0) {
return null;
}
$nodes = $currentStmt->getStartTokenPos() === $startTokenPos ? [] : $this->find($currentStmt, static function (Node $subNode) use($startTokenPos) : bool {
return $subNode->getEndTokenPos() < $startTokenPos;
});
if ($nodes === []) {
$parentNode = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
if (!$this->isAllowedParentNode($parentNode)) {
return null;
}
$currentStmtKey = $currentStmt->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
return $parentNode->stmts[$currentStmtKey - 1] ?? null;
}
return \end($nodes);
}
/**
* @api
*
* Resolve next node from any Node, eg: Expr, Identifier, Name, etc
*/
public function resolveNextNode(Node $node) : ?Node
{
$currentStmt = $this->resolveCurrentStatement($node);
// just added
if (!$currentStmt instanceof Stmt) {
return null;
}
// just added
$endTokenPos = $node->getEndTokenPos();
if ($endTokenPos < 0) {
return null;
}
$nextNode = $currentStmt->getEndTokenPos() === $endTokenPos ? null : $this->findFirst($currentStmt, static function (Node $subNode) use($endTokenPos) : bool {
return $subNode->getStartTokenPos() > $endTokenPos;
});
if (!$nextNode instanceof Node) {
$parentNode = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
if (!$this->isAllowedParentNode($parentNode)) {
return null;
}
$currentStmtKey = $currentStmt->getAttribute(AttributeKey::STMT_KEY);
/** @var StmtsAwareInterface|ClassLike|Declare_ $parentNode */
return $this->resolveNeighborNextStmt($parentNode, $currentStmt, $currentStmtKey);
}
return $nextNode;
}
/**
* Only search in previous Node/Stmt
*

View File

@ -3,11 +3,11 @@
declare (strict_types=1);
namespace Rector\Core\PhpParser\Parser;
use PhpParser\Node\Expr\Variable;
use RectorPrefix202305\Nette\Utils\FileSystem;
use RectorPrefix202305\Nette\Utils\Strings;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;

2
vendor/autoload.php vendored
View File

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

View File

@ -1861,7 +1861,6 @@ return array(
'Rector\\EarlyReturn\\Rector\\If_\\ChangeIfElseValueAssignToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\ChangeNestedIfsToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\ChangeOrIfContinueToMultiContinueRector' => $baseDir . '/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\ChangeOrIfReturnToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\RemoveAlwaysElseRector' => $baseDir . '/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php',
'Rector\\EarlyReturn\\Rector\\Return_\\PreparedValueToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\Return_\\ReturnBinaryAndToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitfc3804475fd4d668551eeb64cd1b908b
class ComposerAutoloaderInitfe597e14c97348bc2e9b33ad274fcb7b
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitfc3804475fd4d668551eeb64cd1b908b
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitfc3804475fd4d668551eeb64cd1b908b', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitfe597e14c97348bc2e9b33ad274fcb7b', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitfc3804475fd4d668551eeb64cd1b908b', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitfe597e14c97348bc2e9b33ad274fcb7b', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitfe597e14c97348bc2e9b33ad274fcb7b::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitfe597e14c97348bc2e9b33ad274fcb7b::$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 ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b
class ComposerStaticInitfe597e14c97348bc2e9b33ad274fcb7b
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2103,7 +2103,6 @@ class ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b
'Rector\\EarlyReturn\\Rector\\If_\\ChangeIfElseValueAssignToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\ChangeNestedIfsToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\ChangeOrIfContinueToMultiContinueRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\ChangeOrIfReturnToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/If_/ChangeOrIfReturnToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\If_\\RemoveAlwaysElseRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php',
'Rector\\EarlyReturn\\Rector\\Return_\\PreparedValueToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php',
'Rector\\EarlyReturn\\Rector\\Return_\\ReturnBinaryAndToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php',
@ -3115,9 +3114,9 @@ class ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfc3804475fd4d668551eeb64cd1b908b::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitfe597e14c97348bc2e9b33ad274fcb7b::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfe597e14c97348bc2e9b33ad274fcb7b::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfe597e14c97348bc2e9b33ad274fcb7b::$classMap;
}, null, ClassLoader::class);
}