Skip to content

Commit 03b553d

Browse files
committed
Updated Rector to commit 04df9bfcee79428c05a6b984fc8ab471d831cffa
rectorphp/rector-src@04df9bf split of RemoveNullNamedArgOnNullDefaultParamRector to handle only named args (#8014)
1 parent 3bf1b80 commit 03b553d

8 files changed

Lines changed: 150 additions & 19 deletions

File tree

config/set/named-args.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Rector\CodeQuality\Rector\CallLike\AddNameToNullArgumentRector;
99
use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector;
1010
use Rector\Config\RectorConfig;
11-
use Rector\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector;
11+
use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;
1212
use Rector\NetteUtils\Rector\StaticCall\UtilsJsonStaticCallNamedArgRector;
1313
return static function (RectorConfig $rectorConfig): void {
14-
$rectorConfig->rules([AddNameToNullArgumentRector::class, AddNameToBooleanArgumentRector::class, RemoveNullArgOnNullDefaultParamRector::class, SortCallLikeNamedArgsRector::class, SortAttributeNamedArgsRector::class, UtilsJsonStaticCallNamedArgRector::class]);
14+
$rectorConfig->rules([AddNameToNullArgumentRector::class, AddNameToBooleanArgumentRector::class, RemoveNullNamedArgOnNullDefaultParamRector::class, SortCallLikeNamedArgsRector::class, SortAttributeNamedArgsRector::class, UtilsJsonStaticCallNamedArgRector::class]);
1515
};

rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,26 @@ public function refactor(Node $node)
8282
if ($node->isFirstClassCallable()) {
8383
return null;
8484
}
85-
if ($node->getArgs() === []) {
85+
$args = $node->getArgs();
86+
if ($args === []) {
8687
return null;
8788
}
89+
// named args are handled by RemoveNullNamedArgOnNullDefaultParamRector
90+
foreach ($args as $arg) {
91+
if ($arg->name instanceof Identifier) {
92+
return null;
93+
}
94+
if ($arg->unpack) {
95+
return null;
96+
}
97+
}
8898
$nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node);
8999
if ($nullPositions === []) {
90100
return null;
91101
}
92102
$hasChanged = \false;
93-
$args = $node->getArgs();
94-
$lastArgPosition = count($args) - 1;
95-
for ($position = $lastArgPosition; $position >= 0; --$position) {
96-
if (!isset($args[$position])) {
97-
continue;
98-
}
103+
for ($position = count($args) - 1; $position >= 0; --$position) {
99104
$arg = $args[$position];
100-
if ($arg->unpack) {
101-
break;
102-
}
103-
// stop when found named arg and position not match
104-
if ($arg->name instanceof Identifier && $position !== $this->callLikeParamDefaultResolver->resolvePositionParameterByName($node, $arg->name->toString())) {
105-
break;
106-
}
107105
if (!$this->valueResolver->isNull($arg->value)) {
108106
break;
109107
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\DeadCode\Rector\MethodCall;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Expr\FuncCall;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Expr\New_;
10+
use PhpParser\Node\Expr\StaticCall;
11+
use PhpParser\Node\Identifier;
12+
use Rector\DeadCode\NodeAnalyzer\CallLikeParamDefaultResolver;
13+
use Rector\PhpParser\Node\Value\ValueResolver;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
/**
18+
* @see \Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\RemoveNullNamedArgOnNullDefaultParamRectorTest
19+
*/
20+
final class RemoveNullNamedArgOnNullDefaultParamRector extends AbstractRector
21+
{
22+
/**
23+
* @readonly
24+
*/
25+
private ValueResolver $valueResolver;
26+
/**
27+
* @readonly
28+
*/
29+
private CallLikeParamDefaultResolver $callLikeParamDefaultResolver;
30+
public function __construct(ValueResolver $valueResolver, CallLikeParamDefaultResolver $callLikeParamDefaultResolver)
31+
{
32+
$this->valueResolver = $valueResolver;
33+
$this->callLikeParamDefaultResolver = $callLikeParamDefaultResolver;
34+
}
35+
public function getRuleDefinition(): RuleDefinition
36+
{
37+
return new RuleDefinition('Remove named null argument, where null is already a default param value', [new CodeSample(<<<'CODE_SAMPLE'
38+
class SomeClass
39+
{
40+
public function call(ExternalClass $externalClass)
41+
{
42+
$externalClass->execute(value: 1, someClass: null);
43+
}
44+
}
45+
46+
class ExternalClass
47+
{
48+
public function execute(int $value, ?SomeClass $someClass = null)
49+
{
50+
}
51+
}
52+
CODE_SAMPLE
53+
, <<<'CODE_SAMPLE'
54+
class SomeClass
55+
{
56+
public function call(ExternalClass $externalClass)
57+
{
58+
$externalClass->execute(value: 1);
59+
}
60+
}
61+
62+
class ExternalClass
63+
{
64+
public function execute(int $value, ?SomeClass $someClass = null)
65+
{
66+
}
67+
}
68+
CODE_SAMPLE
69+
)]);
70+
}
71+
public function getNodeTypes(): array
72+
{
73+
return [MethodCall::class, StaticCall::class, New_::class, FuncCall::class];
74+
}
75+
/**
76+
* @param MethodCall|StaticCall|New_|FuncCall $node
77+
* @return \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\FuncCall|null
78+
*/
79+
public function refactor(Node $node)
80+
{
81+
if ($node->isFirstClassCallable()) {
82+
return null;
83+
}
84+
$args = $node->getArgs();
85+
if ($args === []) {
86+
return null;
87+
}
88+
$hasNamedArg = \false;
89+
foreach ($args as $arg) {
90+
// unpack hides which parameters are bound, so removal is not safe
91+
if ($arg->unpack) {
92+
return null;
93+
}
94+
if ($arg->name instanceof Identifier) {
95+
$hasNamedArg = \true;
96+
}
97+
}
98+
if (!$hasNamedArg) {
99+
return null;
100+
}
101+
$nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node);
102+
if ($nullPositions === []) {
103+
return null;
104+
}
105+
$hasChanged = \false;
106+
foreach ($args as $argPosition => $arg) {
107+
// only named args are removed here; in any order, remaining args still bind by name
108+
if (!$arg->name instanceof Identifier) {
109+
continue;
110+
}
111+
if (!$this->valueResolver->isNull($arg->value)) {
112+
continue;
113+
}
114+
$parameterPosition = $this->callLikeParamDefaultResolver->resolvePositionParameterByName($node, $arg->name->toString());
115+
if ($parameterPosition === null) {
116+
continue;
117+
}
118+
if (!in_array($parameterPosition, $nullPositions, \true)) {
119+
continue;
120+
}
121+
unset($node->args[$argPosition]);
122+
$hasChanged = \true;
123+
}
124+
if ($hasChanged) {
125+
return $node;
126+
}
127+
return null;
128+
}
129+
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '68472f0133461b05ddf22a0d1db0669cbfbde99a';
22+
public const PACKAGE_VERSION = '04df9bfcee79428c05a6b984fc8ab471d831cffa';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-05-28 12:15:19';
27+
public const RELEASE_DATE = '2026-05-28 12:19:29';
2828
/**
2929
* @var int
3030
*/

src/Config/Level/DeadCodeLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector;
4949
use Rector\DeadCode\Rector\If_\UnwrapFutureCompatibleIfPhpVersionRector;
5050
use Rector\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector;
51+
use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;
5152
use Rector\DeadCode\Rector\Node\RemoveNonExistingVarAnnotationRector;
5253
use Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector;
5354
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
@@ -148,6 +149,7 @@ final class DeadCodeLevel
148149
RemoveDeadReturnRector::class,
149150
RemoveArgumentFromDefaultParentCallRector::class,
150151
RemoveNullArgOnNullDefaultParamRector::class,
152+
RemoveNullNamedArgOnNullDefaultParamRector::class,
151153
NarrowWideUnionReturnTypeRector::class,
152154
];
153155
}

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@
14561456
'Rector\\DeadCode\\Rector\\If_\\SimplifyIfElseWithSameContentRector' => $baseDir . '/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php',
14571457
'Rector\\DeadCode\\Rector\\If_\\UnwrapFutureCompatibleIfPhpVersionRector' => $baseDir . '/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php',
14581458
'Rector\\DeadCode\\Rector\\MethodCall\\RemoveNullArgOnNullDefaultParamRector' => $baseDir . '/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php',
1459+
'Rector\\DeadCode\\Rector\\MethodCall\\RemoveNullNamedArgOnNullDefaultParamRector' => $baseDir . '/rules/DeadCode/Rector/MethodCall/RemoveNullNamedArgOnNullDefaultParamRector.php',
14591460
'Rector\\DeadCode\\Rector\\Node\\RemoveNonExistingVarAnnotationRector' => $baseDir . '/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php',
14601461
'Rector\\DeadCode\\Rector\\Plus\\RemoveDeadZeroAndOneOperationRector' => $baseDir . '/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php',
14611462
'Rector\\DeadCode\\Rector\\PropertyProperty\\RemoveNullPropertyInitializationRector' => $baseDir . '/rules/DeadCode/Rector/PropertyProperty/RemoveNullPropertyInitializationRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,7 @@ class ComposerStaticInit212655bc4bab4fd389ef43de213724c8
17161716
'Rector\\DeadCode\\Rector\\If_\\SimplifyIfElseWithSameContentRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/SimplifyIfElseWithSameContentRector.php',
17171717
'Rector\\DeadCode\\Rector\\If_\\UnwrapFutureCompatibleIfPhpVersionRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php',
17181718
'Rector\\DeadCode\\Rector\\MethodCall\\RemoveNullArgOnNullDefaultParamRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php',
1719+
'Rector\\DeadCode\\Rector\\MethodCall\\RemoveNullNamedArgOnNullDefaultParamRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/MethodCall/RemoveNullNamedArgOnNullDefaultParamRector.php',
17191720
'Rector\\DeadCode\\Rector\\Node\\RemoveNonExistingVarAnnotationRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php',
17201721
'Rector\\DeadCode\\Rector\\Plus\\RemoveDeadZeroAndOneOperationRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php',
17211722
'Rector\\DeadCode\\Rector\\PropertyProperty\\RemoveNullPropertyInitializationRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/PropertyProperty/RemoveNullPropertyInitializationRector.php',

vendor/scoper-autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// Restore the backup and ensure the excluded files are properly marked as loaded
1515
$GLOBALS['__composer_autoload_files'] = \array_merge(
1616
$existingComposerAutoloadFiles,
17-
\array_fill_keys(['320cde22f66dd4f5d3fd621d3e88b98f', '9eaa6b0f3f04e58e17ae5ecb754ea313', '80dfc307f8b4b13bcd033cef5c977d19', '8825ede83f2f289127722d4e842cf7e8', 'acbe0d033c55cd0a032b415e08d14f4c', 'e69f7f6ee287b969198c3c9d6777bd38', '36dfd6ed9dd74e8062aa61f09caf8554', 'e4e9c4430b5a6c815e77a26074c8155a', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a', '5928a00fa978807cf85d90ec3f4b0147', '7bfbed8d216c917b4d3e46b319a08b5c', 'c18d2008c6f2fa913c7bba17deb24ff5', '9d2b9fc6db0f153a0a149fefb182415e', '606a39d89246991a373564698c2d8383', 'c4dd194f5b50c0821acf143dcfed86ea'], true)
17+
\array_fill_keys(['9eaa6b0f3f04e58e17ae5ecb754ea313', '320cde22f66dd4f5d3fd621d3e88b98f', 'acbe0d033c55cd0a032b415e08d14f4c', '8825ede83f2f289127722d4e842cf7e8', '80dfc307f8b4b13bcd033cef5c977d19', '36dfd6ed9dd74e8062aa61f09caf8554', 'e69f7f6ee287b969198c3c9d6777bd38', '5928a00fa978807cf85d90ec3f4b0147', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a', 'e4e9c4430b5a6c815e77a26074c8155a', '7bfbed8d216c917b4d3e46b319a08b5c', '9d2b9fc6db0f153a0a149fefb182415e', 'c18d2008c6f2fa913c7bba17deb24ff5', 'c4dd194f5b50c0821acf143dcfed86ea', '606a39d89246991a373564698c2d8383'], true)
1818
);
1919

2020
return $loader;

0 commit comments

Comments
 (0)