diff --git a/rector.php b/rector.php index b1e2a4217ab..729c9b6f663 100644 --- a/rector.php +++ b/rector.php @@ -4,6 +4,7 @@ use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector; use Rector\Config\RectorConfig; +use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector; use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector; use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector; use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector; @@ -11,6 +12,7 @@ use Rector\PHPUnit\Set\PHPUnitSetList; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; +use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector; use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector; return static function (RectorConfig $rectorConfig): void { @@ -80,13 +82,11 @@ ], // race condition with stmts aware patch and PHPStan type - \Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class => [ + AddMethodCallBasedStrictParamTypeRector::class => [ __DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php', ], - \Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector::class => [ - __DIR__ . '/src/Util/FileHasher.php', - ], + RemovePhpVersionIdCheckRector::class => [__DIR__ . '/src/Util/FileHasher.php'], ]); $rectorConfig->phpstanConfig(__DIR__ . '/phpstan-for-rector.neon'); diff --git a/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php b/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php index 41881d4bc3c..aa008f6c169 100644 --- a/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php +++ b/rules/Php80/Rector/Catch_/RemoveUnusedVariableInCatchRector.php @@ -75,6 +75,8 @@ public function refactor(Node $node): ?Node return null; } + $hasChanged = false; + foreach ($node->stmts as $key => $stmt) { if (! $stmt instanceof TryCatch) { continue; @@ -99,10 +101,15 @@ public function refactor(Node $node): ?Node } $catch->var = null; + $hasChanged = true; } } - return $node; + if ($hasChanged) { + return $node; + } + + return null; } public function provideMinPhpVersion(): int diff --git a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php index 1c77ce419ca..d7fe8ee7c87 100644 --- a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php +++ b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php @@ -7,8 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Empty_; +use PhpParser\Node\Expr\Isset_; use PHPStan\Analyser\Scope; use Rector\Strict\NodeFactory\ExactCompareFactory; use Rector\Strict\Rector\AbstractFalsyScalarRuleFixerRector; @@ -91,6 +93,10 @@ private function refactorBooleanNot(BooleanNot $booleanNot, Scope $scope): Expr| } $empty = $booleanNot->expr; + if ($empty->expr instanceof ArrayDimFetch) { + return $this->createDimFetchBooleanAnd($empty); + } + $emptyExprType = $scope->getType($empty->expr); return $this->exactCompareFactory->createNotIdenticalFalsyCompare( @@ -105,4 +111,18 @@ private function refactorEmpty(Empty_ $empty, Scope $scope, bool $treatAsNonEmpt $exprType = $scope->getType($empty->expr); return $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $empty->expr, $treatAsNonEmpty); } + + private function createDimFetchBooleanAnd(Empty_ $empty): ?BooleanAnd + { + $exprType = $this->getType($empty->expr); + + $isset = new Isset_([$empty->expr]); + $compareExpr = $this->exactCompareFactory->createNotIdenticalFalsyCompare($exprType, $empty->expr, false); + + if (! $compareExpr instanceof Expr) { + return null; + } + + return new BooleanAnd($isset, $compareExpr); + } } diff --git a/tests/Issues/ClassOnObjectGetDebugType/config/configured_rule.php b/tests/Issues/ClassOnObjectGetDebugType/config/configured_rule.php index 598f4297e22..99e20a89e8d 100644 --- a/tests/Issues/ClassOnObjectGetDebugType/config/configured_rule.php +++ b/tests/Issues/ClassOnObjectGetDebugType/config/configured_rule.php @@ -7,8 +7,5 @@ use Rector\Php80\Rector\Ternary\GetDebugTypeRector; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rules([ - ClassOnObjectRector::class, - GetDebugTypeRector::class, - ]); + $rectorConfig->rules([ClassOnObjectRector::class, GetDebugTypeRector::class]); }; diff --git a/tests/Issues/EmptyBooleanCompare/EmptyBooleanCompareTest.php b/tests/Issues/EmptyBooleanCompare/EmptyBooleanCompareTest.php new file mode 100644 index 00000000000..7b628f2253f --- /dev/null +++ b/tests/Issues/EmptyBooleanCompare/EmptyBooleanCompareTest.php @@ -0,0 +1,28 @@ +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/tests/Issues/EmptyBooleanCompare/Fixture/fixture.php.inc b/tests/Issues/EmptyBooleanCompare/Fixture/fixture.php.inc new file mode 100644 index 00000000000..2b219f21723 --- /dev/null +++ b/tests/Issues/EmptyBooleanCompare/Fixture/fixture.php.inc @@ -0,0 +1,39 @@ + +----- + diff --git a/tests/Issues/EmptyBooleanCompare/config/configured_rule.php b/tests/Issues/EmptyBooleanCompare/config/configured_rule.php new file mode 100644 index 00000000000..2a67d4445fc --- /dev/null +++ b/tests/Issues/EmptyBooleanCompare/config/configured_rule.php @@ -0,0 +1,11 @@ +rules([BooleanInIfConditionRuleFixerRector::class, DisallowedEmptyRuleFixerRector::class]); +}; diff --git a/tests/Issues/InfiniteSwapParams/config/configured_rule.php b/tests/Issues/InfiniteSwapParams/config/configured_rule.php index fea1ec420fe..fe3bc2672a1 100644 --- a/tests/Issues/InfiniteSwapParams/config/configured_rule.php +++ b/tests/Issues/InfiniteSwapParams/config/configured_rule.php @@ -7,8 +7,5 @@ use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->rules([ - RemoveUnusedVariableInCatchRector::class, - OptionalParametersAfterRequiredRector::class - ]); + $rectorConfig->rules([RemoveUnusedVariableInCatchRector::class, OptionalParametersAfterRequiredRector::class]); };