mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-16 21:08:19 +01:00
Updated Rector to commit db16e33a9a72d6c75600187d9f7851060d9a88fe
db16e33a9a
[EarlyReturn] Remove ReturnBinaryAndToEarlyReturnRector as creates code hard to read and flips condition (#4478)
This commit is contained in:
parent
e73319defd
commit
fb5e28c38b
@ -11,9 +11,8 @@ use Rector\EarlyReturn\Rector\If_\ChangeNestedIfsToEarlyReturnRector;
|
||||
use Rector\EarlyReturn\Rector\If_\ChangeOrIfContinueToMultiContinueRector;
|
||||
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, ChangeOrIfContinueToMultiContinueRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class]);
|
||||
$rectorConfig->rules([ChangeNestedForeachIfsToEarlyContinueRector::class, ChangeAndIfToEarlyReturnRector::class, ChangeIfElseValueAssignToEarlyReturnRector::class, ChangeNestedIfsToEarlyReturnRector::class, RemoveAlwaysElseRector::class, ChangeOrIfContinueToMultiContinueRector::class, PreparedValueToEarlyReturnRector::class, ReturnBinaryOrToEarlyReturnRector::class, ReturnEarlyIfVariableRector::class]);
|
||||
};
|
||||
|
@ -1,4 +1,4 @@
|
||||
# 362 Rules Overview
|
||||
# 361 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
- [DeadCode](#deadcode) (42)
|
||||
|
||||
- [EarlyReturn](#earlyreturn) (10)
|
||||
- [EarlyReturn](#earlyreturn) (9)
|
||||
|
||||
- [Naming](#naming) (6)
|
||||
|
||||
@ -3377,29 +3377,6 @@ Split if statement, when if condition always break execution flow
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnBinaryAndToEarlyReturnRector
|
||||
|
||||
Changes Single return of && to early returns
|
||||
|
||||
- class: [`Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector`](../rules/EarlyReturn/Rector/Return_/ReturnBinaryAndToEarlyReturnRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function accept()
|
||||
{
|
||||
- return $this->something() && $this->somethingelse();
|
||||
+ if (! $this->something()) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ return (bool) $this->somethingelse();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnBinaryOrToEarlyReturnRector
|
||||
|
||||
Changes Single return of || to early returns
|
||||
|
@ -1,125 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\EarlyReturn\Rector\Return_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\NodeAnalyzer\CallAnalyzer;
|
||||
use Rector\Core\NodeManipulator\IfManipulator;
|
||||
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector\ReturnBinaryAndToEarlyReturnRectorTest
|
||||
*/
|
||||
final class ReturnBinaryAndToEarlyReturnRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\IfManipulator
|
||||
*/
|
||||
private $ifManipulator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\AssignAndBinaryMap
|
||||
*/
|
||||
private $assignAndBinaryMap;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeAnalyzer\CallAnalyzer
|
||||
*/
|
||||
private $callAnalyzer;
|
||||
public function __construct(IfManipulator $ifManipulator, AssignAndBinaryMap $assignAndBinaryMap, CallAnalyzer $callAnalyzer)
|
||||
{
|
||||
$this->ifManipulator = $ifManipulator;
|
||||
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
||||
$this->callAnalyzer = $callAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Changes Single return of && to early returns', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function accept()
|
||||
{
|
||||
return $this->something() && $this->somethingelse();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function accept()
|
||||
{
|
||||
if (! $this->something()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->somethingelse();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Return_::class];
|
||||
}
|
||||
/**
|
||||
* @param Return_ $node
|
||||
* @return null|Node[]
|
||||
*/
|
||||
public function refactor(Node $node) : ?array
|
||||
{
|
||||
if (!$node->expr instanceof BooleanAnd) {
|
||||
return null;
|
||||
}
|
||||
$left = $node->expr->left;
|
||||
$ifNegations = $this->createMultipleIfsNegation($left, $node, []);
|
||||
// ensure ifs not removed by other rules
|
||||
if ($ifNegations === []) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->callAnalyzer->doesIfHasObjectCall($ifNegations)) {
|
||||
return null;
|
||||
}
|
||||
$this->mirrorComments($ifNegations[0], $node);
|
||||
/** @var BooleanAnd $booleanAnd */
|
||||
$booleanAnd = $node->expr;
|
||||
$lastReturnExpr = $this->assignAndBinaryMap->getTruthyExpr($booleanAnd->right);
|
||||
return \array_merge($ifNegations, [new Return_($lastReturnExpr)]);
|
||||
}
|
||||
/**
|
||||
* @param If_[] $ifNegations
|
||||
* @return If_[]
|
||||
*/
|
||||
private function createMultipleIfsNegation(Expr $expr, Return_ $return, array $ifNegations) : array
|
||||
{
|
||||
while ($expr instanceof BooleanAnd) {
|
||||
$ifNegations = \array_merge($ifNegations, $this->collectLeftBooleanAndToIfs($expr, $return, $ifNegations));
|
||||
$ifNegations[] = $this->ifManipulator->createIfNegation($expr->right, new Return_($this->nodeFactory->createFalse()));
|
||||
$expr = $expr->right;
|
||||
}
|
||||
return $ifNegations + [$this->ifManipulator->createIfNegation($expr, new Return_($this->nodeFactory->createFalse()))];
|
||||
}
|
||||
/**
|
||||
* @param If_[] $ifNegations
|
||||
* @return If_[]
|
||||
*/
|
||||
private function collectLeftBooleanAndToIfs(BooleanAnd $booleanAnd, Return_ $return, array $ifNegations) : array
|
||||
{
|
||||
$left = $booleanAnd->left;
|
||||
if (!$left instanceof BooleanAnd) {
|
||||
return [$this->ifManipulator->createIfNegation($left, new Return_($this->nodeFactory->createFalse()))];
|
||||
}
|
||||
return $this->createMultipleIfsNegation($left, $return, $ifNegations);
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '59b4c8d69f788a36184b69adf06b03e2ab2e8989';
|
||||
public const PACKAGE_VERSION = 'db16e33a9a72d6c75600187d9f7851060d9a88fe';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-07-11 17:32:32';
|
||||
public const RELEASE_DATE = '2023-07-11 15:47:43';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -16,7 +16,6 @@ use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
||||
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
|
||||
final class IfManipulator
|
||||
{
|
||||
/**
|
||||
@ -34,22 +33,16 @@ final class IfManipulator
|
||||
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
||||
*/
|
||||
private $valueResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\EarlyReturn\NodeTransformer\ConditionInverter
|
||||
*/
|
||||
private $conditionInverter;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
||||
*/
|
||||
private $nodeComparator;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, \Rector\Core\NodeManipulator\StmtsManipulator $stmtsManipulator, ValueResolver $valueResolver, ConditionInverter $conditionInverter, NodeComparator $nodeComparator)
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, \Rector\Core\NodeManipulator\StmtsManipulator $stmtsManipulator, ValueResolver $valueResolver, NodeComparator $nodeComparator)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->stmtsManipulator = $stmtsManipulator;
|
||||
$this->valueResolver = $valueResolver;
|
||||
$this->conditionInverter = $conditionInverter;
|
||||
$this->nodeComparator = $nodeComparator;
|
||||
}
|
||||
/**
|
||||
@ -175,11 +168,6 @@ final class IfManipulator
|
||||
}
|
||||
return $if->elseifs === [];
|
||||
}
|
||||
public function createIfNegation(Expr $expr, Return_ $return) : If_
|
||||
{
|
||||
$expr = $this->conditionInverter->createInvertedCondition($expr);
|
||||
return new If_($expr, ['stmts' => [$return]]);
|
||||
}
|
||||
/**
|
||||
* @deprecated Create If_ directly, this is simple new with no added value
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitbdbcb98a5c11cb94a8c3c305b51a35c3::getLoader();
|
||||
return ComposerAutoloaderInit8a543d47f349b747b51539c55a747fdb::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -1902,7 +1902,6 @@ return array(
|
||||
'Rector\\EarlyReturn\\Rector\\If_\\ChangeOrIfContinueToMultiContinueRector' => $baseDir . '/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.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',
|
||||
'Rector\\EarlyReturn\\Rector\\Return_\\ReturnBinaryOrToEarlyReturnRector' => $baseDir . '/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php',
|
||||
'Rector\\EarlyReturn\\Rector\\StmtsAwareInterface\\ReturnEarlyIfVariableRector' => $baseDir . '/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php',
|
||||
'Rector\\EarlyReturn\\ValueObject\\BareSingleAssignIf' => $baseDir . '/rules/EarlyReturn/ValueObject/BareSingleAssignIf.php',
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitbdbcb98a5c11cb94a8c3c305b51a35c3
|
||||
class ComposerAutoloaderInit8a543d47f349b747b51539c55a747fdb
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInitbdbcb98a5c11cb94a8c3c305b51a35c3
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitbdbcb98a5c11cb94a8c3c305b51a35c3', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit8a543d47f349b747b51539c55a747fdb', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitbdbcb98a5c11cb94a8c3c305b51a35c3', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit8a543d47f349b747b51539c55a747fdb', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit8a543d47f349b747b51539c55a747fdb::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit8a543d47f349b747b51539c55a747fdb::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3
|
||||
class ComposerStaticInit8a543d47f349b747b51539c55a747fdb
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -2154,7 +2154,6 @@ class ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3
|
||||
'Rector\\EarlyReturn\\Rector\\If_\\ChangeOrIfContinueToMultiContinueRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/If_/ChangeOrIfContinueToMultiContinueRector.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',
|
||||
'Rector\\EarlyReturn\\Rector\\Return_\\ReturnBinaryOrToEarlyReturnRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/Return_/ReturnBinaryOrToEarlyReturnRector.php',
|
||||
'Rector\\EarlyReturn\\Rector\\StmtsAwareInterface\\ReturnEarlyIfVariableRector' => __DIR__ . '/../..' . '/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php',
|
||||
'Rector\\EarlyReturn\\ValueObject\\BareSingleAssignIf' => __DIR__ . '/../..' . '/rules/EarlyReturn/ValueObject/BareSingleAssignIf.php',
|
||||
@ -3067,9 +3066,9 @@ class ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitbdbcb98a5c11cb94a8c3c305b51a35c3::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit8a543d47f349b747b51539c55a747fdb::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit8a543d47f349b747b51539c55a747fdb::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit8a543d47f349b747b51539c55a747fdb::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user