Skip to content

Commit 25f099f

Browse files
[Php84] Allow on return on ForeachToArrayAnyRector (#7119)
* 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 * fix * Fix * Fix * fix * Combine array any patterns * Delete separate array any rules * Rename ArrayAny rule * delete test * rename tests
1 parent 3dd70ad commit 25f099f

15 files changed

+321
-12
lines changed

rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/basic_usage.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ class BasicUsage
6868
}
6969
}
7070

71-
?>
71+
?>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnBasicUsage
6+
{
7+
public function checkAnimal(array $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if (str_starts_with($animal, 'c')) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
17+
public function checkNumber(array $numbers)
18+
{
19+
foreach ($numbers as $number) {
20+
if ($number > 10) {
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
public function checkWithKey(array $items)
28+
{
29+
foreach ($items as $key => $value) {
30+
if ($value === 'target') {
31+
return true;
32+
}
33+
}
34+
return false;
35+
}
36+
}
37+
?>
38+
-----
39+
<?php
40+
41+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
42+
43+
class EarlyReturnBasicUsage
44+
{
45+
public function checkAnimal(array $animals)
46+
{
47+
return array_any($animals, fn($animal) => str_starts_with($animal, 'c'));
48+
}
49+
50+
public function checkNumber(array $numbers)
51+
{
52+
return array_any($numbers, fn($number) => $number > 10);
53+
}
54+
55+
public function checkWithKey(array $items)
56+
{
57+
return array_any($items, fn($value) => $value === 'target');
58+
}
59+
}
60+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnSkipMultipleStatements
6+
{
7+
public function run(array $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if (str_starts_with($animal, 'c')) {
11+
return true;
12+
}
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnSkipMultipleStatements
6+
{
7+
public function run(array $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if (str_starts_with($animal, 'c')) {
11+
return true;
12+
}
13+
}
14+
echo 'ok';
15+
return false;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnSkipMultipleStatements
6+
{
7+
public function run(array $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if (str_starts_with($animal, 'c')) {
11+
echo 'Found: ' . $animal;
12+
return true;
13+
}
14+
}
15+
return false;
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnSkipMultipleStatements
6+
{
7+
public function run(array $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if(str_starts_with($animal, 'foo')) {
11+
if (str_starts_with($animal, 'c')) {
12+
return true;
13+
}
14+
}
15+
}
16+
return false;
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnSkipNonArray
6+
{
7+
public function checkAnimal(\ArrayIterator $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if (str_starts_with($animal, 'c')) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnSkipMultipleStatements
6+
{
7+
public function run(array $animals)
8+
{
9+
foreach ($animals as $animal) {
10+
if (str_starts_with($animal, 'c')) {
11+
return 'ok';
12+
}
13+
}
14+
return false;
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class EarlyReturnWithKey
6+
{
7+
public function checkAnimal(array $animals)
8+
{
9+
foreach ($animals as $key => $animal) {
10+
if (str_starts_with($animal, 'c') && $key === 1) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
23+
24+
class EarlyReturnWithKey
25+
{
26+
public function checkAnimal(array $animals)
27+
{
28+
return array_any($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key === 1);
29+
}
30+
}
31+
32+
?>

rules-tests/Php84/Rector/Foreach_/ForeachToArrayAnyRector/Fixture/skip_multiple_statements.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class SkipMultipleStatements
1616
}
1717
return $found;
1818
}
19-
}
19+
}

0 commit comments

Comments
 (0)