diff --git a/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/with_function_call.php.inc b/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/with_function_call.php.inc new file mode 100644 index 00000000000..c8c068ea12f --- /dev/null +++ b/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/with_function_call.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php b/rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php index 47f7a23a51b..c10095efc36 100644 --- a/rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php +++ b/rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php @@ -4,9 +4,11 @@ namespace Rector\DeadCode\NodeAnalyzer; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; +use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\NullType; @@ -22,16 +24,16 @@ public function __construct( /** * @return int[] */ - public function resolveNullPositions(MethodCall|StaticCall|New_ $callLike): array + public function resolveNullPositions(MethodCall|StaticCall|New_|FuncCall $callLike): array { - $methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); - if (! $methodReflection instanceof MethodReflection) { + $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); + if (! $reflection instanceof MethodReflection && ! $reflection instanceof FunctionReflection) { return []; } $nullPositions = []; - $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants()); + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($reflection->getVariants()); foreach ($extendedParametersAcceptor->getParameters() as $position => $extendedParameterReflection) { if (! $extendedParameterReflection->getDefaultValue() instanceof NullType) { continue; @@ -43,14 +45,16 @@ public function resolveNullPositions(MethodCall|StaticCall|New_ $callLike): arra return $nullPositions; } - public function resolvePositionParameterByName(MethodCall|StaticCall|New_ $callLike, string $parameterName): ?int - { - $methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); - if (! $methodReflection instanceof MethodReflection) { + public function resolvePositionParameterByName( + MethodCall|StaticCall|New_|FuncCall $callLike, + string $parameterName + ): ?int { + $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); + if (! $reflection instanceof MethodReflection && ! $reflection instanceof FunctionReflection) { return null; } - $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants()); + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($reflection->getVariants()); foreach ($extendedParametersAcceptor->getParameters() as $position => $extendedParameterReflection) { if ($extendedParameterReflection->getName() === $parameterName) { return $position; diff --git a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php index f8cc84bd9d3..568a1063eb1 100644 --- a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php +++ b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\MethodCall; use PhpParser\Node; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; @@ -71,13 +72,13 @@ public function execute(?SomeClass $someClass = null) public function getNodeTypes(): array { - return [MethodCall::class, StaticCall::class, New_::class]; + return [MethodCall::class, StaticCall::class, New_::class, FuncCall::class]; } /** - * @param MethodCall|StaticCall|New_ $node + * @param MethodCall|StaticCall|New_|FuncCall $node */ - public function refactor(Node $node): StaticCall|MethodCall|New_|null + public function refactor(Node $node): StaticCall|MethodCall|New_|FuncCall|null { if ($node->isFirstClassCallable()) { return null; @@ -87,8 +88,12 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null return null; } - $hasChanged = false; + $nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node); + if ($nullPositions === []) { + return null; + } + $hasChanged = false; $args = $node->getArgs(); $lastArgPosition = count($args) - 1; for ($position = $lastArgPosition; $position >= 0; --$position) { @@ -114,8 +119,7 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null break; } - $nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node); - if (! in_array($position, $nullPositions)) { + if (! in_array($position, $nullPositions, true)) { break; }