Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -25,6 +26,7 @@
ForeachToArrayFindKeyRector::class,
ForeachToArrayAllRector::class,
ForeachToArrayAnyRector::class,
ForeachWithReturnToArrayAnyRector::class,
]
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class BasicUsage
{
public function checkAnimal(array $animals)
{
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
return true;
}
}
return false;
}

public function checkNumber(array $numbers)
{
foreach ($numbers as $number) {
if ($number > 10) {
return true;
}
}
return false;
}

public function checkWithKey(array $items)
{
foreach ($items as $key => $value) {
if ($value === 'target') {
return true;
}
}
return false;
}
}
?>
-----
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class BasicUsage
{
public function checkAnimal(array $animals)
{
return array_any($animals, fn($animal) => 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');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipMultipleStatements
{
public function run(array $animals)
{
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
return true;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipMultipleStatements
{
public function run(array $animals)
{
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
return true;
}
}
echo 'ok';
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipMultipleStatements
{
public function run(array $animals)
{
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
echo 'Found: ' . $animal;
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipMultipleStatements
{
public function run(array $animals)
{
foreach ($animals as $animal) {
if(str_starts_with($animal, 'foo')) {
if (str_starts_with($animal, 'c')) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipNonArray
{
public function checkAnimal(\ArrayIterator $animals)
{
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipMultipleStatements
{
public function run(array $animals)
{
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
return 'ok';
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class SkipVariableReUseAfterForeach
{
public function checkAnimal(array $animals)
{
$found = false;
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
$found = true;
break;
}
}

if (isset($animal)) {
echo 'hit';
}

return $found;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class WithKey
{
public function checkAnimal(array $animals)
{
foreach ($animals as $key => $animal) {
if (str_starts_with($animal, 'c') && $key === 1) {
return true;
}
}
return false;
}
}

?>
-----
<?php

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector\Fixture;

class WithKey
{
public function checkAnimal(array $animals)
{
return array_any($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key === 1);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ForeachWithReturnToArrayAnyRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php84\Rector\Foreach_\ForeachWithReturnToArrayAnyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ForeachWithReturnToArrayAnyRector::class);
};
2 changes: 1 addition & 1 deletion rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class ForeachToArrayAnyRector extends AbstractRector implements MinPhpVers
public function __construct(
private readonly ValueResolver $valueResolver,
private readonly StmtsManipulator $stmtsManipulator,
private readonly ForeachKeyUsedInConditionalAnalyzer $foreachKeyUsedInConditionalAnalyzer
private readonly ForeachKeyUsedInConditionalAnalyzer $foreachKeyUsedInConditionalAnalyzer,
) {
}

Expand Down
Loading
Loading