|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\DowngradePhp84\Rector\Expression; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\ArrowFunction; |
| 9 | +use PhpParser\Node\Expr\Assign; |
| 10 | +use PhpParser\Node\Expr\ConstFetch; |
| 11 | +use PhpParser\Node\Expr\FuncCall; |
| 12 | +use PhpParser\Node\Name; |
| 13 | +use PhpParser\Node\Stmt; |
| 14 | +use PhpParser\Node\Stmt\Break_; |
| 15 | +use PhpParser\Node\Stmt\Expression; |
| 16 | +use PhpParser\Node\Stmt\Foreach_; |
| 17 | +use PhpParser\Node\Stmt\If_; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 21 | + |
| 22 | +/** |
| 23 | + * @changelog https://php.watch/versions/8.4/array_find-array_find_key-array_any-array_all |
| 24 | + * |
| 25 | + * @see \Rector\Tests\DowngradePhp84\Rector\Expression\DowngradeArrayAnyRector\DowngradeArrayAnyRectorTest |
| 26 | + */ |
| 27 | +final class DowngradeArrayAnyRector extends AbstractRector |
| 28 | +{ |
| 29 | + public function getNodeTypes(): array |
| 30 | + { |
| 31 | + return [Expression::class]; |
| 32 | + } |
| 33 | + |
| 34 | + public function getRuleDefinition(): RuleDefinition |
| 35 | + { |
| 36 | + return new RuleDefinition( |
| 37 | + 'Downgrade array_any() to foreach loop', |
| 38 | + [ |
| 39 | + new CodeSample( |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +$found = array_any($animals, fn($animal) => str_starts_with($animal, 'c')); |
| 42 | +CODE_SAMPLE |
| 43 | + , |
| 44 | + <<<'CODE_SAMPLE' |
| 45 | +$found = false; |
| 46 | +foreach ($animals as $animal) { |
| 47 | + if (str_starts_with($animal, 'c')) { |
| 48 | + $found = true; |
| 49 | + break; |
| 50 | + } |
| 51 | +} |
| 52 | +CODE_SAMPLE |
| 53 | + ), |
| 54 | + ] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param Expression $node |
| 60 | + * @return Stmt[]|null |
| 61 | + */ |
| 62 | + public function refactor(Node $node): ?array |
| 63 | + { |
| 64 | + if (! $node->expr instanceof Assign) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + if (! $node->expr->expr instanceof FuncCall) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + if (! $this->isName($node->expr->expr, 'array_all')) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + if ($node->expr->expr->isFirstClassCallable()) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + $args = $node->expr->expr->getArgs(); |
| 81 | + if (count($args) !== 2) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (! $args[1]->value instanceof ArrowFunction) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + $valueCond = $args[1]->value->expr; |
| 90 | + $if = new If_($valueCond, [ |
| 91 | + 'stmts' => [ |
| 92 | + new Expression(new Assign($node->expr->var, new ConstFetch(new Name('true')))), |
| 93 | + new Break_(), |
| 94 | + ], |
| 95 | + ]); |
| 96 | + |
| 97 | + return [ |
| 98 | + // init |
| 99 | + new Expression(new Assign($node->expr->var, new ConstFetch(new Name('false')))), |
| 100 | + |
| 101 | + // foreach loop |
| 102 | + new Foreach_( |
| 103 | + $args[0]->value, |
| 104 | + $args[1]->value->params[0]->var, |
| 105 | + isset($args[1]->value->params[1]->var) |
| 106 | + ? [ |
| 107 | + 'keyVar' => $args[1]->value->params[1]->var, |
| 108 | + 'stmts' => [$if], |
| 109 | + ] |
| 110 | + : [ |
| 111 | + 'stmts' => [$if], |
| 112 | + ], |
| 113 | + ), |
| 114 | + ]; |
| 115 | + } |
| 116 | +} |
0 commit comments