Skip to content

Commit 7342cb4

Browse files
committed
add fixture to handle even different order
1 parent 053dcc8 commit 7342cb4

6 files changed

Lines changed: 55 additions & 53 deletions

File tree

.github/workflows/code_analysis.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ name: Code Analysis
22

33
on:
44
pull_request: null
5-
push: null
5+
push:
6+
branches:
7+
- "main"
68

79
env:
810
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361

config/set/named-args.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
use Rector\CodeQuality\Rector\CallLike\AddNameToNullArgumentRector;
88
use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector;
99
use Rector\Config\RectorConfig;
10-
use Rector\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector;
1110
use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;
1211
use Rector\NetteUtils\Rector\StaticCall\UtilsJsonStaticCallNamedArgRector;
1312

1413
return static function (RectorConfig $rectorConfig): void {
1514
$rectorConfig->rules([
1615
AddNameToNullArgumentRector::class,
1716
AddNameToBooleanArgumentRector::class,
18-
RemoveNullArgOnNullDefaultParamRector::class,
1917
RemoveNullNamedArgOnNullDefaultParamRector::class,
2018
SortCallLikeNamedArgsRector::class,
2119
SortAttributeNamedArgsRector::class,

rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/skip_named_args.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ function skipNamedArgs(?string $someClass = null)
88
{
99
}
1010

11-
SomeFunctionWithDefaultNullArg(someClass: null);
11+
skipNamedArgs(someClass: null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Fixture;
6+
7+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source\SomeExternalClass;
8+
9+
final class HandleNamedArgumentPositionNotMatch
10+
{
11+
public function get()
12+
{
13+
SomeExternalClass::withMiddleNotNullDefault(item: null);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Fixture;
24+
25+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source\SomeExternalClass;
26+
27+
final class HandleNamedArgumentPositionNotMatch
28+
{
29+
public function get()
30+
{
31+
SomeExternalClass::withMiddleNotNullDefault();
32+
}
33+
}
34+
35+
?>

rules-tests/DeadCode/Rector/MethodCall/RemoveNullNamedArgOnNullDefaultParamRector/Fixture/skip_named_argument_position_not_match.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.

rules/DeadCode/Rector/MethodCall/RemoveNullNamedArgOnNullDefaultParamRector.php

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|FuncCall|null
9292

9393
$hasNamedArg = false;
9494
foreach ($args as $arg) {
95+
// unpack hides which parameters are bound, so removal is not safe
96+
if ($arg->unpack) {
97+
return null;
98+
}
99+
95100
if ($arg->name instanceof Identifier) {
96101
$hasNamedArg = true;
97-
break;
98102
}
99103
}
100104

@@ -107,40 +111,9 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|FuncCall|null
107111
return null;
108112
}
109113

110-
// map every arg to its target parameter position, so named args in any order are handled
111-
$argPositionByParameterPosition = [];
112-
foreach ($args as $argPosition => $arg) {
113-
if ($arg->unpack) {
114-
return null;
115-
}
116-
117-
if ($arg->name instanceof Identifier) {
118-
$parameterPosition = $this->callLikeParamDefaultResolver->resolvePositionParameterByName(
119-
$node,
120-
$arg->name->toString()
121-
);
122-
123-
if ($parameterPosition === null) {
124-
return null;
125-
}
126-
} else {
127-
$parameterPosition = $argPosition;
128-
}
129-
130-
$argPositionByParameterPosition[$parameterPosition] = $argPosition;
131-
}
132-
133-
// only handle calls that fill a contiguous prefix of parameters, so a lone misplaced named arg is left untouched
134-
ksort($argPositionByParameterPosition);
135-
if (array_keys($argPositionByParameterPosition) !== range(0, count($args) - 1)) {
136-
return null;
137-
}
138-
139114
$hasChanged = false;
140-
foreach ($argPositionByParameterPosition as $parameterPosition => $argPosition) {
141-
$arg = $args[$argPosition];
142-
143-
// only named args are removed here; remaining args still bind by name
115+
foreach ($args as $argPosition => $arg) {
116+
// only named args are removed here; in any order, remaining args still bind by name
144117
if (! $arg->name instanceof Identifier) {
145118
continue;
146119
}
@@ -149,6 +122,15 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|FuncCall|null
149122
continue;
150123
}
151124

125+
$parameterPosition = $this->callLikeParamDefaultResolver->resolvePositionParameterByName(
126+
$node,
127+
$arg->name->toString()
128+
);
129+
130+
if ($parameterPosition === null) {
131+
continue;
132+
}
133+
152134
if (! in_array($parameterPosition, $nullPositions, true)) {
153135
continue;
154136
}

0 commit comments

Comments
 (0)