Replace foreach with assignment and break with array_find_key
$animals = ['dog', 'cat', 'cow', 'duck', 'goose'];
-$found = null;
-foreach ($animals as $idx => $animal) {
- if (str_starts_with($animal, 'c')) {
- $found = $idx;
- break;
- }
-}
+$found = array_find_key($animals, fn($animal) => str_starts_with($animal, 'c'));
Configure your rector.php
:
<?php
use Rector\Config\RectorConfig;
use Rector\Php84\Rector\Foreach_\ForeachToArrayFindKeyRector;
return RectorConfig::configure()
->withRules([
ForeachToArrayFindKeyRector::class,
]);