Updated Rector to commit 85a407a1b75939448a0ee6d539ebe86538252e52

85a407a1b7 Deprecate BinarySwitchToIfElseRector as opinionated to allow more suitable match() refactoring (#5064)
This commit is contained in:
Tomas Votruba 2023-09-22 09:56:10 +00:00
parent f6beffcb30
commit 4f42274ab7
10 changed files with 46 additions and 94 deletions

View File

@ -28,7 +28,6 @@ use Rector\CodingStyle\Rector\Property\SplitGroupedPropertiesRector;
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector;
use Rector\CodingStyle\Rector\Ternary\TernaryConditionVariableAssignmentRector;
use Rector\CodingStyle\Rector\Use_\SeparateMultiUseImportsRector;
use Rector\Config\RectorConfig;
@ -37,5 +36,5 @@ use Rector\Transform\Rector\FuncCall\FuncCallToConstFetchRector;
use Rector\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->ruleWithConfiguration(FuncCallToConstFetchRector::class, ['php_sapi_name' => 'PHP_SAPI', 'pi' => 'M_PI']);
$rectorConfig->rules([SeparateMultiUseImportsRector::class, PostIncDecToPreIncDecRector::class, NewlineAfterStatementRector::class, RemoveFinalFromConstRector::class, NullableCompareToNullRector::class, BinarySwitchToIfElseRector::class, ConsistentImplodeRector::class, TernaryConditionVariableAssignmentRector::class, SymplifyQuoteEscapeRector::class, StringClassNameToClassConstantRector::class, CatchExceptionNameMatchingTypeRector::class, UseIncrementAssignRector::class, SplitDoubleAssignRector::class, EncapsedStringsToSprintfRector::class, WrapEncapsedVariableInCurlyBracesRector::class, NewlineBeforeNewAssignSetRector::class, AddArrayDefaultToArrayPropertyRector::class, MakeInheritedMethodVisibilitySameAsParentRector::class, CallUserFuncArrayToVariadicRector::class, VersionCompareFuncCallToConstantRector::class, StaticArrowFunctionRector::class, StaticClosureRector::class, CountArrayToEmptyArrayComparisonRector::class, CallUserFuncToMethodCallRector::class, FuncGetArgsToVariadicParamRector::class, StrictArraySearchRector::class, UseClassKeywordForClassNameResolutionRector::class, SplitGroupedPropertiesRector::class, SplitGroupedClassConstantsRector::class, ExplicitPublicClassMethodRector::class]);
$rectorConfig->rules([SeparateMultiUseImportsRector::class, PostIncDecToPreIncDecRector::class, NewlineAfterStatementRector::class, RemoveFinalFromConstRector::class, NullableCompareToNullRector::class, ConsistentImplodeRector::class, TernaryConditionVariableAssignmentRector::class, SymplifyQuoteEscapeRector::class, StringClassNameToClassConstantRector::class, CatchExceptionNameMatchingTypeRector::class, UseIncrementAssignRector::class, SplitDoubleAssignRector::class, EncapsedStringsToSprintfRector::class, WrapEncapsedVariableInCurlyBracesRector::class, NewlineBeforeNewAssignSetRector::class, AddArrayDefaultToArrayPropertyRector::class, MakeInheritedMethodVisibilitySameAsParentRector::class, CallUserFuncArrayToVariadicRector::class, VersionCompareFuncCallToConstantRector::class, StaticArrowFunctionRector::class, StaticClosureRector::class, CountArrayToEmptyArrayComparisonRector::class, CallUserFuncToMethodCallRector::class, FuncGetArgsToVariadicParamRector::class, StrictArraySearchRector::class, UseClassKeywordForClassNameResolutionRector::class, SplitGroupedPropertiesRector::class, SplitGroupedClassConstantsRector::class, ExplicitPublicClassMethodRector::class]);
};

View File

