mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-15 15:42:22 +00:00
Updated Rector to commit d8da061308902cf4ef7a30963d5c9f32b6b9ec02
d8da061308
Make SimplifyIfNullableReturnRector + ChangeNestedIfsToEarlyReturnRector work without next/prev (#3794)
This commit is contained in:
parent
b08682957f
commit
26bbf728d6
|
@ -17,6 +17,7 @@ use PHPStan\Type\ObjectType;
|
|||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\CodeQuality\TypeResolver\AssignVariableTypeResolver;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\NodeManipulator\IfManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
|
||||
|
@ -83,53 +84,75 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [If_::class];
|
||||
return [StmtsAwareInterface::class];
|
||||
}
|
||||
/**
|
||||
* @param If_ $node
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($this->shouldSkip($node)) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
/** @var BooleanNot|Instanceof_ $cond */
|
||||
$cond = $node->cond;
|
||||
/** @var Instanceof_ $instanceof */
|
||||
$instanceof = $cond instanceof BooleanNot ? $cond->expr : $cond;
|
||||
$variable = $instanceof->expr;
|
||||
$class = $instanceof->class;
|
||||
if (!$class instanceof Name) {
|
||||
return null;
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if (!$stmt instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
$previousStmt = $node->stmts[$key - 1] ?? null;
|
||||
if (!$previousStmt instanceof If_) {
|
||||
continue;
|
||||
}
|
||||
$if = $previousStmt;
|
||||
if ($this->shouldSkip($if)) {
|
||||
continue;
|
||||
}
|
||||
/** @var BooleanNot|Instanceof_ $cond */
|
||||
$cond = $if->cond;
|
||||
/** @var Instanceof_ $instanceof */
|
||||
$instanceof = $cond instanceof BooleanNot ? $cond->expr : $cond;
|
||||
// @todo allow property as well
|
||||
$variable = $instanceof->expr;
|
||||
$class = $instanceof->class;
|
||||
if (!$class instanceof Name) {
|
||||
continue;
|
||||
}
|
||||
/** @var Return_ $returnIfStmt */
|
||||
$returnIfStmt = $if->stmts[0];
|
||||
if ($this->isIfStmtReturnIncorrect($cond, $variable, $returnIfStmt)) {
|
||||
continue;
|
||||
}
|
||||
$previousPreviousStmt = $node->stmts[$key - 2] ?? null;
|
||||
if (!$previousPreviousStmt instanceof Expression) {
|
||||
continue;
|
||||
}
|
||||
if (!$previousPreviousStmt->expr instanceof Assign) {
|
||||
continue;
|
||||
}
|
||||
$previousPreviousAssign = $previousPreviousStmt->expr;
|
||||
if (!$this->nodeComparator->areNodesEqual($previousPreviousAssign->var, $variable)) {
|
||||
continue;
|
||||
}
|
||||
if ($this->isNextReturnIncorrect($cond, $variable, $stmt)) {
|
||||
continue;
|
||||
}
|
||||
$variableType = $this->assignVariableTypeResolver->resolve($previousPreviousAssign);
|
||||
if (!$variableType instanceof UnionType) {
|
||||
continue;
|
||||
}
|
||||
$className = $class->toString();
|
||||
$types = $variableType->getTypes();
|
||||
$directReturn = $this->processSimplifyNullableReturn($variableType, $types, $className, $previousPreviousStmt, $previousPreviousAssign->expr);
|
||||
if (!$directReturn instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
// unset previous assign
|
||||
unset($node->stmts[$key - 2]);
|
||||
// unset previous if
|
||||
unset($node->stmts[$key - 1]);
|
||||
$node->stmts[$key] = $directReturn;
|
||||
return $node;
|
||||
}
|
||||
/** @var Return_ $returnIfStmt */
|
||||
$returnIfStmt = $node->stmts[0];
|
||||
if ($this->isIfStmtReturnIncorrect($cond, $variable, $returnIfStmt)) {
|
||||
return null;
|
||||
}
|
||||
$previous = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
|
||||
if (!$previous instanceof Expression) {
|
||||
return null;
|
||||
}
|
||||
$previousAssign = $previous->expr;
|
||||
if (!$previousAssign instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->nodeComparator->areNodesEqual($previousAssign->var, $variable)) {
|
||||
return null;
|
||||
}
|
||||
/** @var Return_ $nextNode */
|
||||
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if ($this->isNextReturnIncorrect($cond, $variable, $nextNode)) {
|
||||
return null;
|
||||
}
|
||||
$variableType = $this->assignVariableTypeResolver->resolve($previousAssign);
|
||||
if (!$variableType instanceof UnionType) {
|
||||
return null;
|
||||
}
|
||||
$className = $class->toString();
|
||||
$types = $variableType->getTypes();
|
||||
return $this->processSimplifyNullableReturn($variableType, $types, $className, $nextNode, $previous, $previousAssign->expr);
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\BooleanNot|\PhpParser\Node\Expr\Instanceof_ $expr
|
||||
|
@ -160,21 +183,21 @@ CODE_SAMPLE
|
|||
/**
|
||||
* @param Type[] $types
|
||||
*/
|
||||
private function processSimplifyNullableReturn(UnionType $unionType, array $types, string $className, Return_ $return, Expression $expression, Expr $expr) : ?Return_
|
||||
private function processSimplifyNullableReturn(UnionType $unionType, array $types, string $className, Expression $expression, Expr $expr) : ?Return_
|
||||
{
|
||||
if (\count($types) > 2) {
|
||||
return null;
|
||||
}
|
||||
if ($types[0] instanceof FullyQualifiedObjectType && $types[1] instanceof NullType && $className === $types[0]->getClassName()) {
|
||||
return $this->removeAndReturn($return, $expression, $expr, $unionType);
|
||||
return $this->createDirectReturn($expression, $expr, $unionType);
|
||||
}
|
||||
if ($types[0] instanceof NullType && $types[1] instanceof FullyQualifiedObjectType && $className === $types[1]->getClassName()) {
|
||||
return $this->removeAndReturn($return, $expression, $expr, $unionType);
|
||||
return $this->createDirectReturn($expression, $expr, $unionType);
|
||||
}
|
||||
if ($this->isNotTypedNullable($types, $className)) {
|
||||
return null;
|
||||
}
|
||||
return $this->removeAndReturn($return, $expression, $expr, $unionType);
|
||||
return $this->createDirectReturn($expression, $expr, $unionType);
|
||||
}
|
||||
/**
|
||||
* @param Type[] $types
|
||||
|
@ -189,10 +212,8 @@ CODE_SAMPLE
|
|||
}
|
||||
return $className !== $types[0]->getClassName();
|
||||
}
|
||||
private function removeAndReturn(Return_ $return, Expression $expression, Expr $expr, UnionType $unionType) : Return_
|
||||
private function createDirectReturn(Expression $expression, Expr $expr, UnionType $unionType) : Return_
|
||||
{
|
||||
$this->removeNode($return);
|
||||
$this->removeNode($expression);
|
||||
$exprReturn = new Return_($expr);
|
||||
$this->varTagRemover->removeVarPhpTagValueNodeIfNotComment($expression, $unionType);
|
||||
$this->mirrorComments($exprReturn, $expression);
|
||||
|
|
|
@ -6,7 +6,6 @@ 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\If_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
|
@ -81,37 +80,33 @@ CODE_SAMPLE
|
|||
/**
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
public function refactor(Node $node) : ?StmtsAwareInterface
|
||||
{
|
||||
$stmts = $node->stmts;
|
||||
if ($stmts === null) {
|
||||
if ($node->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
/** @var Stmt[] $previousStmts[] */
|
||||
$previousStmts = [];
|
||||
foreach ($stmts as $key => $stmt) {
|
||||
$nextStmt = $stmts[$key + 1] ?? null;
|
||||
if (!$nextStmt instanceof Return_) {
|
||||
if ($nextStmt instanceof Stmt) {
|
||||
$previousStmts[] = $stmt;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if (!$stmt instanceof If_) {
|
||||
continue;
|
||||
}
|
||||
$nextStmt = $node->stmts[$key + 1] ?? null;
|
||||
if (!$nextStmt instanceof Return_) {
|
||||
return null;
|
||||
}
|
||||
$nestedIfsWithOnlyReturn = $this->ifManipulator->collectNestedIfsWithOnlyReturn($stmt);
|
||||
if ($nestedIfsWithOnlyReturn === []) {
|
||||
continue;
|
||||
}
|
||||
$node->stmts = \array_merge($previousStmts, $this->processNestedIfsWithOnlyReturn($nestedIfsWithOnlyReturn, $nextStmt));
|
||||
$newStmts = $this->processNestedIfsWithOnlyReturn($nestedIfsWithOnlyReturn, $nextStmt);
|
||||
// replace nested ifs with many separate ifs
|
||||
\array_splice($node->stmts, $key, 1, $newStmts);
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param If_[] $nestedIfsWithOnlyReturn
|
||||
* @return Stmt[]
|
||||
* @return If_[]
|
||||
*/
|
||||
private function processNestedIfsWithOnlyReturn(array $nestedIfsWithOnlyReturn, Return_ $nextReturn) : array
|
||||
{
|
||||
|
@ -128,11 +123,11 @@ CODE_SAMPLE
|
|||
$newStmts = \array_merge($newStmts, $standaloneIfs);
|
||||
}
|
||||
}
|
||||
$newStmts[] = $nextReturn;
|
||||
// $newStmts[] = $nextReturn;
|
||||
return $newStmts;
|
||||
}
|
||||
/**
|
||||
* @return Stmt[]
|
||||
* @return If_[]
|
||||
*/
|
||||
private function createStandaloneIfsWithReturn(If_ $nestedIfWithOnlyReturn, Return_ $return) : array
|
||||
{
|
||||
|
|
|
@ -90,7 +90,7 @@ CODE_SAMPLE
|
|||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
{
|
||||
if ($node instanceof MethodCall) {
|
||||
return $this->refactorMethodCall($node, $scope);
|
||||
return $this->refactorMethodCall($node);
|
||||
}
|
||||
if ($node->expr instanceof MethodCall) {
|
||||
return $this->refactorIfHasReturnTypeWasCalled($node->expr);
|
||||
|
@ -123,7 +123,7 @@ CODE_SAMPLE
|
|||
}
|
||||
return \false;
|
||||
}
|
||||
private function refactorMethodCall(MethodCall $methodCall, Scope $scope) : ?Node
|
||||
private function refactorMethodCall(MethodCall $methodCall) : ?Node
|
||||
{
|
||||
$this->collectCallByVariable($methodCall);
|
||||
if ($this->shouldSkipMethodCall($methodCall)) {
|
||||
|
|
|
@ -7,7 +7,6 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Strict\NodeFactory\ExactCompareFactory;
|
||||
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
|
|
|
@ -11,10 +11,8 @@ use PhpParser\Node\Stmt\ClassMethod;
|
|||
use PHPStan\Analyser\Scope;
|
||||
use Rector\Core\Enum\ObjectReference;
|
||||
use Rector\Core\NodeManipulator\Dependency\DependencyClassMethodDecorator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Rector\AbstractScopeAwareRector;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
|
|
|
@ -9,7 +9,6 @@ use PhpParser\Node\Expr\ArrayDimFetch;
|
|||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\Empty_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Strict\NodeFactory\ExactCompareFactory;
|
||||
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
|
|
|
@ -7,7 +7,6 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Strict\NodeFactory\ExactCompareFactory;
|
||||
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
|
|
|
@ -7,7 +7,6 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Strict\NodeFactory\ExactCompareFactory;
|
||||
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
|
|
|
@ -8,7 +8,6 @@ use PhpParser\Node\Expr;
|
|||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\Strict\NodeFactory\ExactCompareFactory;
|
||||
use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '0e2b39c2841b845417775936177d2425edac09d3';
|
||||
public const PACKAGE_VERSION = 'd8da061308902cf4ef7a30963d5c9f32b6b9ec02';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-11 09:19:44';
|
||||
public const RELEASE_DATE = '2023-05-11 11:47:32';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -156,9 +156,9 @@ final class IfManipulator
|
|||
return $ifs;
|
||||
}
|
||||
/**
|
||||
* @param class-string<Node> $className
|
||||
* @param class-string<Stmt> $stmtClass
|
||||
*/
|
||||
public function isIfWithOnly(Node $node, string $className) : bool
|
||||
public function isIfWithOnly(Node $node, string $stmtClass) : bool
|
||||
{
|
||||
if (!$node instanceof If_) {
|
||||
return \false;
|
||||
|
@ -166,7 +166,7 @@ final class IfManipulator
|
|||
if (!$this->isIfWithoutElseAndElseIfs($node)) {
|
||||
return \false;
|
||||
}
|
||||
return $this->hasOnlyStmtOfType($node, $className);
|
||||
return $this->hasOnlyStmtOfType($node, $stmtClass);
|
||||
}
|
||||
public function isIfWithOnlyOneStmt(If_ $if) : bool
|
||||
{
|
||||
|
@ -209,14 +209,14 @@ final class IfManipulator
|
|||
return $this->hasOnlyStmtOfType($if, If_::class);
|
||||
}
|
||||
/**
|
||||
* @param class-string<Node> $desiredType
|
||||
* @param class-string<Stmt> $stmtClass
|
||||
*/
|
||||
private function hasOnlyStmtOfType(If_ $if, string $desiredType) : bool
|
||||
private function hasOnlyStmtOfType(If_ $if, string $stmtClass) : bool
|
||||
{
|
||||
$stmts = $if->stmts;
|
||||
if (\count($stmts) !== 1) {
|
||||
return \false;
|
||||
}
|
||||
return $stmts[0] instanceof $desiredType;
|
||||
return $stmts[0] instanceof $stmtClass;
|
||||
}
|
||||
}
|
||||
|
|
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 ComposerAutoloaderInitb7e001b200edb4eb0243b1f21161d4f3::getLoader();
|
||||
return ComposerAutoloaderInitf101285877d34f44e2f384904011ebf5::getLoader();
|
||||
|
|
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 ComposerAutoloaderInitb7e001b200edb4eb0243b1f21161d4f3
|
||||
class ComposerAutoloaderInitf101285877d34f44e2f384904011ebf5
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInitb7e001b200edb4eb0243b1f21161d4f3
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb7e001b200edb4eb0243b1f21161d4f3', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitf101285877d34f44e2f384904011ebf5', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb7e001b200edb4eb0243b1f21161d4f3', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitf101285877d34f44e2f384904011ebf5', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitf101285877d34f44e2f384904011ebf5::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitf101285877d34f44e2f384904011ebf5::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3
|
||||
class ComposerStaticInitf101285877d34f44e2f384904011ebf5
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3110,9 +3110,9 @@ class ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb7e001b200edb4eb0243b1f21161d4f3::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitf101285877d34f44e2f384904011ebf5::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitf101285877d34f44e2f384904011ebf5::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitf101285877d34f44e2f384904011ebf5::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user