Skip to content

Commit f9cc5a0

Browse files
[Downgradephp82] Skip in ternary on DowngradeIteratorCountToArrayRector (#262)
* [Downgradephp82] Skip in ternary on DowngradeIteratorCountToArrayRector * Fix * skip nullable object * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 9d0a7b2 commit f9cc5a0

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
4+
5+
final class SkipInternary
6+
{
7+
public function run($taggedServices)
8+
{
9+
return \is_array($taggedServices) ? $taggedServices : \iterator_to_array($taggedServices);
10+
}
11+
12+
public function run2($taggedServices)
13+
{
14+
return ! \is_array($taggedServices) ? \iterator_to_array($taggedServices): $taggedServices;
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
4+
5+
final class SkipNullableTraversable
6+
{
7+
function test(?\Traversable $data)
8+
{
9+
$c = iterator_count($data);
10+
$v = iterator_to_array($data);
11+
}
12+
}

rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
use PhpParser\Node\Expr\New_;
1111
use PhpParser\Node\Expr\Ternary;
1212
use PhpParser\Node\Name\FullyQualified;
13+
use PhpParser\NodeTraverser;
1314
use PHPStan\Type\Type;
15+
use PHPStan\Type\TypeCombinator;
1416
use Rector\NodeAnalyzer\ArgsAnalyzer;
17+
use Rector\PhpParser\Node\BetterNodeFinder;
1518
use Rector\Rector\AbstractRector;
1619
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1720
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -26,7 +29,8 @@
2629
final class DowngradeIteratorCountToArrayRector extends AbstractRector
2730
{
2831
public function __construct(
29-
private readonly ArgsAnalyzer $argsAnalyzer
32+
private readonly ArgsAnalyzer $argsAnalyzer,
33+
private readonly BetterNodeFinder $betterNodeFinder
3034
) {
3135
}
3236

@@ -35,7 +39,7 @@ public function __construct(
3539
*/
3640
public function getNodeTypes(): array
3741
{
38-
return [FuncCall::class];
42+
return [Ternary::class, FuncCall::class];
3943
}
4044

4145
public function getRuleDefinition(): RuleDefinition
@@ -63,10 +67,23 @@ function test(array|Traversable $data) {
6367
}
6468

6569
/**
66-
* @param FuncCall $node
70+
* @param Ternary|FuncCall $node
6771
*/
68-
public function refactor(Node $node): ?Node
72+
public function refactor(Node $node): null|FuncCall|int
6973
{
74+
if ($node instanceof Ternary) {
75+
$hasIsArrayCheck = (bool) $this->betterNodeFinder->findFirst(
76+
$node,
77+
fn (Node $subNode): bool => $subNode instanceof FuncCall && $this->isName($subNode, 'is_array')
78+
);
79+
80+
if ($hasIsArrayCheck) {
81+
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
82+
}
83+
84+
return null;
85+
}
86+
7087
if (! $this->isNames($node, ['iterator_count', 'iterator_to_array'])) {
7188
return null;
7289
}
@@ -107,6 +124,7 @@ private function shouldSkip(Type $type): bool
107124
return false;
108125
}
109126

127+
$type = TypeCombinator::removeNull($type);
110128
return $type->isObject()
111129
->yes();
112130
}

0 commit comments

Comments
 (0)