Skip to content

Commit 7730326

Browse files
authored
[DowngradePhp82] Allow with named argument on DowngradeIteratorCountToArrayRector (#334)
1 parent 393a24a commit 7730326

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
4+
5+
final class WithNamedArg
6+
{
7+
function test(array|\Traversable $data) {
8+
$v = iterator_to_array(preserve_keys: false, iterator: $data);
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp82\Rector\FuncCall\DowngradeIteratorCountToArrayRector\Fixture;
17+
18+
final class WithNamedArg
19+
{
20+
function test(array|\Traversable $data) {
21+
$v = iterator_to_array(preserve_keys: false, iterator: is_array($data) ? new \ArrayIterator($data) : $data);
22+
}
23+
}
24+
25+
?>

rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,22 @@ public function refactor(Node $node): null|FuncCall|int
9292
return null;
9393
}
9494

95-
$args = $node->getArgs();
96-
if ($this->argsAnalyzer->hasNamedArg($args)) {
95+
$arg = $node->getArg('iterator', 0);
96+
if (! $arg instanceof Arg) {
9797
return null;
9898
}
9999

100-
if (! isset($args[0])) {
101-
return null;
102-
}
103-
104-
$type = $this->nodeTypeResolver->getType($args[0]->value);
100+
$type = $this->nodeTypeResolver->getType($arg->value);
105101
if ($this->shouldSkip($type)) {
106102
return null;
107103
}
108104

109-
Assert::isInstanceOf($node->args[0], Arg::class);
105+
$position = $this->argsAnalyzer->resolveArgPosition($node->getArgs(), 'iterator', 0);
106+
107+
Assert::isInstanceOf($node->args[$position], Arg::class);
110108

111-
$firstValue = $node->args[0]->value;
112-
$node->args[0]->value = new Ternary(
109+
$firstValue = $node->args[$position]->value;
110+
$node->args[$position]->value = new Ternary(
113111
$this->nodeFactory->createFuncCall('is_array', [new Arg($firstValue)]),
114112
new New_(new FullyQualified('ArrayIterator'), [new Arg($firstValue)]),
115113
$firstValue

0 commit comments

Comments
 (0)