Updated Rector to commit 9018492c75866aacc5a71329f921312d31772987

9018492c75 [CodeQuality] Remove SimplifyIfIssetToNullCoalescingRector, as overly complex and should be handled by manual context (#2828)
This commit is contained in:
Tomas Votruba 2022-08-24 08:28:31 +00:00
parent 1db8339e7c
commit ac412ec638
8 changed files with 17 additions and 212 deletions

View File

@ -58,7 +58,6 @@ use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfExactValueReturnValueRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfNotNullReturnRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfNullableReturnRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
@ -107,5 +106,5 @@ return static function (RectorConfig $rectorConfig) : void {
'mbstrrpos' => 'mb_strrpos',
'mbsubstr' => 'mb_substr',
]);
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SimplifyForeachToArrayFilterRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, SimplifyIfIssetToNullCoalescingRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, ForToForeachRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, RemoveAlwaysTrueConditionSetInConstructorRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, AddPregQuoteDelimiterRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, ArrayKeysAndInArrayToArrayKeyExistsRector::class, SplitListAssignToSeparateLineRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, NarrowUnionTypeDocRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, ExplicitMethodCallOverMagicGetSetRector::class, DoWhileBreakFalseToIfElseRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, SimplifyIfExactValueReturnValueRector::class]);
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SimplifyForeachToArrayFilterRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, ForToForeachRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, RemoveAlwaysTrueConditionSetInConstructorRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, AddPregQuoteDelimiterRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, ArrayKeysAndInArrayToArrayKeyExistsRector::class, SplitListAssignToSeparateLineRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, NarrowUnionTypeDocRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, ExplicitMethodCallOverMagicGetSetRector::class, DoWhileBreakFalseToIfElseRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, SimplifyIfExactValueReturnValueRector::class]);
};

View File

