diff --git a/config/set/downgrade-php73.php b/config/set/downgrade-php73.php index 0738b4e6a7f1..9d645da8c358 100644 --- a/config/set/downgrade-php73.php +++ b/config/set/downgrade-php73.php @@ -4,6 +4,7 @@ use Rector\Downgrade\Rector\LNumber\ChangePhpVersionInPlatformCheckRector; use Rector\DowngradePhp73\Rector\List_\DowngradeListReferenceAssignmentRector; +use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector; use Rector\DowngradePhp73\Rector\String_\DowngradeFlexibleHeredocSyntaxRector; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; @@ -15,4 +16,5 @@ ->call('configure', [[ ChangePhpVersionInPlatformCheckRector::TARGET_PHP_VERSION => 70300, ]]); + $services->set(DowngradeTrailingCommasInFunctionCallsRector::class); }; diff --git a/rules/downgrade-php73/src/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/downgrade-php73/src/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php new file mode 100644 index 000000000000..dc86f8031c8f --- /dev/null +++ b/rules/downgrade-php73/src/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -0,0 +1,72 @@ +args){ + $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $last = $node->args[array_key_last($node->args)]; + $last->setAttribute('trailing_comma', false); + } + return $node; + } +} diff --git a/rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/DowngradeTrailingCommasInFunctionCallsRectorTest.php b/rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/DowngradeTrailingCommasInFunctionCallsRectorTest.php new file mode 100644 index 000000000000..1b118aec8ca2 --- /dev/null +++ b/rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/DowngradeTrailingCommasInFunctionCallsRectorTest.php @@ -0,0 +1,32 @@ += 7.3 + * @dataProvider provideData() + */ + public function test(SmartFileInfo $fileInfo): void + { + $this->doTestFileInfo($fileInfo); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return DowngradeTrailingCommasInFunctionCallsRector::class; + } +} diff --git a/rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc b/rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..0ae92fbe34a8 --- /dev/null +++ b/rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc @@ -0,0 +1,37 @@ +setData('posts','units',); + self::run('posts','units',); + self::run('posts', + 'units',); + $this->setOnClick("[Zip ID: {$modelid}] {$e->getMessage($modelId,)}",); + } +} + +?> +----- +setData('posts', 'units'); + self::run('posts', 'units'); + self::run('posts', + 'units'); + $this->setOnClick("[Zip ID: {$modelid}] {$e->getMessage($modelId)}"); + } +} + +?> diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 880484b10f2c..150588d7a21d 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -590,4 +590,17 @@ private function wrapValueWith(String_ $string, string $wrap): string { return $wrap . $string->value . $wrap; } + + protected function pCommaSeparated(array $nodes): string + { + $result = parent::pCommaSeparated($nodes); + + $last = end($nodes); + + if ($last->getAttribute('trailing_comma') === false) { + $result = rtrim($result, ','); + } + + return $result; + } }