From 3c73144994801b88aa22c4d06cac1d97657255e8 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 13:56:48 +0200 Subject: [PATCH 01/10] Add ForeachWithReturnToArrayAnyRector There is already a rule to convert foreach that contains a break statement with array_any. This new rule adds support for foreach that return early with true --- .../Fixture/basic_usage.php.inc | 61 +++++++ .../Fixture/skip_missing_return_false.php.inc | 15 ++ ..._multiple_statements_after_foreach.php.inc | 17 ++ ...ple_statements_within_if_statement.php.inc | 17 ++ .../Fixture/skip_nested_if_statements.php.inc | 18 ++ .../Fixture/skip_non_array.php.inc | 16 ++ ...ected_early_return_other_than_true.php.inc | 16 ++ .../skip_variable_reuse_after_foreach.php.inc | 23 +++ .../Fixture/with_key.php.inc | 32 ++++ .../ForeachWithReturnToArrayAnyRectorTest.php | 28 +++ .../config/configured_rule.php | 10 ++ .../ForeachWithReturnToArrayAnyRector.php | 167 ++++++++++++++++++ 12 files changed, 420 insertions(+) create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_multiple_statements_after_foreach.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_multiple_statements_within_if_statement.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_nested_if_statements.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_non_array.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_unexpected_early_return_other_than_true.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_variable_reuse_after_foreach.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/with_key.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.php create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php create mode 100644 rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc new file mode 100644 index 00000000000..859f9a4d784 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc @@ -0,0 +1,61 @@ + 10) { + return true; + } + } + return false; + } + + public function checkWithKey(array $items) + { + foreach ($items as $key => $value) { + if ($value === 'target') { + return true; + } + } + return false; + } +} + +?> +----- + str_starts_with($animal, 'c')); + } + + public function checkNumber(array $numbers) + { + return array_any($numbers, fn($number) => $number > 10); + } + + public function checkWithKey(array $items) + { + return array_any($items, fn($value) => $value === 'target'); + } +} +?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc new file mode 100644 index 00000000000..e36fb2caaf4 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc @@ -0,0 +1,15 @@ + $animal) { + if (str_starts_with($animal, 'c') && $key === 1) { + return true; + } + } + return false; + } +} + +?> +----- + str_starts_with($animal, 'c') && $key === 1); + } +} + +?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.php b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.php new file mode 100644 index 00000000000..d71a0829fa0 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.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/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php new file mode 100644 index 00000000000..a36c4977301 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(ForeachWithReturnToArrayAnyRector::class); +}; diff --git a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php new file mode 100644 index 00000000000..401f05db789 --- /dev/null +++ b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php @@ -0,0 +1,167 @@ + str_starts_with($animal, 'c')); +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [StmtsAwareInterface::class]; + } + + /** + * @param StmtsAwareInterface $node + */ + public function refactor(Node $node) : ?Node + { + if ($node->stmts === null) { + return null; + } + + foreach ($node->stmts as $key => $stmt) { + if ( ! $stmt instanceof Foreach_) { + continue; + } + + $foreach = $stmt; + $nextStmt = $node->stmts[$key + 1] ?? null; + + if ( ! $nextStmt instanceof Return_) { + continue; + } + + if ( ! $nextStmt->expr instanceof Expr) { + continue; + } + + if ( ! $this->valueResolver->isFalse($nextStmt->expr)) { + continue; + } + + $this->isValidForeachStructure($foreach); + $firstNodeInsideForeach = $foreach->stmts[0]; + $condition = $firstNodeInsideForeach->cond; + $params = []; + + if ($foreach->valueVar instanceof Variable) { + $params[] = new Param($foreach->valueVar); + } + + if ( + $foreach->keyVar instanceof Variable && + $this->foreachKeyUsedInConditionalAnalyzer->isUsed($foreach->keyVar, $condition) + ) { + $params[] = new Param(new Variable((string) $this->getName($foreach->keyVar))); + } + + $arrowFunction = new ArrowFunction([ + 'params' => $params, + 'expr' => $condition, + ]); + + $funcCall = $this->nodeFactory->createFuncCall('array_any', [$foreach->expr, $arrowFunction]); + + $node->stmts[$key] = new Return_($funcCall); + unset($node->stmts[$key + 1]); + $node->stmts = array_values($node->stmts); + + return $node; + } + + return null; + } + + private function isValidForeachStructure(Foreach_ $foreach) : bool + { + if (count($foreach->stmts) !== 1) { + return false; + } + + if ( ! $foreach->stmts[0] instanceof If_) { + return false; + } + + $ifStmt = $foreach->stmts[0]; + + if (count($ifStmt->stmts) !== 1) { + return false; + } + + if ( ! $ifStmt->stmts[0] instanceof Return_) { + return false; + } + + $returnStmt = $ifStmt->stmts[0]; + + if ( ! $returnStmt->expr instanceof Expr) { + return false; + } + + if ( ! $this->valueResolver->isTrue($returnStmt->expr)) { + return false; + } + + return true; + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ARRAY_FIND; + } +} From f73ddec1feee121b43ce1a9c7260f6a946a3ee46 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 14:21:57 +0200 Subject: [PATCH 02/10] fix --- config/set/php84.php | 2 ++ .../Fixture/basic_usage.php.inc | 2 +- .../Foreach_/ForeachWithReturnToArrayAnyRector.php | 13 ++++++------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/config/set/php84.php b/config/set/php84.php index 8bdf9646907..cdbce7d685d 100644 --- a/config/set/php84.php +++ b/config/set/php84.php @@ -6,6 +6,7 @@ use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAllRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyRector; +use Rector\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindRector; use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector; @@ -25,6 +26,7 @@ ForeachToArrayFindKeyRector::class, ForeachToArrayAllRector::class, ForeachToArrayAnyRector::class, + ForeachWithReturnToArrayAnyRector::class, ] ); }; diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc index 859f9a4d784..a2ea0614fbb 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc @@ -1,6 +1,6 @@ isValidForeachStructure($foreach); + if( !$this->isValidForeachStructure($foreach)){ + continue; + } + $firstNodeInsideForeach = $foreach->stmts[0]; $condition = $firstNodeInsideForeach->cond; $params = []; From c415066c61283f48c8cf84830ccc582959f485ca Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 14:55:40 +0200 Subject: [PATCH 03/10] Fix --- config/set/php84.php | 2 +- .../Fixture/basic_usage.php.inc | 1 - ...ropertyVisibilityOnReadonlyClassRector.php | 2 +- .../Foreach_/ForeachToArrayAnyRector.php | 2 +- .../ForeachWithReturnToArrayAnyRector.php | 47 +++++++++++-------- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/config/set/php84.php b/config/set/php84.php index cdbce7d685d..0351918d022 100644 --- a/config/set/php84.php +++ b/config/set/php84.php @@ -6,9 +6,9 @@ use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAllRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyRector; -use Rector\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindRector; +use Rector\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector; use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector; use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector; use Rector\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector; diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc index a2ea0614fbb..a156e8db363 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc @@ -34,7 +34,6 @@ class BasicUsage return false; } } - ?> ----- stmts === null) { return null; } foreach ($node->stmts as $key => $stmt) { - if ( ! $stmt instanceof Foreach_) { + if (! $stmt instanceof Foreach_) { continue; } $foreach = $stmt; $nextStmt = $node->stmts[$key + 1] ?? null; - if ( ! $nextStmt instanceof Return_) { + if (! $nextStmt instanceof Return_) { continue; } - if ( ! $nextStmt->expr instanceof Expr) { + if (! $nextStmt->expr instanceof Expr) { continue; } - if ( ! $this->valueResolver->isFalse($nextStmt->expr)) { + if (! $this->valueResolver->isFalse($nextStmt->expr)) { continue; } - if( !$this->isValidForeachStructure($foreach)){ + if (! $this->isValidForeachStructure($foreach)) { continue; } @@ -126,13 +129,18 @@ public function refactor(Node $node) : ?Node return null; } - private function isValidForeachStructure(Foreach_ $foreach) : bool + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ARRAY_FIND; + } + + private function isValidForeachStructure(Foreach_ $foreach): bool { if (count($foreach->stmts) !== 1) { return false; } - if ( ! $foreach->stmts[0] instanceof If_) { + if (! $foreach->stmts[0] instanceof If_) { return false; } @@ -142,25 +150,26 @@ private function isValidForeachStructure(Foreach_ $foreach) : bool return false; } - if ( ! $ifStmt->stmts[0] instanceof Return_) { + if (! $ifStmt->stmts[0] instanceof Return_) { return false; } $returnStmt = $ifStmt->stmts[0]; - if ( ! $returnStmt->expr instanceof Expr) { + if (! $returnStmt->expr instanceof Expr) { return false; } - if ( ! $this->valueResolver->isTrue($returnStmt->expr)) { + if (! $this->valueResolver->isTrue($returnStmt->expr)) { return false; } - return true; - } + $type = $this->nodeTypeResolver->getNativeType($foreach->expr); - public function provideMinPhpVersion(): int - { - return PhpVersionFeature::ARRAY_FIND; + if ($this->allowIterable) { + return $type->isIterable()->yes(); + } + + return $type->isArray()->yes(); } } From 1ac118a86aac7fa94b6d1886537ccbd4df5713e3 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 14:58:36 +0200 Subject: [PATCH 04/10] Fix --- .../Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php index e7f0f979d1d..bd327dd8d11 100644 --- a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php @@ -167,9 +167,11 @@ private function isValidForeachStructure(Foreach_ $foreach): bool $type = $this->nodeTypeResolver->getNativeType($foreach->expr); if ($this->allowIterable) { - return $type->isIterable()->yes(); + return $type->isIterable() + ->yes(); } - return $type->isArray()->yes(); + return $type->isArray() + ->yes(); } } From 5f3b220ee8be92c38cf19d5e3ffd3d55966cb55f Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 15:04:24 +0200 Subject: [PATCH 05/10] fix --- .../Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php index bd327dd8d11..faa8f17e5b8 100644 --- a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php @@ -97,8 +97,10 @@ public function refactor(Node $node): ?Node continue; } + /** @var If_ $firstNodeInsideForeach */ $firstNodeInsideForeach = $foreach->stmts[0]; $condition = $firstNodeInsideForeach->cond; + $params = []; if ($foreach->valueVar instanceof Variable) { From fe6a6de7b9592b5f37bd5897fdb6bba1c2c3e6a8 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 17:03:28 +0200 Subject: [PATCH 06/10] Combine array any patterns --- config/set/php84.php | 2 + .../boolean_assignment_basic_usage.php.inc | 71 ++++ ...ssignment_skip_multiple_statements.php.inc | 19 ++ ...ent_skip_no_boolean_initialization.php.inc | 18 + .../boolean_assignment_skip_no_break.php.inc | 17 + .../boolean_assignment_skip_non_array.php.inc | 18 + ..._skip_variable_reuse_after_foreach.php.inc | 23 ++ .../boolean_assignment_with_key.php.inc | 35 ++ .../Fixture/early_return_basic_usage.php.inc | 60 ++++ ...y_return_skip_missing_return_false.php.inc | 15 + ..._multiple_statements_after_foreach.php.inc | 17 + ...ple_statements_within_if_statement.php.inc | 17 + ...y_return_skip_nested_if_statements.php.inc | 18 + .../early_return_skip_non_array.php.inc | 16 + ...ected_early_return_other_than_true.php.inc | 16 + ..._skip_variable_reuse_after_foreach.php.inc | 23 ++ .../Fixture/early_return_with_key.php.inc | 32 ++ .../ForeachToArrayAnyCombinedRectorTest.php | 28 ++ .../config/configured_rule.php | 10 + .../ForeachToArrayAnyCombinedRector.php | 320 ++++++++++++++++++ .../ForeachWithReturnToArrayAnyRector.php | 6 - 21 files changed, 775 insertions(+), 6 deletions(-) create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_multiple_statements.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_no_boolean_initialization.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_no_break.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_non_array.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_variable_reuse_after_foreach.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_with_key.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_basic_usage.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_missing_return_false.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_multiple_statements_after_foreach.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_multiple_statements_within_if_statement.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_nested_if_statements.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_non_array.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_unexpected_early_return_other_than_true.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_variable_reuse_after_foreach.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_with_key.php.inc create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/ForeachToArrayAnyCombinedRectorTest.php create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php create mode 100644 rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php diff --git a/config/set/php84.php b/config/set/php84.php index 0351918d022..959dcbc83b5 100644 --- a/config/set/php84.php +++ b/config/set/php84.php @@ -5,6 +5,7 @@ use Rector\Config\RectorConfig; use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAllRector; +use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyCombinedRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindRector; @@ -27,6 +28,7 @@ ForeachToArrayAllRector::class, ForeachToArrayAnyRector::class, ForeachWithReturnToArrayAnyRector::class, + ForeachToArrayAnyCombinedRector::class, ] ); }; diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc new file mode 100644 index 00000000000..7470b4568dc --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc @@ -0,0 +1,71 @@ + 10) { + $exists = true; + break; + } + } + return $exists; + } + + public function checkWithKey(array $items) + { + $hasMatch = false; + foreach ($items as $key => $value) { + if ($value === 'target') { + $hasMatch = true; + break; + } + } + return $hasMatch; + } +} + +?> +----- + str_starts_with($animal, 'c')); + return $found; + } + + public function checkNumber(array $numbers) + { + $exists = array_any($numbers, fn($number) => $number > 10); + return $exists; + } + + public function checkWithKey(array $items) + { + $hasMatch = array_any($items, fn($value) => $value === 'target'); + return $hasMatch; + } +} + +?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_multiple_statements.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_multiple_statements.php.inc new file mode 100644 index 00000000000..5e166722539 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_skip_multiple_statements.php.inc @@ -0,0 +1,19 @@ + $animal) { + if (str_starts_with($animal, 'c') && $key === 1) { + $found = true; + break; + } + } + return $found; + } +} + +?> +----- + str_starts_with($animal, 'c') && $key === 1); + return $found; + } +} + +?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_basic_usage.php.inc new file mode 100644 index 00000000000..1a1592ddc76 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_basic_usage.php.inc @@ -0,0 +1,60 @@ + 10) { + return true; + } + } + return false; + } + + public function checkWithKey(array $items) + { + foreach ($items as $key => $value) { + if ($value === 'target') { + return true; + } + } + return false; + } +} +?> +----- + str_starts_with($animal, 'c')); + } + + public function checkNumber(array $numbers) + { + return array_any($numbers, fn($number) => $number > 10); + } + + public function checkWithKey(array $items) + { + return array_any($items, fn($value) => $value === 'target'); + } +} +?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_missing_return_false.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_missing_return_false.php.inc new file mode 100644 index 00000000000..0a93daab9ed --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/early_return_skip_missing_return_false.php.inc @@ -0,0 +1,15 @@ + $animal) { + if (str_starts_with($animal, 'c') && $key === 1) { + return true; + } + } + return false; + } +} + +?> +----- + str_starts_with($animal, 'c') && $key === 1); + } +} + +?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/ForeachToArrayAnyCombinedRectorTest.php b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/ForeachToArrayAnyCombinedRectorTest.php new file mode 100644 index 00000000000..ceb7109f2bb --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/ForeachToArrayAnyCombinedRectorTest.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/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php new file mode 100644 index 00000000000..80e755bba52 --- /dev/null +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(ForeachToArrayAnyCombinedRector::class); +}; diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php new file mode 100644 index 00000000000..37ff65916d1 --- /dev/null +++ b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php @@ -0,0 +1,320 @@ + str_starts_with($animal, 'c')); +CODE_SAMPLE + ), + new CodeSample( + <<<'CODE_SAMPLE' +foreach ($animals as $animal) { + if (str_starts_with($animal, 'c')) { + return true; + } +} +return false; +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +return array_any($animals, fn($animal) => str_starts_with($animal, 'c')); +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [StmtsAwareInterface::class]; + } + + /** + * @param StmtsAwareInterface $node + */ + public function refactor(Node $node): ?Node + { + if ($node->stmts === null) { + return null; + } + + return $this->refactorBooleanAssignmentPattern($node) + ?? $this->refactorEarlyReturnPattern($node); + } + + public function provideMinPhpVersion(): int + { + return PhpVersionFeature::ARRAY_ANY; + } + + private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?Node + { + foreach ($node->stmts as $key => $stmt) { + if (! $stmt instanceof Foreach_) { + continue; + } + + $prevStmt = $node->stmts[$key - 1] ?? null; + if (! $prevStmt instanceof Expression) { + continue; + } + + if (! $prevStmt->expr instanceof Assign) { + continue; + } + + $foreach = $stmt; + $prevAssign = $prevStmt->expr; + + if (! $this->valueResolver->isFalse($prevAssign->expr)) { + continue; + } + + if (! $prevAssign->var instanceof Variable) { + continue; + } + + $assignedVariable = $prevAssign->var; + + if (! $this->isValidBooleanAssignmentForeachStructure($foreach, $assignedVariable)) { + continue; + } + + if ($this->stmtsManipulator->isVariableUsedInNextStmt( + $node, + $key + 1, + (string) $this->getName($foreach->valueVar) + )) { + continue; + } + + /** @var If_ $firstNodeInsideForeach */ + $firstNodeInsideForeach = $foreach->stmts[0]; + + $condition = $firstNodeInsideForeach->cond; + $valueParam = $foreach->valueVar; + + if (! $valueParam instanceof Variable) { + continue; + } + + $params = [new Param($valueParam)]; + + if ($foreach->keyVar instanceof Variable && $this->foreachKeyUsedInConditionalAnalyzer->isUsed( + $foreach->keyVar, + $condition + )) { + $params[] = new Param(new Variable((string) $this->getName($foreach->keyVar))); + } + + $arrowFunction = new ArrowFunction([ + 'params' => $params, + 'expr' => $condition, + ]); + + $funcCall = $this->nodeFactory->createFuncCall('array_any', [$foreach->expr, $arrowFunction]); + + $newAssign = new Assign($assignedVariable, $funcCall); + $newExpression = new Expression($newAssign); + + unset($node->stmts[$key - 1]); + $node->stmts[$key] = $newExpression; + + $node->stmts = array_values($node->stmts); + + return $node; + } + + return null; + } + + private function refactorEarlyReturnPattern(StmtsAwareInterface $node): ?Node + { + foreach ($node->stmts as $key => $stmt) { + if (! $stmt instanceof Foreach_) { + continue; + } + + $foreach = $stmt; + $nextStmt = $node->stmts[$key + 1] ?? null; + + if (! $nextStmt instanceof Return_) { + continue; + } + + if (! $nextStmt->expr instanceof Expr) { + continue; + } + + if (! $this->valueResolver->isFalse($nextStmt->expr)) { + continue; + } + + if (! $this->isValidEarlyReturnForeachStructure($foreach)) { + continue; + } + + /** @var If_ $firstNodeInsideForeach */ + $firstNodeInsideForeach = $foreach->stmts[0]; + $condition = $firstNodeInsideForeach->cond; + + $params = []; + + if ($foreach->valueVar instanceof Variable) { + $params[] = new Param($foreach->valueVar); + } + + if ( + $foreach->keyVar instanceof Variable && + $this->foreachKeyUsedInConditionalAnalyzer->isUsed($foreach->keyVar, $condition) + ) { + $params[] = new Param(new Variable((string) $this->getName($foreach->keyVar))); + } + + $arrowFunction = new ArrowFunction([ + 'params' => $params, + 'expr' => $condition, + ]); + + $funcCall = $this->nodeFactory->createFuncCall('array_any', [$foreach->expr, $arrowFunction]); + + $node->stmts[$key] = new Return_($funcCall); + unset($node->stmts[$key + 1]); + $node->stmts = array_values($node->stmts); + + return $node; + } + + return null; + } + + private function isValidBooleanAssignmentForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool + { + if (count($foreach->stmts) !== 1) { + return false; + } + + $firstStmt = $foreach->stmts[0]; + if ( + ! $firstStmt instanceof If_ || + count($firstStmt->stmts) !== 2 + ) { + return false; + } + + $assignmentStmt = $firstStmt->stmts[0]; + $breakStmt = $firstStmt->stmts[1]; + + if ( + ! $assignmentStmt instanceof Expression || + ! $assignmentStmt->expr instanceof Assign || + ! $breakStmt instanceof Break_ + ) { + return false; + } + + $assignment = $assignmentStmt->expr; + + if (! $this->nodeComparator->areNodesEqual($assignment->var, $assignedVariable)) { + return false; + } + + if (! $this->valueResolver->isTrue($assignment->expr)) { + return false; + } + + $type = $this->nodeTypeResolver->getNativeType($foreach->expr); + return $type->isArray() + ->yes(); + } + + private function isValidEarlyReturnForeachStructure(Foreach_ $foreach): bool + { + if (count($foreach->stmts) !== 1) { + return false; + } + + if (! $foreach->stmts[0] instanceof If_) { + return false; + } + + $ifStmt = $foreach->stmts[0]; + + if (count($ifStmt->stmts) !== 1) { + return false; + } + + if (! $ifStmt->stmts[0] instanceof Return_) { + return false; + } + + $returnStmt = $ifStmt->stmts[0]; + + if (! $returnStmt->expr instanceof Expr) { + return false; + } + + if (! $this->valueResolver->isTrue($returnStmt->expr)) { + return false; + } + + $type = $this->nodeTypeResolver->getNativeType($foreach->expr); + + return $type->isArray() + ->yes(); + } +} diff --git a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php index faa8f17e5b8..ce912d0dc03 100644 --- a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php @@ -29,7 +29,6 @@ final class ForeachWithReturnToArrayAnyRector extends AbstractRector implements public function __construct( private readonly ValueResolver $valueResolver, private readonly ForeachKeyUsedInConditionalAnalyzer $foreachKeyUsedInConditionalAnalyzer, - private readonly bool $allowIterable = false ) { } @@ -168,11 +167,6 @@ private function isValidForeachStructure(Foreach_ $foreach): bool $type = $this->nodeTypeResolver->getNativeType($foreach->expr); - if ($this->allowIterable) { - return $type->isIterable() - ->yes(); - } - return $type->isArray() ->yes(); } From 876cf12ef160dd026a12d87f7b22783668f3c894 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 17:06:31 +0200 Subject: [PATCH 07/10] Delete separate array any rules --- config/set/php84.php | 4 - .../Fixture/basic_usage.php.inc | 71 ------ .../Fixture/skip_multiple_statements.php.inc | 19 -- .../skip_no_boolean_initialization.php.inc | 18 -- .../Fixture/skip_no_break.php.inc | 17 -- .../Fixture/skip_non_array.php.inc | 18 -- .../skip_variable_reuse_after_foreach.php.inc | 23 -- .../Fixture/with_key.php.inc | 35 --- .../ForeachToArrayAnyRectorTest.php | 28 --- .../config/configured_rule.php | 10 - .../Fixture/basic_usage.php.inc | 60 ------ .../Fixture/skip_missing_return_false.php.inc | 15 -- ..._multiple_statements_after_foreach.php.inc | 17 -- ...ple_statements_within_if_statement.php.inc | 17 -- .../Fixture/skip_nested_if_statements.php.inc | 18 -- .../Fixture/skip_non_array.php.inc | 16 -- ...ected_early_return_other_than_true.php.inc | 16 -- .../skip_variable_reuse_after_foreach.php.inc | 23 -- .../Fixture/with_key.php.inc | 32 --- .../ForeachWithReturnToArrayAnyRectorTest.php | 28 --- .../config/configured_rule.php | 10 - .../Foreach_/ForeachToArrayAnyRector.php | 202 ------------------ .../ForeachWithReturnToArrayAnyRector.php | 173 --------------- 23 files changed, 870 deletions(-) delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_no_boolean_initialization.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_no_break.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_non_array.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_variable_reuse_after_foreach.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/with_key.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/ForeachToArrayAnyRectorTest.php delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/config/configured_rule.php delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_multiple_statements_after_foreach.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_multiple_statements_within_if_statement.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_nested_if_statements.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_non_array.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_unexpected_early_return_other_than_true.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_variable_reuse_after_foreach.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/with_key.php.inc delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.php delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php delete mode 100644 rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php delete mode 100644 rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php diff --git a/config/set/php84.php b/config/set/php84.php index 959dcbc83b5..9c6df9ff4c3 100644 --- a/config/set/php84.php +++ b/config/set/php84.php @@ -6,10 +6,8 @@ use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAllRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyCombinedRector; -use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindRector; -use Rector\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector; use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector; use Rector\Php84\Rector\FuncCall\RoundingModeEnumRector; use Rector\Php84\Rector\MethodCall\NewMethodCallWithoutParenthesesRector; @@ -26,8 +24,6 @@ ForeachToArrayFindRector::class, ForeachToArrayFindKeyRector::class, ForeachToArrayAllRector::class, - ForeachToArrayAnyRector::class, - ForeachWithReturnToArrayAnyRector::class, ForeachToArrayAnyCombinedRector::class, ] ); diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc deleted file mode 100644 index b146121786c..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc +++ /dev/null @@ -1,71 +0,0 @@ - 10) { - $exists = true; - break; - } - } - return $exists; - } - - public function checkWithKey(array $items) - { - $hasMatch = false; - foreach ($items as $key => $value) { - if ($value === 'target') { - $hasMatch = true; - break; - } - } - return $hasMatch; - } -} - -?> ------ - str_starts_with($animal, 'c')); - return $found; - } - - public function checkNumber(array $numbers) - { - $exists = array_any($numbers, fn($number) => $number > 10); - return $exists; - } - - public function checkWithKey(array $items) - { - $hasMatch = array_any($items, fn($value) => $value === 'target'); - return $hasMatch; - } -} - -?> \ No newline at end of file diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc deleted file mode 100644 index 598b2c446d3..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc +++ /dev/null @@ -1,19 +0,0 @@ - $animal) { - if (str_starts_with($animal, 'c') && $key === 1) { - $found = true; - break; - } - } - return $found; - } -} - -?> ------ - str_starts_with($animal, 'c') && $key === 1); - return $found; - } -} - -?> \ No newline at end of file diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/ForeachToArrayAnyRectorTest.php b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/ForeachToArrayAnyRectorTest.php deleted file mode 100644 index acdf3c48c3a..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/ForeachToArrayAnyRectorTest.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/Php84/Rector/Foreach_/ForeachToArrayAnyRector/config/configured_rule.php b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/config/configured_rule.php deleted file mode 100644 index aef591ab108..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ForeachToArrayAnyRector::class); -}; diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc deleted file mode 100644 index a156e8db363..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/basic_usage.php.inc +++ /dev/null @@ -1,60 +0,0 @@ - 10) { - return true; - } - } - return false; - } - - public function checkWithKey(array $items) - { - foreach ($items as $key => $value) { - if ($value === 'target') { - return true; - } - } - return false; - } -} -?> ------ - str_starts_with($animal, 'c')); - } - - public function checkNumber(array $numbers) - { - return array_any($numbers, fn($number) => $number > 10); - } - - public function checkWithKey(array $items) - { - return array_any($items, fn($value) => $value === 'target'); - } -} -?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc deleted file mode 100644 index e36fb2caaf4..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/Fixture/skip_missing_return_false.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - $animal) { - if (str_starts_with($animal, 'c') && $key === 1) { - return true; - } - } - return false; - } -} - -?> ------ - str_starts_with($animal, 'c') && $key === 1); - } -} - -?> diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.php b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.php deleted file mode 100644 index d71a0829fa0..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/ForeachWithReturnToArrayAnyRectorTest.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/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php b/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php deleted file mode 100644 index a36c4977301..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ForeachWithReturnToArrayAnyRector::class); -}; diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php deleted file mode 100644 index 9beaf679928..00000000000 --- a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php +++ /dev/null @@ -1,202 +0,0 @@ - str_starts_with($animal, 'c')); -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [StmtsAwareInterface::class]; - } - - /** - * @param StmtsAwareInterface $node - */ - public function refactor(Node $node): ?Node - { - if ($node->stmts === null) { - return null; - } - - foreach ($node->stmts as $key => $stmt) { - if (! $stmt instanceof Foreach_) { - continue; - } - - $prevStmt = $node->stmts[$key - 1] ?? null; - if (! $prevStmt instanceof Expression) { - continue; - } - - if (! $prevStmt->expr instanceof Assign) { - continue; - } - - $foreach = $stmt; - $prevAssign = $prevStmt->expr; - - if (! $this->valueResolver->isFalse($prevAssign->expr)) { - continue; - } - - if (! $prevAssign->var instanceof Variable) { - continue; - } - - $assignedVariable = $prevAssign->var; - - if (! $this->isValidForeachStructure($foreach, $assignedVariable)) { - continue; - } - - if ($this->stmtsManipulator->isVariableUsedInNextStmt( - $node, - $key + 1, - (string) $this->getName($foreach->valueVar) - )) { - continue; - } - - /** @var If_ $firstNodeInsideForeach */ - $firstNodeInsideForeach = $foreach->stmts[0]; - - $condition = $firstNodeInsideForeach->cond; - $valueParam = $foreach->valueVar; - - if (! $valueParam instanceof Variable) { - continue; - } - - $params = [new Param($valueParam)]; - - if ($foreach->keyVar instanceof Variable && $this->foreachKeyUsedInConditionalAnalyzer->isUsed( - $foreach->keyVar, - $condition - )) { - $params[] = new Param(new Variable((string) $this->getName($foreach->keyVar))); - } - - $arrowFunction = new ArrowFunction([ - 'params' => $params, - 'expr' => $condition, - ]); - - $funcCall = $this->nodeFactory->createFuncCall('array_any', [$foreach->expr, $arrowFunction]); - - $newAssign = new Assign($assignedVariable, $funcCall); - $newExpression = new Expression($newAssign); - - unset($node->stmts[$key - 1]); - $node->stmts[$key] = $newExpression; - - $node->stmts = array_values($node->stmts); - - return $node; - } - - return null; - } - - public function provideMinPhpVersion(): int - { - return PhpVersionFeature::ARRAY_ANY; - } - - private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVariable): bool - { - if (count($foreach->stmts) !== 1) { - return false; - } - - $firstStmt = $foreach->stmts[0]; - if ( - ! $firstStmt instanceof If_ || - count($firstStmt->stmts) !== 2 - ) { - return false; - } - - $assignmentStmt = $firstStmt->stmts[0]; - $breakStmt = $firstStmt->stmts[1]; - - if ( - ! $assignmentStmt instanceof Expression || - ! $assignmentStmt->expr instanceof Assign || - ! $breakStmt instanceof Break_ - ) { - return false; - } - - $assignment = $assignmentStmt->expr; - - if (! $this->nodeComparator->areNodesEqual($assignment->var, $assignedVariable)) { - return false; - } - - if (! $this->valueResolver->isTrue($assignment->expr)) { - return false; - } - - $type = $this->nodeTypeResolver->getNativeType($foreach->expr); - return $type->isArray() - ->yes(); - } -} diff --git a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php b/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php deleted file mode 100644 index ce912d0dc03..00000000000 --- a/rules/Php84/Rector/Foreach_/ForeachWithReturnToArrayAnyRector.php +++ /dev/null @@ -1,173 +0,0 @@ - str_starts_with($animal, 'c')); -CODE_SAMPLE - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [StmtsAwareInterface::class]; - } - - /** - * @param StmtsAwareInterface $node - */ - public function refactor(Node $node): ?Node - { - if ($node->stmts === null) { - return null; - } - - foreach ($node->stmts as $key => $stmt) { - if (! $stmt instanceof Foreach_) { - continue; - } - - $foreach = $stmt; - $nextStmt = $node->stmts[$key + 1] ?? null; - - if (! $nextStmt instanceof Return_) { - continue; - } - - if (! $nextStmt->expr instanceof Expr) { - continue; - } - - if (! $this->valueResolver->isFalse($nextStmt->expr)) { - continue; - } - - if (! $this->isValidForeachStructure($foreach)) { - continue; - } - - /** @var If_ $firstNodeInsideForeach */ - $firstNodeInsideForeach = $foreach->stmts[0]; - $condition = $firstNodeInsideForeach->cond; - - $params = []; - - if ($foreach->valueVar instanceof Variable) { - $params[] = new Param($foreach->valueVar); - } - - if ( - $foreach->keyVar instanceof Variable && - $this->foreachKeyUsedInConditionalAnalyzer->isUsed($foreach->keyVar, $condition) - ) { - $params[] = new Param(new Variable((string) $this->getName($foreach->keyVar))); - } - - $arrowFunction = new ArrowFunction([ - 'params' => $params, - 'expr' => $condition, - ]); - - $funcCall = $this->nodeFactory->createFuncCall('array_any', [$foreach->expr, $arrowFunction]); - - $node->stmts[$key] = new Return_($funcCall); - unset($node->stmts[$key + 1]); - $node->stmts = array_values($node->stmts); - - return $node; - } - - return null; - } - - public function provideMinPhpVersion(): int - { - return PhpVersionFeature::ARRAY_FIND; - } - - private function isValidForeachStructure(Foreach_ $foreach): bool - { - if (count($foreach->stmts) !== 1) { - return false; - } - - if (! $foreach->stmts[0] instanceof If_) { - return false; - } - - $ifStmt = $foreach->stmts[0]; - - if (count($ifStmt->stmts) !== 1) { - return false; - } - - if (! $ifStmt->stmts[0] instanceof Return_) { - return false; - } - - $returnStmt = $ifStmt->stmts[0]; - - if (! $returnStmt->expr instanceof Expr) { - return false; - } - - if (! $this->valueResolver->isTrue($returnStmt->expr)) { - return false; - } - - $type = $this->nodeTypeResolver->getNativeType($foreach->expr); - - return $type->isArray() - ->yes(); - } -} From fe81ee57fa0d5c767b89336ecf1c0edaffc9de60 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Fri, 8 Aug 2025 17:09:50 +0200 Subject: [PATCH 08/10] Rename ArrayAny rule --- config/set/php84.php | 4 ++-- .../config/configured_rule.php | 10 ---------- .../Fixture/boolean_assignment_basic_usage.php.inc | 4 ++-- ...boolean_assignment_skip_multiple_statements.php.inc | 2 +- ...n_assignment_skip_no_boolean_initialization.php.inc | 2 +- .../Fixture/boolean_assignment_skip_no_break.php.inc | 2 +- .../Fixture/boolean_assignment_skip_non_array.php.inc | 2 +- ...ssignment_skip_variable_reuse_after_foreach.php.inc | 2 +- .../Fixture/boolean_assignment_with_key.php.inc | 4 ++-- .../Fixture/early_return_basic_usage.php.inc | 4 ++-- .../early_return_skip_missing_return_false.php.inc | 2 +- ...turn_skip_multiple_statements_after_foreach.php.inc | 2 +- ...kip_multiple_statements_within_if_statement.php.inc | 2 +- .../early_return_skip_nested_if_statements.php.inc | 2 +- .../Fixture/early_return_skip_non_array.php.inc | 2 +- ...kip_unexpected_early_return_other_than_true.php.inc | 2 +- ...ly_return_skip_variable_reuse_after_foreach.php.inc | 2 +- .../Fixture/early_return_with_key.php.inc | 4 ++-- .../ForeachToArrayAnyRectorTest.php} | 4 ++-- .../ForeachToArrayAnyRector/config/configured_rule.php | 10 ++++++++++ ...yCombinedRector.php => ForeachToArrayAnyRector.php} | 4 ++-- 21 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_basic_usage.php.inc (88%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_skip_multiple_statements.php.inc (80%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_skip_no_boolean_initialization.php.inc (78%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_skip_no_break.php.inc (76%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_skip_non_array.php.inc (78%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_skip_variable_reuse_after_foreach.php.inc (81%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/boolean_assignment_with_key.php.inc (76%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_basic_usage.php.inc (86%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_missing_return_false.php.inc (73%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_multiple_statements_after_foreach.php.inc (76%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_multiple_statements_within_if_statement.php.inc (77%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_nested_if_statements.php.inc (79%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_non_array.php.inc (75%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_unexpected_early_return_other_than_true.php.inc (74%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_skip_variable_reuse_after_foreach.php.inc (81%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector => ForeachToArrayAnyRector}/Fixture/early_return_with_key.php.inc (73%) rename rules-tests/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector/ForeachToArrayAnyCombinedRectorTest.php => ForeachToArrayAnyRector/ForeachToArrayAnyRectorTest.php} (77%) create mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/config/configured_rule.php rename rules/Php84/Rector/Foreach_/{ForeachToArrayAnyCombinedRector.php => ForeachToArrayAnyRector.php} (97%) diff --git a/config/set/php84.php b/config/set/php84.php index 9c6df9ff4c3..8bdf9646907 100644 --- a/config/set/php84.php +++ b/config/set/php84.php @@ -5,7 +5,7 @@ use Rector\Config\RectorConfig; use Rector\Php84\Rector\Class_\DeprecatedAnnotationToDeprecatedAttributeRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayAllRector; -use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyCombinedRector; +use Rector\Php84\Rector\Foreach_\ForeachToArrayAnyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector; use Rector\Php84\Rector\Foreach_\ForeachToArrayFindRector; use Rector\Php84\Rector\FuncCall\AddEscapeArgumentRector; @@ -24,7 +24,7 @@ ForeachToArrayFindRector::class, ForeachToArrayFindKeyRector::class, ForeachToArrayAllRector::class, - ForeachToArrayAnyCombinedRector::class, + ForeachToArrayAnyRector::class, ] ); }; diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php deleted file mode 100644 index 80e755bba52..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(ForeachToArrayAnyCombinedRector::class); -}; diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_basic_usage.php.inc similarity index 88% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_basic_usage.php.inc index 7470b4568dc..e392146c228 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector/Fixture/boolean_assignment_basic_usage.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_basic_usage.php.inc @@ -1,6 +1,6 @@ rule(ForeachToArrayAnyRector::class); +}; diff --git a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php similarity index 97% rename from rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php rename to rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php index 37ff65916d1..3ff1f72a6b9 100644 --- a/rules/Php84/Rector/Foreach_/ForeachToArrayAnyCombinedRector.php +++ b/rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php @@ -26,9 +26,9 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyCombinedRector\ForeachToArrayAnyCombinedRectorTest + * @see \Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\ForeachToArrayAnyRectorTest */ -final class ForeachToArrayAnyCombinedRector extends AbstractRector implements MinPhpVersionInterface +final class ForeachToArrayAnyRector extends AbstractRector implements MinPhpVersionInterface { public function __construct( private readonly ValueResolver $valueResolver, From f14d497b1564044ef8791d6cc748d3354f78ff39 Mon Sep 17 00:00:00 2001 From: Orest Divintari Date: Sat, 9 Aug 2025 09:34:58 +0200 Subject: [PATCH 09/10] delete test --- ..._skip_variable_reuse_after_foreach.php.inc | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_variable_reuse_after_foreach.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_variable_reuse_after_foreach.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_variable_reuse_after_foreach.php.inc deleted file mode 100644 index b5f8f12d9fa..00000000000 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_variable_reuse_after_foreach.php.inc +++ /dev/null @@ -1,23 +0,0 @@ - Date: Sat, 9 Aug 2025 17:17:38 +0200 Subject: [PATCH 10/10] rename tests --- ...ean_assignment_basic_usage.php.inc => basic_usage.php.inc} | 0 .../Fixture/early_return_basic_usage.php.inc | 4 ++-- .../Fixture/early_return_skip_missing_return_false.php.inc | 2 +- ...arly_return_skip_multiple_statements_after_foreach.php.inc | 2 +- ...eturn_skip_multiple_statements_within_if_statement.php.inc | 2 +- .../Fixture/early_return_skip_nested_if_statements.php.inc | 2 +- .../Fixture/early_return_skip_non_array.php.inc | 2 +- ...eturn_skip_unexpected_early_return_other_than_true.php.inc | 2 +- .../Fixture/early_return_with_key.php.inc | 4 ++-- ...le_statements.php.inc => skip_multiple_statements.php.inc} | 0 ...ization.php.inc => skip_no_boolean_initialization.php.inc} | 0 ...assignment_skip_no_break.php.inc => skip_no_break.php.inc} | 0 ...signment_skip_non_array.php.inc => skip_non_array.php.inc} | 0 ...each.php.inc => skip_variable_reuse_after_foreach.php.inc} | 0 .../{boolean_assignment_with_key.php.inc => with_key.php.inc} | 0 15 files changed, 10 insertions(+), 10 deletions(-) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_basic_usage.php.inc => basic_usage.php.inc} (100%) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_skip_multiple_statements.php.inc => skip_multiple_statements.php.inc} (100%) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_skip_no_boolean_initialization.php.inc => skip_no_boolean_initialization.php.inc} (100%) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_skip_no_break.php.inc => skip_no_break.php.inc} (100%) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_skip_non_array.php.inc => skip_non_array.php.inc} (100%) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_skip_variable_reuse_after_foreach.php.inc => skip_variable_reuse_after_foreach.php.inc} (100%) rename rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/{boolean_assignment_with_key.php.inc => with_key.php.inc} (100%) diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_basic_usage.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_basic_usage.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_basic_usage.php.inc index 46904ed5dfb..ba71a7e1c5d 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_basic_usage.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_basic_usage.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class BasicUsage +class EarlyReturnBasicUsage { public function checkAnimal(array $animals) { @@ -40,7 +40,7 @@ class BasicUsage namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class BasicUsage +class EarlyReturnBasicUsage { public function checkAnimal(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_missing_return_false.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_missing_return_false.php.inc index b9c9100ce15..9717f2e9b74 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_missing_return_false.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_missing_return_false.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class SkipMultipleStatements +class EarlyReturnSkipMultipleStatements { public function run(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_after_foreach.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_after_foreach.php.inc index 300f23af00c..02a564f0d9d 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_after_foreach.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_after_foreach.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class SkipMultipleStatements +class EarlyReturnSkipMultipleStatements { public function run(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_within_if_statement.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_within_if_statement.php.inc index 38be38d657d..b4e8494ca31 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_within_if_statement.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_multiple_statements_within_if_statement.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class SkipMultipleStatements +class EarlyReturnSkipMultipleStatements { public function run(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_nested_if_statements.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_nested_if_statements.php.inc index 655b172eb7e..66efe24fcce 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_nested_if_statements.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_nested_if_statements.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class SkipMultipleStatements +class EarlyReturnSkipMultipleStatements { public function run(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_non_array.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_non_array.php.inc index f4bc3a207ae..8190f7a0c6c 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_non_array.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_non_array.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class SkipNonArray +class EarlyReturnSkipNonArray { public function checkAnimal(\ArrayIterator $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_unexpected_early_return_other_than_true.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_unexpected_early_return_other_than_true.php.inc index d76fab9a7fe..d62c6aca14d 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_unexpected_early_return_other_than_true.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_skip_unexpected_early_return_other_than_true.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class SkipMultipleStatements +class EarlyReturnSkipMultipleStatements { public function run(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_with_key.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_with_key.php.inc index 87502337b20..360ae557da7 100644 --- a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_with_key.php.inc +++ b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/early_return_with_key.php.inc @@ -2,7 +2,7 @@ namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class WithKey +class EarlyReturnWithKey { public function checkAnimal(array $animals) { @@ -21,7 +21,7 @@ class WithKey namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture; -class WithKey +class EarlyReturnWithKey { public function checkAnimal(array $animals) { diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_multiple_statements.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_multiple_statements.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_no_boolean_initialization.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_no_boolean_initialization.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_no_boolean_initialization.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_no_boolean_initialization.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_no_break.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_no_break.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_no_break.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_no_break.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_non_array.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_non_array.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_non_array.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_non_array.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_variable_reuse_after_foreach.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_variable_reuse_after_foreach.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_skip_variable_reuse_after_foreach.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_variable_reuse_after_foreach.php.inc diff --git a/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_with_key.php.inc b/rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/with_key.php.inc similarity index 100% rename from rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/boolean_assignment_with_key.php.inc rename to rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/with_key.php.inc