diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 1f520c8a538..a93c35267db 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 414 Rules Overview +# 413 Rules Overview
@@ -6,7 +6,7 @@ - [Arguments](#arguments) (6) -- [CodeQuality](#codequality) (78) +- [CodeQuality](#codequality) (77) - [CodingStyle](#codingstyle) (37) @@ -1343,13 +1343,16 @@ Simplify `is_array` and `empty` functions combination into a simple identical ch ### SimplifyEmptyCheckOnEmptyArrayRector -Simplify `empty` functions calls on empty arrays +Simplify `empty()` functions calls on empty arrays - class: [`Rector\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector`](../rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php) ```diff --$array = []; if(empty($values)) -+$array = []; if([] === $values) + $array = []; + +-if (empty($values)) { ++if ([] === $values) { + } ```
@@ -1547,25 +1550,6 @@ Simplify tautology ternary to value
-### SimplifyUselessLastVariableAssignRector - -Removes the latest useless variable assigns before a variable will return. - -- class: [`Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector`](../rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php) - -```diff - function ($b) { -- $a = true; - if ($b === 1) { - return $b; - } -- return $a; -+ return true; - }; -``` - -
- ### SimplifyUselessVariableRector Removes useless variable assigns diff --git a/config/set/code-quality.php b/config/set/code-quality.php index 3e9955f0842..5c67125423e 100644 --- a/config/set/code-quality.php +++ b/config/set/code-quality.php @@ -47,7 +47,6 @@ use Rector\CodeQuality\Rector\FuncCall\StrvalToTypeCastRector; use Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector; use Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector; -use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessLastVariableAssignRector; use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector; use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector; use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector; @@ -194,7 +193,6 @@ InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, - SimplifyUselessLastVariableAssignRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/assign_ops.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/assign_ops.php.inc deleted file mode 100644 index ddb966e197b..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/assign_ops.php.inc +++ /dev/null @@ -1,47 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/empty_array.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/empty_array.php.inc deleted file mode 100644 index 2f7e1da145e..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/empty_array.php.inc +++ /dev/null @@ -1,36 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture.php.inc deleted file mode 100644 index a8db14c64d8..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,91 +0,0 @@ - $a++, - --$b => ++$a, - ]; - return $c; -}; - -?> ------ - $a++, - --$b => ++$a, - ]; - return $c; -}; - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture2.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture2.php.inc deleted file mode 100644 index aa51b2a4709..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/fixture2.php.inc +++ /dev/null @@ -1,413 +0,0 @@ -$parameter = 'this'; - } - -} - -$noErrorToo = null; -function ($noError = 'noError') use ($noErrorToo) { - -}; - -$used = true; - -function foo($foo) { - return preg_replace_callback('~~', function ($matches) { - return $matches[0]; - }, $foo); -} - -return $used; - -function ($values) { - $foo = ''; - - foreach ($values as $value) { - echo $foo . $value; - } -}; - -function () { - for ($i = 0; $i < 10; $i++) { - } -}; - -function ($values) { - foreach ($values as $value) { - $foo = 'foo' . $value; - } - echo $foo; -}; - -function ($values) { - list($a, $b) = $values; - return $a + $b; -}; - -function ($values) { - [$c, $d] = $values; - return $c * $d; -}; - -function ($values) { - $current = 'current'; - $next = 'next'; - - while ($next) { - if ($current) { - - } - - $current = false; - - if (true) { - foreach ($values as $value) { - $next = $value; - } - } - - do { - $previous = 'previous'; - } while ($previous); - } -}; - -function (&$parameter) { - $parameter = 'value-by-reference'; -}; - -function () use (&$inheritedVariable) { - $inheritedVariable = 'value-by-reference'; -}; - -function ($interval) { - $j = 0; - for ($i = $j; $i < 10; $i += $interval) { - } -}; - -function () { - static $static = false; - if ($static) { - return; - } - - $static = true; -}; - -function () { - $a = 'a'; - $b = 'b'; - - $this->compact; - - return compact('a', "b"); -}; - -function () { - $a = ''; - echo "$a"; -}; - -function () { - $a = ''; - echo "${a}"; -}; - -function () { - $a = ''; - echo "$a()"; -}; - -function () { - $a = ''; - echo <<a, $this->b) = [$a, $b]; - } - -} - -function () { - $i = 0; - while ($i++ <= 10) { - } -}; - -function () { - $i = 0; - do { - } while (++$i <= 10); -}; - -function () { - $i = 10; - while ($i-- > 0) { - } -}; - -function () { - $i = 10; - do { - } while (--$i > 0); -}; - -function ($data) { - $i = 0; - $c = ''; - foreach ($data as $c) { - $c = $i++; - } - echo $c; -}; - -function ($values) { - $expectedKey = 0; - - foreach ($values as $key => $value) { - if ($key !== $expectedKey++) { - return $value; - } - } - - return null; -}; - -?> ------ -$parameter = 'this'; - } - -} - -$noErrorToo = null; -function ($noError = 'noError') use ($noErrorToo) { - -}; - -function foo($foo) { - return preg_replace_callback('~~', function ($matches) { - return $matches[0]; - }, $foo); -} - -return true; - -function ($values) { - $foo = ''; - - foreach ($values as $value) { - echo $foo . $value; - } -}; - -function () { - for ($i = 0; $i < 10; $i++) { - } -}; - -function ($values) { - foreach ($values as $value) { - $foo = 'foo' . $value; - } - echo $foo; -}; - -function ($values) { - list($a, $b) = $values; - return $a + $b; -}; - -function ($values) { - [$c, $d] = $values; - return $c * $d; -}; - -function ($values) { - $current = 'current'; - $next = 'next'; - - while ($next) { - if ($current) { - - } - - $current = false; - - if (true) { - foreach ($values as $value) { - $next = $value; - } - } - - do { - $previous = 'previous'; - } while ($previous); - } -}; - -function (&$parameter) { - $parameter = 'value-by-reference'; -}; - -function () use (&$inheritedVariable) { - $inheritedVariable = 'value-by-reference'; -}; - -function ($interval) { - $j = 0; - for ($i = $j; $i < 10; $i += $interval) { - } -}; - -function () { - static $static = false; - if ($static) { - return; - } - - $static = true; -}; - -function () { - $a = 'a'; - $b = 'b'; - - $this->compact; - - return compact('a', "b"); -}; - -function () { - $a = ''; - echo "$a"; -}; - -function () { - $a = ''; - echo "${a}"; -}; - -function () { - $a = ''; - echo "$a()"; -}; - -function () { - $a = ''; - echo <<a, $this->b) = [$a, $b]; - } - -} - -function () { - $i = 0; - while ($i++ <= 10) { - } -}; - -function () { - $i = 0; - do { - } while (++$i <= 10); -}; - -function () { - $i = 10; - while ($i-- > 0) { - } -}; - -function () { - $i = 10; - do { - } while (--$i > 0); -}; - -function ($data) { - $i = 0; - $c = ''; - foreach ($data as $c) { - $c = $i++; - } - echo $c; -}; - -function ($values) { - $expectedKey = 0; - - foreach ($values as $key => $value) { - if ($key !== $expectedKey++) { - return $value; - } - } - - return null; -}; - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/nested_ifs.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/nested_ifs.php.inc deleted file mode 100644 index b1943e60bd7..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/nested_ifs.php.inc +++ /dev/null @@ -1,52 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_call_by_ref.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_call_by_ref.php.inc deleted file mode 100644 index 08495b3d9d3..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_call_by_ref.php.inc +++ /dev/null @@ -1,13 +0,0 @@ - $value, - 'bar', - 'baz' => [ - 'qux', - ], - ]; - - if (\rand(0, 1)) { - echo ''; - } - - return $content; - } -} - -?> \ No newline at end of file diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_global_variable.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_global_variable.php.inc deleted file mode 100644 index 529da4a3997..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_global_variable.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -run($skipOnMethodCall); - return $content; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_on_nested_return.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_on_nested_return.php.inc deleted file mode 100644 index c67ce6daeaf..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_on_nested_return.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -getValue(); - - /** @var string $name */ - return $name; - } - - private function getValue() - { - return 'name'; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_return_by_ref.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_return_by_ref.php.inc deleted file mode 100644 index 9c57428626d..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/skip_return_by_ref.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - 'foo', - 'bar', - 'baz' => [ - 'qux', - ], - ]; - - if (\rand(0, 1)) { - echo ''; - } - - return $content; - } -} - -?> ------ - 'foo', - 'bar', - 'baz' => [ - 'qux', - ], - ]; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/variable_variable.php.inc b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/variable_variable.php.inc deleted file mode 100644 index a493299deae..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/Fixture/variable_variable.php.inc +++ /dev/null @@ -1,32 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/SimplifyUselessLastVariableAssignRectorTest.php b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/SimplifyUselessLastVariableAssignRectorTest.php deleted file mode 100644 index 4a58c41df97..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/SimplifyUselessLastVariableAssignRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/config/configured_rule.php deleted file mode 100644 index 3d5d3bdccc8..00000000000 --- a/rules-tests/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(SimplifyUselessLastVariableAssignRector::class); -}; diff --git a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php index 305ff00f409..7123a6fdeab 100644 --- a/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php +++ b/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php @@ -44,8 +44,22 @@ public function __construct( public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( - 'Simplify `empty` functions calls on empty arrays', - [new CodeSample('$array = []; if(empty($values))', '$array = []; if([] === $values)')] + 'Simplify empty() functions calls on empty arrays', + [new CodeSample( + <<<'CODE_SAMPLE' +$array = []; + +if (empty($values)) { +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$array = []; + +if ([] === $values) { +} +CODE_SAMPLE + )] ); } diff --git a/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php b/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php deleted file mode 100644 index 0f385d8a21d..00000000000 --- a/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessLastVariableAssignRector.php +++ /dev/null @@ -1,290 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [StmtsAwareInterface::class]; - } - - /** - * @param StmtsAwareInterface $node - */ - public function refactor(Node $node): ?Node - { - $stmts = $node->stmts; - if ($stmts === null) { - return null; - } - - foreach ($stmts as $stmt) { - if (! $stmt instanceof Return_) { - continue; - } - - if ($this->shouldSkip($stmt)) { - return null; - } - - $assignStmt = $this->getLatestVariableAssignment($stmts, $stmt); - if (! $assignStmt instanceof Expression) { - return null; - } - - if ($this->shouldSkipOnAssignStmt($assignStmt)) { - return null; - } - - if ($this->isAssigmentUseless($stmts, $assignStmt, $stmt)) { - return null; - } - - $assign = $assignStmt->expr; - if (! $assign instanceof Assign && ! $assign instanceof AssignOp) { - return null; - } - - $this->removeNode($assignStmt); - return $this->processSimplifyUselessVariable($node, $stmt, $assign); - } - - return null; - } - - private function shouldSkip(Return_ $return): bool - { - $variable = $return->expr; - if (! $variable instanceof Variable) { - return true; - } - - if ($this->returnAnalyzer->hasByRefReturn($return)) { - return true; - } - - if ($this->variableAnalyzer->isStaticOrGlobal($variable)) { - return true; - } - - if ($this->variableAnalyzer->isUsedByReference($variable)) { - return true; - } - - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($return); - return ! $phpDocInfo->getVarType() instanceof MixedType; - } - - /** - * @param Stmt[] $stmts - */ - private function getLatestVariableAssignment(array $stmts, Return_ $return): ?Expression - { - $returnVariable = $return->expr; - if (! $returnVariable instanceof Variable) { - return null; - } - - //Search for the latest variable assigment - foreach (\array_reverse($stmts) as $stmt) { - if (! $stmt instanceof Expression) { - continue; - } - - $assignNode = $stmt->expr; - if (! $assignNode instanceof Assign && ! $assignNode instanceof AssignOp) { - continue; - } - - $currentVariableNode = $assignNode->var; - if (! $currentVariableNode instanceof Variable) { - continue; - } - - if ($this->nodeNameResolver->areNamesEqual($returnVariable, $currentVariableNode)) { - return $stmt; - } - } - - return null; - } - - private function shouldSkipOnAssignStmt(Expression $expression): bool - { - if ($this->hasSomeComment($expression)) { - return true; - } - - $assign = $expression->expr; - if (! $assign instanceof Assign && ! $assign instanceof AssignOp) { - return true; - } - - $variable = $assign->var; - if (! $variable instanceof Variable) { - return true; - } - - $value = $assign->expr; - if ($this->exprAnalyzer->isDynamicExpr($value)) { - return true; - } - - return $value instanceof Array_ && $this->arrayManipulator->isDynamicArray($value); - } - - private function hasSomeComment(Expression $expression): bool - { - if ($expression->getComments() !== []) { - return true; - } - - return $expression->getDocComment() instanceof Doc; - } - - /** - * @param Stmt[] $stmts - */ - private function isAssigmentUseless(array $stmts, Expression $expression, Return_ $return): bool - { - $assign = $expression->expr; - if (! $assign instanceof Assign && ! $assign instanceof AssignOp) { - return false; - } - - $variable = $assign->var; - if (! $variable instanceof Variable) { - return false; - } - - $nodesInRange = $this->getNodesInRange($stmts, $expression, $return); - if ($nodesInRange === null) { - return false; - } - - //Find the variable usage - $variableUsageNodes = $this->betterNodeFinder->find( - $nodesInRange, - fn (Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $variable) - ); - - //Should be exactly used 2 times (assignment + return) - return count($variableUsageNodes) !== 2; - } - - /** - * @param Stmt[] $stmts - * @return Stmt[]|null - */ - private function getNodesInRange(array $stmts, Expression $expression, Return_ $return): ?array - { - $resultStmts = []; - $wasStarted = false; - - foreach ($stmts as $stmt) { - if ($stmt === $expression) { - $wasStarted = true; - } - - if ($wasStarted) { - $resultStmts[] = $stmt; - } - - if ($stmt === $return) { - return $resultStmts; - } - } - - if ($wasStarted) { - // This should not happen, if you land here check your given parameter - return null; - } - - return $resultStmts; - } - - private function processSimplifyUselessVariable( - StmtsAwareInterface $stmtsAware, - Return_ $return, - Assign|AssignOp $assign, - ): ?StmtsAwareInterface { - if (! $assign instanceof Assign) { - $binaryClass = $this->assignAndBinaryMap->getAlternative($assign); - if ($binaryClass === null) { - return null; - } - - $return->expr = new $binaryClass($assign->var, $assign->expr); - } else { - $return->expr = $assign->expr; - } - - return $stmtsAware; - } -}