From 4c907f44b83ccd00bdbb9b29996fc9df59d55bad Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 May 2025 13:37:43 +0700 Subject: [PATCH 1/3] [DowngradePhp73] Real patch for previous node token just swapped with trailing comma and named argument (take 2) --- ...adeTrailingCommasInFunctionCallsRector.php | 11 ++++---- .../Tokenizer/TrailingCommaRemover.php | 26 +++++++++++++++++++ .../Fixture/fixture.php.inc | 6 ++++- .../named_argument_trailing_comma.php.inc | 5 +++- .../named_argument_trailing_comma2.php.inc | 5 +++- .../named_argument_trailing_comma3.php.inc | 6 ++++- .../named_argument_trailing_comma4.php.inc | 6 ++++- 7 files changed, 55 insertions(+), 10 deletions(-) diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 6bd30df4..54674f46 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -85,13 +85,14 @@ 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) { + // reprinted, needs to remove from call like itself if ($arg->getEndTokenPos() < 0) { - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); - return $node; + $hasChanged = $this->trailingCommaRemover->removeFromCallLike($this->file, $node); + + if ($hasChanged) { + return $node; + } } } diff --git a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php index 22b2e056..5c8f9309 100644 --- a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php +++ b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php @@ -5,6 +5,7 @@ namespace Rector\DowngradePhp73\Tokenizer; use PhpParser\Node; +use PhpParser\Node\Expr\CallLike; use Rector\ValueObject\Application\File; final class TrailingCommaRemover @@ -28,4 +29,29 @@ public function remove(File $file, Node $node): void break; } } + + public function removeFromCallLike(File $file, CallLike $node): bool + { + $tokens = $file->getOldTokens(); + $iteration = 1; + + $hasChanged = false; + while (isset($tokens[$node->getEndTokenPos() - $iteration])) { + $text = trim($tokens[$node->getEndTokenPos() - $iteration]->text); + + if (in_array($text, [')', ''], true)) { + ++$iteration; + continue; + } + + if ($text === ',') { + $tokens[$node->getEndTokenPos() - $iteration]->text = ''; + $hasChanged = true; + } + + break; + } + + return $hasChanged; + } } 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 dbd17312..eade45bf 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma.php.inc @@ -29,7 +29,10 @@ final class NamedArgumentTrailingComma public function execute() { - $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 index 516d0a8c..9a7c4e14 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma2.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma2.php.inc @@ -29,7 +29,10 @@ final class NamedArgumentTrailingComma2 public function execute() { - $this->run('foo', 'bar'); + $this->run( + 'foo', + 'bar' + ); } } diff --git a/tests/Set/Fixture/named_argument_trailing_comma3.php.inc b/tests/Set/Fixture/named_argument_trailing_comma3.php.inc index 6e31e7da..783692fb 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma3.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma3.php.inc @@ -30,7 +30,11 @@ final class NamedArgumentTrailingComma3 public function execute() { - $this->run('foo', 'bar', 'baz'); + $this->run( + 'foo', + 'bar', + 'baz' + ); } } diff --git a/tests/Set/Fixture/named_argument_trailing_comma4.php.inc b/tests/Set/Fixture/named_argument_trailing_comma4.php.inc index 1631bd52..a9270de8 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma4.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma4.php.inc @@ -30,7 +30,11 @@ final class NamedArgumentTrailingComma4 public function execute() { - $this->run('foo', 'bar', 'baz'); + $this->run( + 'foo', + 'bar', + 'baz' + ); } } From 44586f9285bdc381215294048215a74f2c1f2ec5 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 May 2025 13:42:19 +0700 Subject: [PATCH 2/3] clean --- ...adeTrailingCommasInFunctionCallsRector.php | 2 + ...ed_argument_without_trailing_comma.php.inc | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/Set/Fixture/named_argument_without_trailing_comma.php.inc diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 54674f46..c27d2738 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -93,6 +93,8 @@ public function refactor(Node $node): ?Node if ($hasChanged) { return $node; } + + return null; } } diff --git a/tests/Set/Fixture/named_argument_without_trailing_comma.php.inc b/tests/Set/Fixture/named_argument_without_trailing_comma.php.inc new file mode 100644 index 00000000..970246ba --- /dev/null +++ b/tests/Set/Fixture/named_argument_without_trailing_comma.php.inc @@ -0,0 +1,41 @@ +run( + baz: 'baz', + foo: 'foo', + bar: 'bar' + ); + } +} + +?> +----- +run( + 'foo', + 'bar', + 'baz' + ); + } +} + +?> From 3644d98fb28cc5ffbd83d3472595271b1be3899f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 31 May 2025 13:43:06 +0700 Subject: [PATCH 3/3] rectify --- .../DowngradeTrailingCommasInFunctionCallsRector.php | 1 - rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index c27d2738..13d91e43 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; diff --git a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php index 5c8f9309..a608afac 100644 --- a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php +++ b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php @@ -30,14 +30,14 @@ public function remove(File $file, Node $node): void } } - public function removeFromCallLike(File $file, CallLike $node): bool + public function removeFromCallLike(File $file, CallLike $callLike): bool { $tokens = $file->getOldTokens(); $iteration = 1; $hasChanged = false; - while (isset($tokens[$node->getEndTokenPos() - $iteration])) { - $text = trim($tokens[$node->getEndTokenPos() - $iteration]->text); + while (isset($tokens[$callLike->getEndTokenPos() - $iteration])) { + $text = trim($tokens[$callLike->getEndTokenPos() - $iteration]->text); if (in_array($text, [')', ''], true)) { ++$iteration; @@ -45,7 +45,7 @@ public function removeFromCallLike(File $file, CallLike $node): bool } if ($text === ',') { - $tokens[$node->getEndTokenPos() - $iteration]->text = ''; + $tokens[$callLike->getEndTokenPos() - $iteration]->text = ''; $hasChanged = true; }