diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 6bd30df4..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; @@ -85,16 +84,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 +91,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' + ); + } +} + +?> 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..783692fb --- /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' + ); + } +} + +?>