From e45953963ed15ef7df0f10f9e718971d784a051e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 30 May 2025 19:58:27 +0700 Subject: [PATCH 1/4] [Down to PHP 7.2] Handle download named argument with trailing comma --- .../named_argument_trailing_comma.php.inc | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/Set/Fixture/named_argument_trailing_comma.php.inc diff --git a/tests/Set/Fixture/named_argument_trailing_comma.php.inc b/tests/Set/Fixture/named_argument_trailing_comma.php.inc new file mode 100644 index 00000000..eade45bf --- /dev/null +++ b/tests/Set/Fixture/named_argument_trailing_comma.php.inc @@ -0,0 +1,39 @@ +run( + foo: 'foo', + bar: 'bar', + ); + } +} + +?> +----- +run( + 'foo', + 'bar' + ); + } +} + +?> From d939a023df2535e05fdd98659a8d4ad9d05ebb67 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 30 May 2025 20:17:00 +0700 Subject: [PATCH 2/4] more fixture --- .../Set/Fixture/named_argument_trailing_comma.php.inc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/Set/Fixture/named_argument_trailing_comma.php.inc b/tests/Set/Fixture/named_argument_trailing_comma.php.inc index eade45bf..1f50b7ef 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma.php.inc @@ -13,6 +13,11 @@ final class NamedArgumentTrailingComma foo: 'foo', bar: 'bar', ); + + $this->run( + bar: 'bar', + foo: 'foo', + ); } } @@ -33,6 +38,11 @@ final class NamedArgumentTrailingComma 'foo', 'bar' ); + + $this->run( + 'foo', + 'bar' + ); } } From bedaffeaa569683ab1110df4b404639dae222e5b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 30 May 2025 20:29:19 +0700 Subject: [PATCH 3/4] Fix --- .../DowngradeTrailingCommasInFunctionCallsRector.php | 11 +++++++++++ .../Set/Fixture/named_argument_trailing_comma.php.inc | 10 ++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 6ddf5253..6bd30df4 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -11,6 +11,7 @@ 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; @@ -84,6 +85,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) { + if ($arg->getEndTokenPos() < 0) { + $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + return $node; + } + } + $lastArgKey = count($args) - 1; $lastArg = $args[$lastArgKey]; diff --git a/tests/Set/Fixture/named_argument_trailing_comma.php.inc b/tests/Set/Fixture/named_argument_trailing_comma.php.inc index 1f50b7ef..8efb66cc 100644 --- a/tests/Set/Fixture/named_argument_trailing_comma.php.inc +++ b/tests/Set/Fixture/named_argument_trailing_comma.php.inc @@ -34,15 +34,9 @@ final class NamedArgumentTrailingComma public function execute() { - $this->run( - 'foo', - 'bar' - ); + $this->run('foo', 'bar'); - $this->run( - 'foo', - 'bar' - ); + $this->run('foo', 'bar'); } } From ddcb9434f51b51fe73d70dcbfa8d2442206f35f8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 30 May 2025 20:31:04 +0700 Subject: [PATCH 4/4] Fix --- tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc b/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc index 7783f706..d8d38c04 100644 --- a/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc +++ b/tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc @@ -44,10 +44,6 @@ class Fixture } } -$contents = new Fixture( - $content->getType(), - ['error' => $e->getMessage()], - $content->getId() -); +$contents = new Fixture($content->getType(), ['error' => $e->getMessage()], $content->getId()); ?>