@ -1,4 +1,4 @@
# 411 Rules Overview
# 410 Rules Overview
<br>
@ -6,7 +6,7 @@
- [Arguments](#arguments) (5)
- [CodeQuality](#codequality) (73)
- [CodeQuality](#codequality) (72)
- [CodingStyle](#codingstyle) (36)
@ -1326,29 +1326,6 @@ Changes compared to value and return of expr to direct return
<br>
### SimplifyIfIssetToNullCoalescingRector
Simplify binary if to null coalesce
- class: [`Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector`](../rules/CodeQuality/Rector/If_/SimplifyIfIssetToNullCoalescingRector.php)
```diff
final class SomeController
{
public function run($possibleStatieYamlFile)
{
- if (isset($possibleStatieYamlFile['import'])) {
- $possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'], $filesToImport);
- } else {
- $possibleStatieYamlFile['import'] = $filesToImport;
- }
+ $possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'] ?? [], $filesToImport);
}
}
```
<br>
### SimplifyIfNotNullReturnRector
Changes redundant null check to instant return

View File

@ -1,169 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector\SimplifyIfIssetToNullCoalescingRectorTest
*/
final class SimplifyIfIssetToNullCoalescingRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
*/
private $argsAnalyzer;
public function __construct(ArgsAnalyzer $argsAnalyzer)
{
$this->argsAnalyzer = $argsAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Simplify binary if to null coalesce', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeController
{
public function run($possibleStatieYamlFile)
{
if (isset($possibleStatieYamlFile['import'])) {
$possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'], $filesToImport);
} else {
$possibleStatieYamlFile['import'] = $filesToImport;
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeController
{
public function run($possibleStatieYamlFile)
{
$possibleStatieYamlFile['import'] = array_merge($possibleStatieYamlFile['import'] ?? [], $filesToImport);
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
/** @var Isset_ $issetNode */
$issetNode = $node->cond;
$valueNode = $issetNode->vars[0];
// various scenarios
$ifFirstStmt = $node->stmts[0];
if (!$ifFirstStmt instanceof Expression) {
return null;
}
$else = $node->else;
if (!$else instanceof Else_) {
return null;
}
$elseFirstStmt = $else->stmts[0];
if (!$elseFirstStmt instanceof Expression) {
return null;
}
/** @var Assign $firstAssign */
$firstAssign = $ifFirstStmt->expr;
/** @var Assign $secondAssign */
$secondAssign = $elseFirstStmt->expr;
// 1. array_merge
if (!$firstAssign->expr instanceof FuncCall) {
return null;
}
if (!$this->isName($firstAssign->expr, 'array_merge')) {
return null;
}
if (!$this->argsAnalyzer->isArgsInstanceInArgsPositions($firstAssign->expr->args, [0, 1])) {
return null;
}
/** @var Arg $firstArg */
$firstArg = $firstAssign->expr->args[0];
if (!$this->nodeComparator->areNodesEqual($firstArg->value, $valueNode)) {
return null;
}
/** @var Arg $secondArg */
$secondArg = $firstAssign->expr->args[1];
if (!$this->nodeComparator->areNodesEqual($secondAssign->expr, $secondArg->value)) {
return null;
}
$args = [new Arg(new Coalesce($valueNode, new Array_([]))), new Arg($secondAssign->expr)];
$funcCall = new FuncCall(new Name('array_merge'), $args);
return new Assign($valueNode, $funcCall);
}
private function shouldSkip(If_ $if) : bool
{
if ($if->else === null) {
return \true;
}
if (\count($if->elseifs) > 1) {
return \true;
}
if (!$if->cond instanceof Isset_) {
return \true;
}
if (!$this->hasOnlyStatementAssign($if)) {
return \true;
}
if (!$this->hasOnlyStatementAssign($if->else)) {
return \true;
}
$ifStmt = $if->stmts[0];
if (!$ifStmt instanceof Expression) {
return \true;
}
if (!$ifStmt->expr instanceof Assign) {
return \true;
}
if (!$this->nodeComparator->areNodesEqual($if->cond->vars[0], $ifStmt->expr->var)) {
return \true;
}
$firstElseStmt = $if->else->stmts[0];
if (!$firstElseStmt instanceof Expression) {
return \false;
}
if (!$firstElseStmt->expr instanceof Assign) {
return \false;
}
return !$this->nodeComparator->areNodesEqual($if->cond->vars[0], $firstElseStmt->expr->var);
}
/**
* @param \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\Else_ $node
*/
private function hasOnlyStatementAssign($node) : bool
{
if (\count($node->stmts) !== 1) {
return \false;
}
if (!$node->stmts[0] instanceof Expression) {
return \false;
}
return $node->stmts[0]->expr instanceof Assign;
}
}

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '6306f9e8ecd8e367e7220b02a6613461a34dcc8a';
public const PACKAGE_VERSION = '9018492c75866aacc5a71329f921312d31772987';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-08-24 08:07:35';
public const RELEASE_DATE = '2022-08-24 08:24:13';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -1355,7 +1355,6 @@ return array(
'Rector\\CodeQuality\\Rector\\If_\\ShortenElseIfRector' => $baseDir . '/rules/CodeQuality/Rector/If_/ShortenElseIfRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfElseToTernaryRector' => $baseDir . '/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfExactValueReturnValueRector' => $baseDir . '/rules/CodeQuality/Rector/If_/SimplifyIfExactValueReturnValueRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfIssetToNullCoalescingRector' => $baseDir . '/rules/CodeQuality/Rector/If_/SimplifyIfIssetToNullCoalescingRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfNotNullReturnRector' => $baseDir . '/rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfNullableReturnRector' => $baseDir . '/rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfReturnBoolRector' => $baseDir . '/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit83af4ed37a67c995d5c32428732bd8ba
class ComposerAutoloaderInit25f8cae84e9b6b1c64dbee5f269829a2
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit83af4ed37a67c995d5c32428732bd8ba
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit83af4ed37a67c995d5c32428732bd8ba', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit25f8cae84e9b6b1c64dbee5f269829a2', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit83af4ed37a67c995d5c32428732bd8ba', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit25f8cae84e9b6b1c64dbee5f269829a2', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit25f8cae84e9b6b1c64dbee5f269829a2::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInit25f8cae84e9b6b1c64dbee5f269829a2::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire83af4ed37a67c995d5c32428732bd8ba($fileIdentifier, $file);
composerRequire25f8cae84e9b6b1c64dbee5f269829a2($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit83af4ed37a67c995d5c32428732bd8ba
* @param string $file
* @return void
*/
function composerRequire83af4ed37a67c995d5c32428732bd8ba($fileIdentifier, $file)
function composerRequire25f8cae84e9b6b1c64dbee5f269829a2($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 ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba
class ComposerStaticInit25f8cae84e9b6b1c64dbee5f269829a2
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -1652,7 +1652,6 @@ class ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba
'Rector\\CodeQuality\\Rector\\If_\\ShortenElseIfRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/ShortenElseIfRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfElseToTernaryRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfExactValueReturnValueRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/SimplifyIfExactValueReturnValueRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfIssetToNullCoalescingRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/SimplifyIfIssetToNullCoalescingRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfNotNullReturnRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/SimplifyIfNotNullReturnRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfNullableReturnRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php',
'Rector\\CodeQuality\\Rector\\If_\\SimplifyIfReturnBoolRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/If_/SimplifyIfReturnBoolRector.php',
@ -3260,9 +3259,9 @@ class ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit83af4ed37a67c995d5c32428732bd8ba::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit25f8cae84e9b6b1c64dbee5f269829a2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit25f8cae84e9b6b1c64dbee5f269829a2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit25f8cae84e9b6b1c64dbee5f269829a2::$classMap;
}, null, ClassLoader::class);
}