diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 6bd30df4..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; @@ -85,13 +84,16 @@ 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; + } + + return null; } } diff --git a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php index 22b2e056..a608afac 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 $callLike): bool + { + $tokens = $file->getOldTokens(); + $iteration = 1; + + $hasChanged = false; + while (isset($tokens[$callLike->getEndTokenPos() - $iteration])) { + $text = trim($tokens[$callLike->getEndTokenPos() - $iteration]->text); + + if (in_array($text, [')', ''], true)) { + ++$iteration; + continue; + } + + if ($text === ',') { + $tokens[$callLike->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' + ); } } 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' + ); + } +} + +?>