Skip to content

Commit 3380a5e

Browse files
authored
Skip iterator_to_array()/iterator_count() already guarded by is_array() on DowngradeIteratorCountToArrayRector (#415)
1 parent 2fa8bab commit 3380a5e

4 files changed

Lines changed: 132 additions & 1 deletion

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ parameters:
117117
- rules/DowngradePhp74/Rector/MethodCall/DowngradeReflectionGetTypeRector.php
118118
- rules/DowngradePhp80/Rector/MethodCall/DowngradeReflectionGetAttributesRector.php
119119
- rules/DowngradePhp81/Rector/MethodCall/DowngradeIsEnumRector.php
120+
- rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php
120121
- rules/DowngradePhp73/Rector/String_/DowngradeFlexibleHeredocSyntaxRector.php
121122
- rules/DowngradePhp80/Rector/Expression/DowngradeMatchToSwitchRector.php
122123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
4+
5+
final class IsArrayGuardOnOtherExpr
6+
{
7+
public function run($data, $other)
8+
{
9+
return is_array($other) ? $other : iterator_to_array($data);
10+
}
11+
12+
public function run2($data)
13+
{
14+
return is_array($data) ? iterator_to_array($data) : [];
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
23+
24+
final class IsArrayGuardOnOtherExpr
25+
{
26+
public function run($data, $other)
27+
{
28+
return is_array($other) ? $other : iterator_to_array(is_array($data) ? new \ArrayIterator($data) : $data);
29+
}
30+
31+
public function run2($data)
32+
{
33+
return is_array($data) ? iterator_to_array(is_array($data) ? new \ArrayIterator($data) : $data) : [];
34+
}
35+
}
36+
37+
?>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
4+
5+
final class SkipIsArrayGuarded
6+
{
7+
private $data;
8+
9+
public function run($data)
10+
{
11+
return is_array($data) ? $data : iterator_to_array($data);
12+
}
13+
14+
public function run2($data)
15+
{
16+
return ! is_array($data) ? iterator_count($data) : count($data);
17+
}
18+
19+
public function run3()
20+
{
21+
return array_merge(is_array($this->data) ? $this->data : iterator_to_array($this->data), []);
22+
}
23+
24+
public function run4(SomeUnknownClass $object)
25+
{
26+
return array_merge(is_array($object->getItems()) ? $object->getItems() : iterator_to_array($object->getItems()), []);
27+
}
28+
29+
public function run5()
30+
{
31+
return is_array($this->getItems()) ? $this->getItems() : iterator_to_array($this->getItems());
32+
}
33+
34+
private function getItems()
35+
{
36+
return [];
37+
}
38+
}

‎rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
99
use PhpParser\Node\Expr;
10+
use PhpParser\Node\Expr\BooleanNot;
1011
use PhpParser\Node\Expr\FuncCall;
1112
use PhpParser\Node\Expr\New_;
1213
use PhpParser\Node\Expr\Ternary;
@@ -27,6 +28,8 @@
2728
*/
2829
final class DowngradeIteratorCountToArrayRector extends AbstractRector
2930
{
31+
private const string SKIP_NODE = 'skip_node';
32+
3033
public function __construct(
3134
private readonly ArgsAnalyzer $argsAnalyzer,
3235
) {
@@ -65,10 +68,19 @@ function test(array|Traversable $data) {
6568
}
6669

6770
/**
68-
* @param FuncCall $node
71+
* @param Ternary|FuncCall $node
6972
*/
7073
public function refactor(Node $node): null|FuncCall
7174
{
75+
if ($node instanceof Ternary) {
76+
$this->markIsArrayGuardedCall($node);
77+
return null;
78+
}
79+
80+
if ($node->getAttribute(self::SKIP_NODE) === true) {
81+
return null;
82+
}
83+
7284
if (! $this->isNames($node, ['iterator_count', 'iterator_to_array'])) {
7385
return null;
7486
}
@@ -107,6 +119,43 @@ public function refactor(Node $node): null|FuncCall
107119
return $node;
108120
}
109121

122+
/**
123+
* Skip the not-array branch of an is_array() check on the same expr, e.g.
124+
* is_array($data) ? $data : iterator_to_array($data)
125+
*/
126+
private function markIsArrayGuardedCall(Ternary $ternary): void
127+
{
128+
$cond = $ternary->cond;
129+
$notArrayBranch = $ternary->else;
130+
131+
if ($cond instanceof BooleanNot) {
132+
$cond = $cond->expr;
133+
$notArrayBranch = $ternary->if;
134+
}
135+
136+
if (! $cond instanceof FuncCall || ! $this->isName($cond, 'is_array') || $cond->isFirstClassCallable()) {
137+
return;
138+
}
139+
140+
if (! $notArrayBranch instanceof FuncCall || $notArrayBranch->isFirstClassCallable()) {
141+
return;
142+
}
143+
144+
if (! $this->isNames($notArrayBranch, ['iterator_count', 'iterator_to_array'])) {
145+
return;
146+
}
147+
148+
$checkedArg = $cond->getArg('value', 0);
149+
$iteratorArg = $notArrayBranch->getArg('iterator', 0);
150+
if (! $checkedArg instanceof Arg || ! $iteratorArg instanceof Arg) {
151+
return;
152+
}
153+
154+
if ($this->nodeComparator->areNodesEqual($checkedArg->value, $iteratorArg->value)) {
155+
$notArrayBranch->setAttribute(self::SKIP_NODE, true);
156+
}
157+
}
158+
110159
private function isArrayIteratorWrapped(Expr $expr): bool
111160
{
112161
if (! $expr instanceof Ternary) {
@@ -130,6 +179,12 @@ private function shouldSkip(Type $type): bool
130179
return false;
131180
}
132181

182+
// never an array, e.g. narrowed by is_array($data) ? $data : iterator_to_array($data),
183+
// so the is_array() check would always be false
184+
if ($type->isArray()->no()) {
185+
return true;
186+
}
187+
133188
$type = TypeCombinator::removeNull($type);
134189
return $type->isObject()
135190
->yes();

0 commit comments

Comments
 (0)