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 @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DowngradePhp84\Rector\Expression\DowngradeArrayAllRector;
use Rector\DowngradePhp84\Rector\FuncCall\DowngradeRoundingModeEnumRector;
use Rector\DowngradePhp84\Rector\MethodCall\DowngradeNewMethodCallWithoutParenthesesRector;
use Rector\ValueObject\PhpVersion;
Expand All @@ -12,5 +13,6 @@
$rectorConfig->rules([
DowngradeNewMethodCallWithoutParenthesesRector::class,
DowngradeRoundingModeEnumRector::class,
DowngradeArrayAllRector::class,
]);
};
4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
->withPhpSets(php82: true)
->withSets([PHPUnitSetList::PHPUNIT_100, PHPUnitSetList::PHPUNIT_CODE_QUALITY])
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
deadCode: true,
typeDeclarations: true,
privatization: true,
naming: true,
typeDeclarations: true,
earlyReturn: true,
rectorPreset: true
)->withPaths([
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\DowngradeArrayAllRector;

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

final class DowngradeArrayAllRectorTest 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\DowngradeArrayAllRector\Fixture;

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

?>
-----
<?php

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

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

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

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

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

?>
-----
<?php

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

class WithKey
{
public function run(array $animals)
{
$found = true;
foreach ($animals as $key => $animal) {
if (!(str_starts_with($animal, 'c') && $key > 0)) {
$found = false;
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\DowngradeArrayAllRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeArrayAllRector::class);
};
120 changes: 120 additions & 0 deletions rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?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\BooleanNot;
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\NodeTypeResolver\Node\AttributeKey;
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\DowngradeArrayAllRector\DowngradeArrayAllRectorTest
*/
final class DowngradeArrayAllRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [Expression::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Downgrade array_all() to foreach loop',
[
new CodeSample(
<<<'CODE_SAMPLE'
$found = array_all($animals, fn($animal) => str_starts_with($animal, 'c'));
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$found = true;
foreach ($animals as $animal) {
if (!str_starts_with($animal, 'c')) {
$found = false;
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_all')) {
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;
$valueCond->setAttribute(AttributeKey::ORIGINAL_NODE, null);

$if = new If_(new BooleanNot($valueCond), [
'stmts' => [
new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))),
new Break_(),
],
]);

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

// 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],
],
),
];
}
}