From e7f895f6eacf54c9cec64aa60f589e679466abb4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 24 Oct 2025 20:35:45 +0700 Subject: [PATCH 1/3] [DeadCode] Handle with middle default not null on RemoveNullArgOnNullDefaultParamRector --- .../handle_middle_default_not_null.php.inc | 35 +++++++++++++++++++ .../Source/SomeExternalClass.php | 4 +++ .../RemoveNullArgOnNullDefaultParamRector.php | 18 ++++++---- 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/handle_middle_default_not_null.php.inc diff --git a/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/handle_middle_default_not_null.php.inc b/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/handle_middle_default_not_null.php.inc new file mode 100644 index 00000000000..6fe117f241d --- /dev/null +++ b/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/handle_middle_default_not_null.php.inc @@ -0,0 +1,35 @@ +withMiddleNotNullDefault(null, 1, null, null); + } +} + +?> +----- +withMiddleNotNullDefault(null, 1); + } +} + +?> diff --git a/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Source/SomeExternalClass.php b/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Source/SomeExternalClass.php index ded8d884a0a..d7b0322e750 100644 --- a/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Source/SomeExternalClass.php +++ b/rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Source/SomeExternalClass.php @@ -15,4 +15,8 @@ public function callWithDefaultNull(?string $name = null) public static function staticCallWithDefaultNull(?int $id = null) { } + + public static function withMiddleNotNullDefault(?int $id = null, int $data = 1, ?string $name = null, ?string $item = null) + { + } } diff --git a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php index 61da2f19826..94dcf6e0a9e 100644 --- a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php +++ b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php @@ -88,27 +88,33 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null $hasChanged = false; - foreach ($node->getArgs() as $position => $arg) { - if ($arg->unpack) { + $args = $node->getArgs(); + $lastArgPosition = count($args) - 1; + for ($position = $lastArgPosition; $position >=0; $position--) { + if (! isset($args[$position])) { continue; } + $arg = $args[$position]; + if ($arg->unpack) { + return null; + } + // skip named args if ($arg->name instanceof Node) { - continue; + return null; } if (! $this->valueResolver->isNull($arg->value)) { - continue; + return null; } $nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node); if (! in_array($position, $nullPositions)) { - continue; + return null; } unset($node->args[$position]); - $hasChanged = true; } From c73fe7750dfc3937c2a056853b0e176fbaa8b62d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 24 Oct 2025 20:37:17 +0700 Subject: [PATCH 2/3] [DeadCode] Handle with middle default not null on RemoveNullArgOnNullDefaultParamRector --- .../MethodCall/RemoveNullArgOnNullDefaultParamRector.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php index 94dcf6e0a9e..51b8c86a70c 100644 --- a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php +++ b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php @@ -97,21 +97,21 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null $arg = $args[$position]; if ($arg->unpack) { - return null; + break; } // skip named args if ($arg->name instanceof Node) { - return null; + break; } if (! $this->valueResolver->isNull($arg->value)) { - return null; + break; } $nullPositions = $this->callLikeParamDefaultResolver->resolveNullPositions($node); if (! in_array($position, $nullPositions)) { - return null; + break; } unset($node->args[$position]); From 885b48c6e74481dea0d1ad25c9c955800ac74721 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 24 Oct 2025 13:41:20 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- .../Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php index 51b8c86a70c..ccbf86dcd15 100644 --- a/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php +++ b/rules/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector.php @@ -90,7 +90,7 @@ public function refactor(Node $node): StaticCall|MethodCall|New_|null $args = $node->getArgs(); $lastArgPosition = count($args) - 1; - for ($position = $lastArgPosition; $position >=0; $position--) { + for ($position = $lastArgPosition; $position >=0; --$position) { if (! isset($args[$position])) { continue; }