From 57f9d5e3ea2b9b2c05f701818c65cdf85382b692 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 May 2025 06:45:13 +0700 Subject: [PATCH 1/4] [DowngradePhp73] Real patch for previous node token just swapped with trailing comma and named argument --- ...adeTrailingCommasInFunctionCallsRector.php | 13 +------ .../Tokenizer/TrailingCommaRemover.php | 8 +++- .../NodeAnalyzer/NamedToUnnamedArgs.php | 9 +---- .../NodeAnalyzer/UnnamedArgumentResolver.php | 2 +- .../Fixture/fixture.php.inc | 6 ++- .../named_argument_trailing_comma.php.inc | 12 ++---- .../named_argument_trailing_comma2.php.inc | 39 +++++++++++++++++++ 7 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 tests/Set/Fixture/named_argument_trailing_comma2.php.inc diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 6bd30df4..fd70f25f 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -85,16 +85,6 @@ public function refactor(Node $node): ?Node return null; } - // reprint is needed as position changed that can't rely on token position - // @see https://github.com/rectorphp/rector-downgrade-php/pull/281 - // @see https://github.com/rectorphp/rector-downgrade-php/pull/285 - foreach ($args as $arg) { - if ($arg->getEndTokenPos() < 0) { - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); - return $node; - } - } - $lastArgKey = count($args) - 1; $lastArg = $args[$lastArgKey]; @@ -102,7 +92,8 @@ public function refactor(Node $node): ?Node return null; } - $this->trailingCommaRemover->remove($this->file, $lastArg); + $previousArg = $args[$lastArgKey - 1] ?? null; + $this->trailingCommaRemover->remove($this->file, $lastArg, $previousArg); return $node; } diff --git a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php index 22b2e056..9b418df7 100644 --- a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php +++ b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php @@ -9,7 +9,7 @@ final class TrailingCommaRemover { - public function remove(File $file, Node $node): void + public function remove(File $file, Node $node, ?Node $previousNode = null): void { $tokens = $file->getOldTokens(); $iteration = 1; @@ -24,6 +24,12 @@ public function remove(File $file, Node $node): void break; } + // position just swapped + if ($previousNode instanceof Node && $previousNode->getEndTokenPos() > $node->getEndTokenPos()) { + $this->remove($file, $previousNode); + break; + } + $tokens[$node->getEndTokenPos() + $iteration]->text = ''; break; } diff --git a/rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php b/rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php index 54e2beec..a72f776d 100644 --- a/rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php +++ b/rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php @@ -52,13 +52,8 @@ public function fillFromNamedArgs( continue; } - $unnamedArgs[$paramPosition] = new Arg( - $currentArg->value, - $currentArg->byRef, - $currentArg->unpack, - [], - null - ); + $currentArg->name = null; + $unnamedArgs[$paramPosition] = $currentArg; } } diff --git a/rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php b/rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php index 68e7b47d..64b2759a 100644 --- a/rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php +++ b/rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php @@ -45,7 +45,7 @@ public function resolveFromReflection( foreach ($currentArgs as $key => $arg) { if (! $arg->name instanceof Identifier) { - $unnamedArgs[$key] = new Arg($arg->value, $arg->byRef, $arg->unpack, [], null); + $unnamedArgs[$key] = $arg; continue; } diff --git a/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc b/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc index d8d38c04..7783f706 100644 --- a/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc +++ b/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc @@ -44,6 +44,10 @@ class Fixture } } -$contents = new Fixture($content->getType(), ['error' => $e->getMessage()], $content->getId()); +$contents = new Fixture( + $content->getType(), + ['error' => $e->getMessage()], + $content->getId() +); ?> diff --git a/tests/Set/Fixture/named_argument_trailing_comma.php.inc b/tests/Set/Fixture/named_argument_trailing_comma.php.inc index 8efb66cc..eade45bf 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma.php.inc @@ -13,11 +13,6 @@ final class NamedArgumentTrailingComma foo: 'foo', bar: 'bar', ); - - $this->run( - bar: 'bar', - foo: 'foo', - ); } } @@ -34,9 +29,10 @@ final class NamedArgumentTrailingComma public function execute() { - $this->run('foo', 'bar'); - - $this->run('foo', 'bar'); + $this->run( + 'foo', + 'bar' + ); } } diff --git a/tests/Set/Fixture/named_argument_trailing_comma2.php.inc b/tests/Set/Fixture/named_argument_trailing_comma2.php.inc new file mode 100644 index 00000000..9a7c4e14 --- /dev/null +++ b/tests/Set/Fixture/named_argument_trailing_comma2.php.inc @@ -0,0 +1,39 @@ +run( + bar: 'bar', + foo: 'foo', + ); + } +} + +?> +----- +run( + 'foo', + 'bar' + ); + } +} + +?> From 72aa6148ee84704df7eb48be6976d6a706d196fe Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 May 2025 06:48:52 +0700 Subject: [PATCH 2/4] swap last to middle --- .../named_argument_trailing_comma3.php.inc | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/Set/Fixture/named_argument_trailing_comma3.php.inc diff --git a/tests/Set/Fixture/named_argument_trailing_comma3.php.inc b/tests/Set/Fixture/named_argument_trailing_comma3.php.inc new file mode 100644 index 00000000..11d09924 --- /dev/null +++ b/tests/Set/Fixture/named_argument_trailing_comma3.php.inc @@ -0,0 +1,41 @@ +run( + bar: 'bar', + baz: 'baz', + foo: 'foo', + ); + } +} + +?> +----- +run( + 'foo', + 'bar', + 'baz' + ); + } +} + +?> From 02d411868f387643553d07a988201654e4843b16 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 30 May 2025 23:49:39 +0000 Subject: [PATCH 3/4] [ci-review] Rector Rectify --- .../FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index fd70f25f..bc449541 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -11,7 +11,6 @@ use PhpParser\Node\Expr\StaticCall; use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer; use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; From 943fd08aba47709f9e285c90127e86b4fca19015 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 May 2025 06:51:03 +0700 Subject: [PATCH 4/4] udpate fixture --- tests/Set/Fixture/named_argument_trailing_comma3.php.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Set/Fixture/named_argument_trailing_comma3.php.inc b/tests/Set/Fixture/named_argument_trailing_comma3.php.inc index 11d09924..783692fb 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma3.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma3.php.inc @@ -25,7 +25,7 @@ namespace Rector\Tests\Set\Fixture; final class NamedArgumentTrailingComma3 { - public function run(string $foo, string $bar) + public function run(string $foo, string $bar, string $baz) {} public function execute()