mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-13 22:52:23 +00:00
Updated Rector to commit 0ecf88aa469af79d7965bfc5efe57888d56cf88e
0ecf88aa46
[PHP 8.0] Keep previously defined default, it might have been used later outside the switch construction (#2807)
This commit is contained in:
parent
25a78da614
commit
3d2ac73661
|
@ -40,6 +40,18 @@ final class MatchSwitchAnalyzer
|
|||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->nodeComparator = $nodeComparator;
|
||||
}
|
||||
/**
|
||||
* @param CondAndExpr[] $condAndExprs
|
||||
*/
|
||||
public function isReturnCondsAndExprs(array $condAndExprs) : bool
|
||||
{
|
||||
foreach ($condAndExprs as $condAndExpr) {
|
||||
if ($condAndExpr->equalsMatchKind(MatchKind::RETURN)) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
/**
|
||||
* @param CondAndExpr[] $condAndExprs
|
||||
*/
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\NodeManipulator;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Match_;
|
||||
use PhpParser\Node\MatchArm;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Php80\ValueObject\MatchAssignResult;
|
||||
final class AssignMatchTransformer
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
||||
*/
|
||||
private $nodeComparator;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, NodeComparator $nodeComparator)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->nodeComparator = $nodeComparator;
|
||||
}
|
||||
public function unwrapMatchArmAssignsToAssign(Match_ $match, Expr $expr, bool $hasDefaultValue, ?Stmt $previousStmt, ?Stmt $nextStmt) : ?MatchAssignResult
|
||||
{
|
||||
// containts next this expr?
|
||||
if (!$hasDefaultValue && $this->isFollowedByReturnWithExprUsage($nextStmt, $expr)) {
|
||||
return null;
|
||||
}
|
||||
$prevInitializedAssign = $this->resolvePreviousInitializedAssign($previousStmt, $expr);
|
||||
$assign = new Assign($expr, $match);
|
||||
if (!$prevInitializedAssign instanceof Assign) {
|
||||
$currentAssign = $this->resolveCurrentAssign($hasDefaultValue, $assign);
|
||||
if ($currentAssign instanceof Assign) {
|
||||
return new MatchAssignResult($currentAssign, \false);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
$matchArmCount = \count($match->arms);
|
||||
if ($hasDefaultValue) {
|
||||
$default = $match->arms[$matchArmCount - 1]->body;
|
||||
if ($this->nodeComparator->areNodesEqual($default, $prevInitializedAssign->var)) {
|
||||
return new MatchAssignResult($assign, \false);
|
||||
}
|
||||
} else {
|
||||
$match->arms[$matchArmCount] = new MatchArm(null, $prevInitializedAssign->expr);
|
||||
}
|
||||
return new MatchAssignResult($assign, \true);
|
||||
}
|
||||
private function resolveCurrentAssign(bool $hasDefaultValue, Assign $assign) : ?Assign
|
||||
{
|
||||
return $hasDefaultValue ? $assign : null;
|
||||
}
|
||||
private function isFollowedByReturnWithExprUsage(?\PhpParser\Node\Stmt $nextStmt, Expr $expr) : bool
|
||||
{
|
||||
if (!$nextStmt instanceof Return_) {
|
||||
return \false;
|
||||
}
|
||||
if (!$nextStmt->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
$returnExprs = $this->betterNodeFinder->findInstanceOf($nextStmt, Expr::class);
|
||||
foreach ($returnExprs as $returnExpr) {
|
||||
if ($this->nodeComparator->areNodesEqual($expr, $returnExpr)) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function resolvePreviousInitializedAssign(?Stmt $previousStmt, Expr $expr) : ?Assign
|
||||
{
|
||||
if (!$previousStmt instanceof Expression) {
|
||||
return null;
|
||||
}
|
||||
$previousExpr = $previousStmt->expr;
|
||||
if (!$previousExpr instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
// is that assign to our variable?
|
||||
if (!$this->nodeComparator->areNodesEqual($previousExpr->var, $expr)) {
|
||||
return null;
|
||||
}
|
||||
return $previousExpr;
|
||||
}
|
||||
}
|
|
@ -6,20 +6,16 @@ namespace Rector\Php80\Rector\Switch_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Throw_;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\Node\Stmt\Switch_;
|
||||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\Php80\Enum\MatchKind;
|
||||
use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer;
|
||||
use Rector\Php80\NodeFactory\MatchFactory;
|
||||
use Rector\Php80\NodeManipulator\AssignMatchTransformer;
|
||||
use Rector\Php80\NodeResolver\SwitchExprsResolver;
|
||||
use Rector\Php80\ValueObject\CondAndExpr;
|
||||
use Rector\Php80\ValueObject\MatchAssignResult;
|
||||
use Rector\Php80\ValueObject\MatchResult;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -47,17 +43,11 @@ final class ChangeSwitchToMatchRector extends AbstractRector implements MinPhpVe
|
|||
* @var \Rector\Php80\NodeFactory\MatchFactory
|
||||
*/
|
||||
private $matchFactory;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php80\NodeManipulator\AssignMatchTransformer
|
||||
*/
|
||||
private $assignMatchTransformer;
|
||||
public function __construct(SwitchExprsResolver $switchExprsResolver, MatchSwitchAnalyzer $matchSwitchAnalyzer, MatchFactory $matchFactory, AssignMatchTransformer $assignMatchTransformer)
|
||||
public function __construct(SwitchExprsResolver $switchExprsResolver, MatchSwitchAnalyzer $matchSwitchAnalyzer, MatchFactory $matchFactory)
|
||||
{
|
||||
$this->switchExprsResolver = $switchExprsResolver;
|
||||
$this->matchSwitchAnalyzer = $matchSwitchAnalyzer;
|
||||
$this->matchFactory = $matchFactory;
|
||||
$this->assignMatchTransformer = $assignMatchTransformer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -110,20 +100,7 @@ CODE_SAMPLE
|
|||
if (!$this->matchSwitchAnalyzer->haveCondAndExprsMatchPotential($condAndExprs)) {
|
||||
continue;
|
||||
}
|
||||
$isReturn = \false;
|
||||
foreach ($condAndExprs as $condAndExpr) {
|
||||
if ($condAndExpr->equalsMatchKind(MatchKind::RETURN)) {
|
||||
$isReturn = \true;
|
||||
break;
|
||||
}
|
||||
$expr = $condAndExpr->getExpr();
|
||||
if ($expr instanceof Throw_) {
|
||||
continue;
|
||||
}
|
||||
if (!$expr instanceof Assign) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
$isReturn = $this->matchSwitchAnalyzer->isReturnCondsAndExprs($condAndExprs);
|
||||
$matchResult = $this->matchFactory->createFromCondAndExprs($stmt->cond, $condAndExprs, $nextStmt);
|
||||
if (!$matchResult instanceof MatchResult) {
|
||||
continue;
|
||||
|
@ -135,15 +112,10 @@ CODE_SAMPLE
|
|||
$assignVar = $this->resolveAssignVar($condAndExprs);
|
||||
$hasDefaultValue = $this->matchSwitchAnalyzer->hasDefaultValue($match);
|
||||
if ($assignVar instanceof Expr) {
|
||||
$previousStmt = $node->stmts[$key - 1] ?? null;
|
||||
$assignResult = $this->assignMatchTransformer->unwrapMatchArmAssignsToAssign($match, $assignVar, $hasDefaultValue, $previousStmt, $nextStmt);
|
||||
if (!$assignResult instanceof MatchAssignResult) {
|
||||
if (!$hasDefaultValue) {
|
||||
continue;
|
||||
}
|
||||
$assign = $assignResult->getAssign();
|
||||
if ($assignResult->isShouldRemovePreviousStmt()) {
|
||||
unset($node->stmts[$key - 1]);
|
||||
}
|
||||
$assign = new Assign($assignVar, $match);
|
||||
$node->stmts[$key] = new Expression($assign);
|
||||
$hasChanged = \true;
|
||||
continue;
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php80\ValueObject;
|
||||
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
final class MatchAssignResult
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PhpParser\Node\Expr\Assign
|
||||
*/
|
||||
private $assign;
|
||||
/**
|
||||
* @readonly
|
||||
* @var bool
|
||||
*/
|
||||
private $shouldRemovePrevoiusStmt;
|
||||
public function __construct(Assign $assign, bool $shouldRemovePrevoiusStmt)
|
||||
{
|
||||
$this->assign = $assign;
|
||||
$this->shouldRemovePrevoiusStmt = $shouldRemovePrevoiusStmt;
|
||||
}
|
||||
public function getAssign() : Assign
|
||||
{
|
||||
return $this->assign;
|
||||
}
|
||||
public function isShouldRemovePreviousStmt() : bool
|
||||
{
|
||||
return $this->shouldRemovePrevoiusStmt;
|
||||
}
|
||||
}
|
|
@ -17,12 +17,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '6b5a537b01a23e5a45e2a7dcb205a2b69d1f62eb';
|
||||
public const PACKAGE_VERSION = '0ecf88aa469af79d7965bfc5efe57888d56cf88e';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-08-19 15:47:35';
|
||||
public const RELEASE_DATE = '2022-08-19 14:00:56';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit5f8e5a4b08a0268b87abee5b9efdf2b3::getLoader();
|
||||
return ComposerAutoloaderInit236ae775483cc63b8aa6d40765321a66::getLoader();
|
||||
|
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
|
@ -2369,7 +2369,6 @@ return array(
|
|||
'Rector\\Php80\\NodeFactory\\MatchFactory' => $baseDir . '/rules/Php80/NodeFactory/MatchFactory.php',
|
||||
'Rector\\Php80\\NodeFactory\\NestedAttrGroupsFactory' => $baseDir . '/rules/Php80/NodeFactory/NestedAttrGroupsFactory.php',
|
||||
'Rector\\Php80\\NodeFactory\\StrStartsWithFuncCallFactory' => $baseDir . '/rules/Php80/NodeFactory/StrStartsWithFuncCallFactory.php',
|
||||
'Rector\\Php80\\NodeManipulator\\AssignMatchTransformer' => $baseDir . '/rules/Php80/NodeManipulator/AssignMatchTransformer.php',
|
||||
'Rector\\Php80\\NodeManipulator\\AttributeGroupNamedArgumentManipulator' => $baseDir . '/rules/Php80/NodeManipulator/AttributeGroupNamedArgumentManipulator.php',
|
||||
'Rector\\Php80\\NodeManipulator\\ResourceReturnToObject' => $baseDir . '/rules/Php80/NodeManipulator/ResourceReturnToObject.php',
|
||||
'Rector\\Php80\\NodeManipulator\\TokenManipulator' => $baseDir . '/rules/Php80/NodeManipulator/TokenManipulator.php',
|
||||
|
@ -2401,7 +2400,6 @@ return array(
|
|||
'Rector\\Php80\\ValueObject\\ClassNameAndTagValueNode' => $baseDir . '/rules/Php80/ValueObject/ClassNameAndTagValueNode.php',
|
||||
'Rector\\Php80\\ValueObject\\CondAndExpr' => $baseDir . '/rules/Php80/ValueObject/CondAndExpr.php',
|
||||
'Rector\\Php80\\ValueObject\\DoctrineTagAndAnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/DoctrineTagAndAnnotationToAttribute.php',
|
||||
'Rector\\Php80\\ValueObject\\MatchAssignResult' => $baseDir . '/rules/Php80/ValueObject/MatchAssignResult.php',
|
||||
'Rector\\Php80\\ValueObject\\MatchResult' => $baseDir . '/rules/Php80/ValueObject/MatchResult.php',
|
||||
'Rector\\Php80\\ValueObject\\NestedAnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/NestedAnnotationToAttribute.php',
|
||||
'Rector\\Php80\\ValueObject\\NestedDoctrineTagAndAnnotationToAttribute' => $baseDir . '/rules/Php80/ValueObject/NestedDoctrineTagAndAnnotationToAttribute.php',
|
||||
|
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
||||
class ComposerAutoloaderInit236ae775483cc63b8aa6d40765321a66
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit5f8e5a4b08a0268b87abee5b9efdf2b3', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit236ae775483cc63b8aa6d40765321a66', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit5f8e5a4b08a0268b87abee5b9efdf2b3', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit236ae775483cc63b8aa6d40765321a66', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit236ae775483cc63b8aa6d40765321a66::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit236ae775483cc63b8aa6d40765321a66::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire5f8e5a4b08a0268b87abee5b9efdf2b3($fileIdentifier, $file);
|
||||
composerRequire236ae775483cc63b8aa6d40765321a66($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire5f8e5a4b08a0268b87abee5b9efdf2b3($fileIdentifier, $file)
|
||||
function composerRequire236ae775483cc63b8aa6d40765321a66($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
10
vendor/composer/autoload_static.php
vendored
10
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
||||
class ComposerStaticInit236ae775483cc63b8aa6d40765321a66
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
|
@ -2666,7 +2666,6 @@ class ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
|||
'Rector\\Php80\\NodeFactory\\MatchFactory' => __DIR__ . '/../..' . '/rules/Php80/NodeFactory/MatchFactory.php',
|
||||
'Rector\\Php80\\NodeFactory\\NestedAttrGroupsFactory' => __DIR__ . '/../..' . '/rules/Php80/NodeFactory/NestedAttrGroupsFactory.php',
|
||||
'Rector\\Php80\\NodeFactory\\StrStartsWithFuncCallFactory' => __DIR__ . '/../..' . '/rules/Php80/NodeFactory/StrStartsWithFuncCallFactory.php',
|
||||
'Rector\\Php80\\NodeManipulator\\AssignMatchTransformer' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/AssignMatchTransformer.php',
|
||||
'Rector\\Php80\\NodeManipulator\\AttributeGroupNamedArgumentManipulator' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/AttributeGroupNamedArgumentManipulator.php',
|
||||
'Rector\\Php80\\NodeManipulator\\ResourceReturnToObject' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/ResourceReturnToObject.php',
|
||||
'Rector\\Php80\\NodeManipulator\\TokenManipulator' => __DIR__ . '/../..' . '/rules/Php80/NodeManipulator/TokenManipulator.php',
|
||||
|
@ -2698,7 +2697,6 @@ class ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
|||
'Rector\\Php80\\ValueObject\\ClassNameAndTagValueNode' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/ClassNameAndTagValueNode.php',
|
||||
'Rector\\Php80\\ValueObject\\CondAndExpr' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/CondAndExpr.php',
|
||||
'Rector\\Php80\\ValueObject\\DoctrineTagAndAnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/DoctrineTagAndAnnotationToAttribute.php',
|
||||
'Rector\\Php80\\ValueObject\\MatchAssignResult' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/MatchAssignResult.php',
|
||||
'Rector\\Php80\\ValueObject\\MatchResult' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/MatchResult.php',
|
||||
'Rector\\Php80\\ValueObject\\NestedAnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/NestedAnnotationToAttribute.php',
|
||||
'Rector\\Php80\\ValueObject\\NestedDoctrineTagAndAnnotationToAttribute' => __DIR__ . '/../..' . '/rules/Php80/ValueObject/NestedDoctrineTagAndAnnotationToAttribute.php',
|
||||
|
@ -3256,9 +3254,9 @@ class ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit5f8e5a4b08a0268b87abee5b9efdf2b3::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit236ae775483cc63b8aa6d40765321a66::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit236ae775483cc63b8aa6d40765321a66::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit236ae775483cc63b8aa6d40765321a66::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user