diff --git a/rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector/Fixture/remove_from_unknown_type.php.inc b/rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector/Fixture/remove_from_unknown_type.php.inc new file mode 100644 index 00000000..8a9beb58 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector/Fixture/remove_from_unknown_type.php.inc @@ -0,0 +1,25 @@ +resolve($abstract, $parameters, raiseEvents: \true); + } +} + +?> +----- +resolve($abstract, $parameters, \true); + } +} + +?> diff --git a/rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php b/rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php index a3eaef83..0b3013dc 100644 --- a/rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php +++ b/rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Expr\StaticCall; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\MethodReflection; +use PHPStan\Type\MixedType; use Rector\DowngradePhp80\NodeAnalyzer\UnnamedArgumentResolver; use Rector\NodeAnalyzer\ArgsAnalyzer; use Rector\Rector\AbstractRector; @@ -83,7 +84,7 @@ private function execute($a = null, $b = null) public function refactor(Node $node): ?Node { $args = $node->getArgs(); - if ($this->shouldSkip($args)) { + if (! $this->argsAnalyzer->hasNamedArg($args)) { return null; } @@ -102,6 +103,21 @@ private function removeNamedArguments(MethodCall | StaticCall | New_ | FuncCall } if (! $functionLikeReflection instanceof MethodReflection && ! $functionLikeReflection instanceof FunctionReflection) { + // remove leftovers in case of unknown type, to avoid crashing on unknown syntax + if ($node instanceof MethodCall) { + $callerType = $this->getType($node->var); + + if ($callerType instanceof MixedType) { + foreach ($node->getArgs() as $arg) { + if ($arg->name instanceof Node) { + $arg->name = null; + } + } + + return $node; + } + } + return null; } @@ -109,12 +125,4 @@ private function removeNamedArguments(MethodCall | StaticCall | New_ | FuncCall return $node; } - - /** - * @param mixed[]|Arg[] $args - */ - private function shouldSkip(array $args): bool - { - return ! $this->argsAnalyzer->hasNamedArg($args); - } }