diff --git a/config/set/downgrade-php84.php b/config/set/downgrade-php84.php index d3d914cc..4f943dea 100644 --- a/config/set/downgrade-php84.php +++ b/config/set/downgrade-php84.php @@ -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; @@ -12,5 +13,6 @@ $rectorConfig->rules([ DowngradeNewMethodCallWithoutParenthesesRector::class, DowngradeRoundingModeEnumRector::class, + DowngradeArrayAllRector::class, ]); }; diff --git a/rector.php b/rector.php index 2dd42da1..1f6fe9ad 100644 --- a/rector.php +++ b/rector.php @@ -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([ diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.php new file mode 100644 index 00000000..10c3a076 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/DowngradeArrayAllRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..29580082 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/fixture.php.inc @@ -0,0 +1,33 @@ + str_starts_with($animal, 'c')); + } +} + +?> +----- + diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/with_key.php.inc b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/with_key.php.inc new file mode 100644 index 00000000..8869e4a5 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/Fixture/with_key.php.inc @@ -0,0 +1,33 @@ + str_starts_with($animal, 'c') && $key > 0); + } +} + +?> +----- + $animal) { + if (!(str_starts_with($animal, 'c') && $key > 0)) { + $found = false; + break; + } + } + } +} + +?> diff --git a/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php new file mode 100644 index 00000000..00fe21e0 --- /dev/null +++ b/rules-tests/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector/config/configured_rule.php @@ -0,0 +1,10 @@ +rule(DowngradeArrayAllRector::class); +}; diff --git a/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php new file mode 100644 index 00000000..2df6cbf3 --- /dev/null +++ b/rules/DowngradePhp84/Rector/Expression/DowngradeArrayAllRector.php @@ -0,0 +1,120 @@ + 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], + ], + ), + ]; + } +}