Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/downgrade-php84.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Rector\Config\RectorConfig;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
use Rector\ValueObject\PhpVersion;
Expand All @@ -16,5 +17,6 @@
DowngradeRoundingModeEnumRector::class,
DowngradeArrayAllRector::class,
DowngradeArrayAnyRector::class,
DowngradeArrayFindRector::class,
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Fixture
{
public function run(array $animals)
{
$found = array_all($animals, fn($animal) => str_starts_with($animal, 'c'));
$found = array_any($animals, fn($animal) => str_starts_with($animal, 'c'));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class WithKey
{
public function run(array $animals)
{
$found = array_all($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0);
$found = array_any($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0);
}
}

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector;

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

final class DowngradeArrayFindRectorTest 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,33 @@
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector\Fixture;

class Fixture
{
public function run(array $animals)
{
$found = array_find($animals, fn($animal) => str_starts_with($animal, 'c'));
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector\Fixture;

class Fixture
{
public function run(array $animals)
{
$found = null;
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
$found = $animal;
break;
}
}
}
}

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

namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector\Fixture;

class WithKey
{
public function run(array $animals)
{
$found = array_find($animals, fn($animal, $key) => str_starts_with($animal, 'c') && $key > 0);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector\Fixture;

class WithKey
{
public function run(array $animals)
{
$found = null;
foreach ($animals as $key => $animal) {
if (str_starts_with($animal, 'c') && $key > 0) {
$found = $animal;
break;
}
}
}
}

?>
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\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeArrayFindRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function refactor(Node $node): ?array
return null;
}

if (! $this->isName($node->expr->expr, 'array_all')) {
if (! $this->isName($node->expr->expr, 'array_any')) {
return null;
}

Expand Down
116 changes: 116 additions & 0 deletions rules/DowngradePhp84/Rector/Expression/DowngradeArrayFindRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp84\Rector\Expression;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://php.watch/versions/8.4/array_find-array_find_key-array_any-array_all
*
* @see \Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayFindRector\DowngradeArrayFindRectorTest
*/
final class DowngradeArrayFindRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [Expression::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Downgrade array_find() to foreach loop',
[
new CodeSample(
<<<'CODE_SAMPLE'
$found = array_find($animals, fn($animal) => str_starts_with($animal, 'c'));
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$found = null;
foreach ($animals as $animal) {
if (str_starts_with($animal, 'c')) {
$found = $animal;
break;
}
}
CODE_SAMPLE
),
]
);
}

/**
* @param Expression $node
* @return Stmt[]|null
*/
public function refactor(Node $node): ?array
{
if (! $node->expr instanceof Assign) {
return null;
}

if (! $node->expr->expr instanceof FuncCall) {
return null;
}

if (! $this->isName($node->expr->expr, 'array_find')) {
return null;
}

if ($node->expr->expr->isFirstClassCallable()) {
return null;
}

$args = $node->expr->expr->getArgs();
if (count($args) !== 2) {
return null;
}

if (! $args[1]->value instanceof ArrowFunction) {
return null;
}

$valueCond = $args[1]->value->expr;
$if = new If_($valueCond, [
'stmts' => [
new Expression(new Assign($node->expr->var, $args[1]->value->params[0]->var)),
new Break_(),
],
]);

return [
// init
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('null')))),

// foreach loop
new Foreach_(
$args[0]->value,
$args[1]->value->params[0]->var,
isset($args[1]->value->params[1]->var)
? [
'keyVar' => $args[1]->value->params[1]->var,
'stmts' => [$if],
]
: [
'stmts' => [$if],
],
),
];
}
}