From 8d07bbd4eea22c670e0aa5c1c6d83c1e62344ac5 Mon Sep 17 00:00:00 2001 From: Tommy Quissens Date: Thu, 3 Dec 2020 20:10:23 +0100 Subject: [PATCH 1/2] Bump dependency "Fixed formatting-preserving pretty printing for "{$x}"" --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index adb12828f134..5994b6fcb6ad 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "jean85/pretty-package-versions": "^1.5.1", "nette/robot-loader": "^3.2", "nette/utils": "^3.1", - "nikic/php-parser": "4.10.2", + "nikic/php-parser": "4.10.3", "phpstan/phpdoc-parser": "^0.4.9", "phpstan/phpstan": "^0.12.52", "phpstan/phpstan-phpunit": "^0.12.16", From 0c39734463a43528ca64849b8d62f2c30ff6b533 Mon Sep 17 00:00:00 2001 From: Tommy Quissens Date: Tue, 22 Sep 2020 15:50:14 +0200 Subject: [PATCH 2/2] [Downgrade PHP 7.3] Trailing commas in function calls --- config/set/downgrade-php73.php | 2 + .../src/Node/AttributeKey.php | 5 ++ ...adeTrailingCommasInFunctionCallsRector.php | 72 +++++++++++++++++++ ...railingCommasInFunctionCallsRectorTest.php | 32 +++++++++ .../Fixture/fixture.php.inc | 37 ++++++++++ .../Printer/BetterStandardPrinter.php | 16 +++++ 6 files changed, 164 insertions(+) create mode 100644 rules/downgrade-php73/src/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php create mode 100644 rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/DowngradeTrailingCommasInFunctionCallsRectorTest.php create mode 100644 rules/downgrade-php73/tests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc 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/packages/node-type-resolver/src/Node/AttributeKey.php b/packages/node-type-resolver/src/Node/AttributeKey.php index 0f52cfb43cb0..08dbff12bea6 100644 --- a/packages/node-type-resolver/src/Node/AttributeKey.php +++ b/packages/node-type-resolver/src/Node/AttributeKey.php @@ -227,4 +227,9 @@ final class AttributeKey * @var string */ public const IS_FRESH_NODE = 'is_fresh_node'; + + /** + * @var string + */ + public const TRAILING_COMMA = 'trailing_comma'; } 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..fe1253bb7e2e --- /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(AttributeKey::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..47d1634578fa 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -590,4 +590,20 @@ 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 instanceof Node) { + $trailingComma = $last->getAttribute(AttributeKey::TRAILING_COMMA); + if ($trailingComma === false) { + $result = rtrim($result, ','); + } + } + + return $result; + } }