diff --git a/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/with_named_arg.php.inc b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/with_named_arg.php.inc new file mode 100644 index 00000000..ec24bc47 --- /dev/null +++ b/rules-tests/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector/Fixture/with_named_arg.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php b/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php index 00970dfb..61cd95ea 100644 --- a/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php +++ b/rules/DowngradePhp82/Rector/FuncCall/DowngradeIteratorCountToArrayRector.php @@ -92,24 +92,22 @@ public function refactor(Node $node): null|FuncCall|int return null; } - $args = $node->getArgs(); - if ($this->argsAnalyzer->hasNamedArg($args)) { + $arg = $node->getArg('iterator', 0); + if (! $arg instanceof Arg) { return null; } - if (! isset($args[0])) { - return null; - } - - $type = $this->nodeTypeResolver->getType($args[0]->value); + $type = $this->nodeTypeResolver->getType($arg->value); if ($this->shouldSkip($type)) { return null; } - Assert::isInstanceOf($node->args[0], Arg::class); + $position = $this->argsAnalyzer->resolveArgPosition($node->getArgs(), 'iterator', 0); + + Assert::isInstanceOf($node->args[$position], Arg::class); - $firstValue = $node->args[0]->value; - $node->args[0]->value = new Ternary( + $firstValue = $node->args[$position]->value; + $node->args[$position]->value = new Ternary( $this->nodeFactory->createFuncCall('is_array', [new Arg($firstValue)]), new New_(new FullyQualified('ArrayIterator'), [new Arg($firstValue)]), $firstValue