Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;

function SomeFunctionWithDefaultNullArg(?string $someClass = null)
{
}

SomeFunctionWithDefaultNullArg(null);

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;

function SomeFunctionWithDefaultNullArg(?string $someClass = null)
{
}

SomeFunctionWithDefaultNullArg();

?>
22 changes: 13 additions & 9 deletions rules/DeadCode/NodeAnalyzer/CallLikeParamDefaultResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down