@ -4,39 +4,15 @@ declare (strict_types=1);
namespace Rector\CodingStyle\Rector\Switch_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use Rector\Core\NodeAnalyzer\ExprAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Renaming\NodeManipulator\SwitchManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector\BinarySwitchToIfElseRectorTest
* @deprecated This rule is deprecated as prevents refactoring to match (), use PHPStan, manual handling and PHP 8.0 upgrade set instead
*/
final class BinarySwitchToIfElseRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Renaming\NodeManipulator\SwitchManipulator
*/
private $switchManipulator;
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ExprAnalyzer
*/
private $exprAnalyzer;
public function __construct(SwitchManipulator $switchManipulator, ExprAnalyzer $exprAnalyzer)
{
$this->switchManipulator = $switchManipulator;
$this->exprAnalyzer = $exprAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Changes switch with 2 options to if-else', [new CodeSample(<<<'CODE_SAMPLE'
@ -70,42 +46,7 @@ CODE_SAMPLE
*/
public function refactor(Node $node) : ?Node
{
if ($node->cases === [] || \count($node->cases) > 2) {
return null;
}
// avoid removal of cases if it goes to be skipped next
$cases = $node->cases;
/** @var Case_ $firstCase */
$firstCase = \array_shift($cases);
if (!$firstCase->cond instanceof Expr) {
return null;
}
if ($this->exprAnalyzer->isDynamicExpr($firstCase->cond)) {
return null;
}
$secondCase = \array_shift($cases);
// special case with empty first case → ||
$isFirstCaseEmpty = $firstCase->stmts === [];
if ($isFirstCaseEmpty && $secondCase instanceof Case_ && $secondCase->cond instanceof Expr) {
$else = new BooleanOr(new Equal($node->cond, $firstCase->cond), new Equal($node->cond, $secondCase->cond));
$ifNode = new If_($else);
$ifNode->stmts = $this->switchManipulator->removeBreakNodes($secondCase->stmts);
return $ifNode;
}
$ifNode = new If_(new Equal($node->cond, $firstCase->cond));
$ifNode->stmts = $this->switchManipulator->removeBreakNodes($firstCase->stmts);
// just one condition
if (!$secondCase instanceof Case_) {
return $ifNode;
}
if ($secondCase->cond instanceof Expr) {
// has condition
$equal = new Equal($node->cond, $secondCase->cond);
$ifNode->elseifs[] = new ElseIf_($equal, $this->switchManipulator->removeBreakNodes($secondCase->stmts));
} else {
// defaults
$ifNode->else = new Else_($this->switchManipulator->removeBreakNodes($secondCase->stmts));
}
return $ifNode;
\trigger_error(\sprintf('The "%s" rule is deprecated as prevents refactoring to match (), use PHPStan, manual handling and PHP 8.0 upgrade set instead.', self::class), \E_USER_ERROR);
return null;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '40d127ee553bda3f486546f721a72698830d5b89';
public const PACKAGE_VERSION = '85a407a1b75939448a0ee6d539ebe86538252e52';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-09-22 02:38:19';
public const RELEASE_DATE = '2023-09-22 11:52:48';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit74647fac53a9d53ef3626b53a324f04b
class ComposerAutoloaderInit4550c0cd10b7e4bfde148cf529736b43
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit74647fac53a9d53ef3626b53a324f04b
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit74647fac53a9d53ef3626b53a324f04b', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit4550c0cd10b7e4bfde148cf529736b43', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit74647fac53a9d53ef3626b53a324f04b', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit4550c0cd10b7e4bfde148cf529736b43', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit74647fac53a9d53ef3626b53a324f04b::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit4550c0cd10b7e4bfde148cf529736b43::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit74647fac53a9d53ef3626b53a324f04b::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit4550c0cd10b7e4bfde148cf529736b43::$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 ComposerStaticInit74647fac53a9d53ef3626b53a324f04b
class ComposerStaticInit4550c0cd10b7e4bfde148cf529736b43
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2601,9 +2601,9 @@ class ComposerStaticInit74647fac53a9d53ef3626b53a324f04b
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit74647fac53a9d53ef3626b53a324f04b::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit74647fac53a9d53ef3626b53a324f04b::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit74647fac53a9d53ef3626b53a324f04b::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit4550c0cd10b7e4bfde148cf529736b43::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4550c0cd10b7e4bfde148cf529736b43::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4550c0cd10b7e4bfde148cf529736b43::$classMap;
}, null, ClassLoader::class);
}

View File

@ -1944,12 +1944,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
"reference": "09ddd39b611dd02321b4b79f5e175a819e04c670"
"reference": "2c4fedfdafa9932f93ca1196272f440cda1e64ce"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/09ddd39b611dd02321b4b79f5e175a819e04c670",
"reference": "09ddd39b611dd02321b4b79f5e175a819e04c670",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/2c4fedfdafa9932f93ca1196272f440cda1e64ce",
"reference": "2c4fedfdafa9932f93ca1196272f440cda1e64ce",
"shasum": ""
},
"require": {
@ -1982,7 +1982,7 @@
"tomasvotruba\/unused-public": "^0.2",
"tracy\/tracy": "^2.10"
},
"time": "2023-09-13T14:49:54+00:00",
"time": "2023-09-22T09:39:15+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main 4b25180'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 2b2a7cd'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main be3974a'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 09ddd39'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main 4b25180'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 2b2a7cd'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main be3974a'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 2c4fedf'));
private function __construct()
{
}

View File

@ -6,6 +6,7 @@ namespace Rector\Symfony\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
@ -81,10 +82,7 @@ CODE_SAMPLE
private function resolveCommandNameFromSetName(Class_ $class) : ?Expr
{
$configureClassMethod = $class->getMethod('configure');
if (!$configureClassMethod instanceof ClassMethod) {
return null;
}
if ($configureClassMethod->stmts === null) {
if (!$configureClassMethod instanceof ClassMethod || $configureClassMethod->stmts === null) {
return null;
}
foreach ($configureClassMethod->stmts as $key => $stmt) {
@ -95,18 +93,32 @@ CODE_SAMPLE
continue;
}
$methodCall = $stmt->expr;
if (!$this->isName($methodCall->name, 'setName')) {
continue;
if ($methodCall->var instanceof Variable) {
return $this->resolveFromNonFluentMethodCall($methodCall, $configureClassMethod, $key);
}
if (!$this->isObjectType($methodCall->var, new ObjectType('Symfony\\Component\\Console\\Command\\Command'))) {
continue;
}
$commandNameEpxr = $methodCall->getArgs()[0]->value;
unset($configureClassMethod->stmts[$key]);
return $commandNameEpxr;
$expr = null;
$this->traverseNodesWithCallable($stmt, function (Node $node) use(&$expr) {
if ($node instanceof MethodCall && $this->isName($node->name, 'setName')) {
$expr = $node->getArgs()[0]->value;
// remove nested call
return $node->var;
}
return null;
});
return $expr;
}
return null;
}
private function resolveFromNonFluentMethodCall(MethodCall $methodCall, ClassMethod $classMethod, int $key) : ?Expr
{
if (!$this->isName($methodCall->name, 'setName')) {
return null;
}
$expr = $methodCall->getArgs()[0]->value;
// cleanup fluent call
unset($classMethod->stmts[$key]);
return $expr;
}
private function createStaticProtectedPropertyWithDefault(string $name, Node $node) : Property
{
$property = new \PhpParser\Builder\Property($name);