Skip to content

Commit 4b31180

Browse files
authored
Add support for unknown types in DowngradeNamedArgumentRector (#289)
* add fixture * remove unclear type named args
1 parent 1454a52 commit 4b31180

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
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\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
4+
5+
final class RemoveFromUnknownType
6+
{
7+
function someMethod($container, $abstract, $parameters ) {
8+
return $container->resolve($abstract, $parameters, raiseEvents: \true);
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\DowngradePhp80\Rector\MethodCall\DowngradeNamedArgumentRector\Fixture;
17+
18+
final class RemoveFromUnknownType
19+
{
20+
function someMethod($container, $abstract, $parameters ) {
21+
return $container->resolve($abstract, $parameters, \true);
22+
}
23+
}
24+
25+
?>

rules/DowngradePhp80/Rector/MethodCall/DowngradeNamedArgumentRector.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpParser\Node\Expr\StaticCall;
1313
use PHPStan\Reflection\FunctionReflection;
1414
use PHPStan\Reflection\MethodReflection;
15+
use PHPStan\Type\MixedType;
1516
use Rector\DowngradePhp80\NodeAnalyzer\UnnamedArgumentResolver;
1617
use Rector\NodeAnalyzer\ArgsAnalyzer;
1718
use Rector\Rector\AbstractRector;
@@ -83,7 +84,7 @@ private function execute($a = null, $b = null)
8384
public function refactor(Node $node): ?Node
8485
{
8586
$args = $node->getArgs();
86-
if ($this->shouldSkip($args)) {
87+
if (! $this->argsAnalyzer->hasNamedArg($args)) {
8788
return null;
8889
}
8990

@@ -102,19 +103,26 @@ private function removeNamedArguments(MethodCall | StaticCall | New_ | FuncCall
102103
}
103104

104105
if (! $functionLikeReflection instanceof MethodReflection && ! $functionLikeReflection instanceof FunctionReflection) {
106+
// remove leftovers in case of unknown type, to avoid crashing on unknown syntax
107+
if ($node instanceof MethodCall) {
108+
$callerType = $this->getType($node->var);
109+
110+
if ($callerType instanceof MixedType) {
111+
foreach ($node->getArgs() as $arg) {
112+
if ($arg->name instanceof Node) {
113+
$arg->name = null;
114+
}
115+
}
116+
117+
return $node;
118+
}
119+
}
120+
105121
return null;
106122
}
107123

108124
$node->args = $this->unnamedArgumentResolver->resolveFromReflection($functionLikeReflection, $args);
109125

110126
return $node;
111127
}
112-
113-
/**
114-
* @param mixed[]|Arg[] $args
115-
*/
116-
private function shouldSkip(array $args): bool
117-
{
118-
return ! $this->argsAnalyzer->hasNamedArg($args);
119-
}
120128
}

0 commit comments

Comments
 (0)