From a55ba56944dc6c32168a1ce8cad7a63b9f24b32e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 1 Jan 2025 21:15:01 +0700 Subject: [PATCH 1/4] [Downgradephp82] Skip in ternary on DowngradeIteratorCountToArrayRector --- .../Fixture/skip_in_ternary.php.inc | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc diff --git a/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc new file mode 100644 index 00000000..dcd923c6 --- /dev/null +++ b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc @@ -0,0 +1,11 @@ + Date: Wed, 1 Jan 2025 21:30:03 +0700 Subject: [PATCH 2/4] Fix --- .../Fixture/skip_in_ternary.php.inc | 5 ++++ .../DowngradeIteratorCountToArrayRector.php | 24 +++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc index dcd923c6..e36ee42b 100644 --- a/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc +++ b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_in_ternary.php.inc @@ -8,4 +8,9 @@ final class SkipInternary { return \is_array($taggedServices) ? $taggedServices : \iterator_to_array($taggedServices); } + + public function run2($taggedServices) + { + return ! \is_array($taggedServices) ? \iterator_to_array($taggedServices): $taggedServices; + } } diff --git a/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php b/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php index 25c45763..75898f64 100644 --- a/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php +++ b/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php @@ -10,8 +10,10 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\Ternary; use PhpParser\Node\Name\FullyQualified; +use PhpParser\NodeTraverser; use PHPStan\Type\Type; use Rector\NodeAnalyzer\ArgsAnalyzer; +use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -26,7 +28,8 @@ final class DowngradeIteratorCountToArrayRector extends AbstractRector { public function __construct( - private readonly ArgsAnalyzer $argsAnalyzer + private readonly ArgsAnalyzer $argsAnalyzer, + private readonly BetterNodeFinder $betterNodeFinder ) { } @@ -35,7 +38,7 @@ public function __construct( */ public function getNodeTypes(): array { - return [FuncCall::class]; + return [Ternary::class, FuncCall::class]; } public function getRuleDefinition(): RuleDefinition @@ -63,10 +66,23 @@ function test(array|Traversable $data) { } /** - * @param FuncCall $node + * @param Ternary|FuncCall $node */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): null|FuncCall|int { + if ($node instanceof Ternary) { + $hasIsArrayCheck = (bool) $this->betterNodeFinder->findFirst( + $node, + fn (Node $subNode): bool => $subNode instanceof FuncCall && $this->isName($subNode, 'is_array') + ); + + if ($hasIsArrayCheck) { + return NodeTraverser::DONT_TRAVERSE_CHILDREN; + } + + return null; + } + if (! $this->isNames($node, ['iterator_count', 'iterator_to_array'])) { return null; } From df9de66d96a2248f48b46a971e343a91a1456ff3 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 1 Jan 2025 21:33:37 +0700 Subject: [PATCH 3/4] skip nullable object --- .../Fixture/skip_nullable_traversable.php.inc | 12 ++++++++++++ .../FuncCall/DowngradeIteratorCountToArrayRector.php | 2 ++ 2 files changed, 14 insertions(+) create mode 100644 rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_nullable_traversable.php.inc diff --git a/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_nullable_traversable.php.inc b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_nullable_traversable.php.inc new file mode 100644 index 00000000..b91204f1 --- /dev/null +++ b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/skip_nullable_traversable.php.inc @@ -0,0 +1,12 @@ +isObject() ->yes(); } From 8b06c441db3c33f7396ab807acb836800eeb85aa Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 1 Jan 2025 14:34:31 +0000 Subject: [PATCH 4/4] [ci-review] Rector Rectify --- .../Rector/FuncCall/DowngradeIteratorCountToArrayRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php b/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php index 7e0dfe63..1f02c07a 100644 --- a/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php +++ b/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php @@ -12,13 +12,13 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\NodeTraverser; use PHPStan\Type\Type; +use PHPStan\Type\TypeCombinator; use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use Webmozart\Assert\Assert; -use PHPStan\Type\TypeCombinator; /** * @changelog https://www.php.net/manual/en/migration82.other-changes.php#migration82.other-changes.functions.spl