From fe6ddfc2bffa85bd1f4c56e25a42dbc0b07273b2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 12 Dec 2024 21:15:01 +0700 Subject: [PATCH 1/5] [DowngradePhp73] Handle heredoc new line stripped on multi rules --- .../DowngradeHeredoc/DowngradeHeredocTest.php | 28 +++++ .../DowngradeHeredoc/Fixture/fixture.php.inc | 105 ++++++++++++++++++ .../config/configured_rule.php | 10 ++ 3 files changed, 143 insertions(+) create mode 100644 tests/Issues/DowngradeHeredoc/DowngradeHeredocTest.php create mode 100644 tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc create mode 100644 tests/Issues/DowngradeHeredoc/config/configured_rule.php diff --git a/tests/Issues/DowngradeHeredoc/DowngradeHeredocTest.php b/tests/Issues/DowngradeHeredoc/DowngradeHeredocTest.php new file mode 100644 index 00000000..cf730d2a --- /dev/null +++ b/tests/Issues/DowngradeHeredoc/DowngradeHeredocTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc b/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc new file mode 100644 index 00000000..4ee39bda --- /dev/null +++ b/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc @@ -0,0 +1,105 @@ + self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']], + ), + ], + ); + } +} + +?> +----- + self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']]), + ]); + } +} + +?> diff --git a/tests/Issues/DowngradeHeredoc/config/configured_rule.php b/tests/Issues/DowngradeHeredoc/config/configured_rule.php new file mode 100644 index 00000000..5550cffe --- /dev/null +++ b/tests/Issues/DowngradeHeredoc/config/configured_rule.php @@ -0,0 +1,10 @@ +sets([DowngradeLevelSetList::DOWN_TO_PHP_72]); +}; From 1dbfd30aa3fb7908b4cd7194565d0c6e625278d1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 12 Dec 2024 21:38:48 +0700 Subject: [PATCH 2/5] fix --- .../Fixture/fixture.php.inc | 9 ++++---- .../Fixture/new.php.inc | 5 ++++- ...adeTrailingCommasInFunctionCallsRector.php | 6 +++-- .../DowngradeHeredoc/Fixture/fixture.php.inc | 22 ++++++++++++++----- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc index 801aaf49..85cef5f4 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc @@ -26,12 +26,13 @@ class Fixture { public function run() { - compact('posts', 'units'); - $this->setData('posts', 'units'); + compact('posts','units'); + $this->setData('posts','units'); - self::run('posts', 'units'); + self::run('posts','units'); - self::run('posts', 'units'); + self::run('posts', + 'units'); } } diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/new.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/new.php.inc index 9209e6b0..30fd356d 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/new.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/new.php.inc @@ -22,7 +22,10 @@ class Foo { public function doFoo() { - new self('foo', 'bar'); + new self( + 'foo', + 'bar' + ); } } ?> diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 0b09978e..ea05f9fa 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -90,8 +90,10 @@ public function refactor(Node $node): ?Node return null; } - // remove comma - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $tokens = $this->file->getOldTokens(); + if (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + 1]) && $tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text === ',') { + $tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text = ''; + } return $node; } diff --git a/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc b/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc index 4ee39bda..0c1fdb69 100644 --- a/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc +++ b/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc @@ -63,8 +63,11 @@ class Fixture { public function getDefinition(): FixerDefinitionInterface { - return new FixerDefinition('Sorts attributes using the configured sort algorithm.', [ - new VersionSpecificCodeSample(<<<'EOL' + return new FixerDefinition( + 'Sorts attributes using the configured sort algorithm.', + [ + new VersionSpecificCodeSample( + <<<'EOL' self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']]), - ]); +, + new VersionSpecification(80000), + ['sort_algorithm' => self::ORDER_CUSTOM, 'order' => ['A\B\Qux', 'A\B\Bar', 'A\B\Corge']] + ), + ] + ); } } From 89f79e499591c1b06c61c8340c371083025c0754 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 12 Dec 2024 14:40:04 +0000 Subject: [PATCH 3/5] [ci-review] Rector Rectify --- .../FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php | 1 - 1 file changed, 1 deletion(-) diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index ea05f9fa..81e112a4 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -10,7 +10,6 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; From 7ceba8be3f91850d5c335f7751888208e44d906e Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 12 Dec 2024 21:43:36 +0700 Subject: [PATCH 4/5] add fixture multi space --- .../Fixture/multi_space.php.inc | 29 +++++++++++++++++++ ...adeTrailingCommasInFunctionCallsRector.php | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc new file mode 100644 index 00000000..7a6444da --- /dev/null +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 81e112a4..5b98c559 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -90,7 +90,7 @@ public function refactor(Node $node): ?Node } $tokens = $this->file->getOldTokens(); - if (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + 1]) && $tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text === ',') { + if (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + 1]) && trim($tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text) === ',') { $tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text = ''; } From 618a92f65a387e0bb0a485b6cf756f49ea42d669 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 12 Dec 2024 21:47:49 +0700 Subject: [PATCH 5/5] fix --- .../Fixture/multi_space.php.inc | 2 +- .../DowngradeTrailingCommasInFunctionCallsRector.php | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc index 7a6444da..b76b67fd 100644 --- a/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc @@ -22,7 +22,7 @@ class MultiSpace { public function run() { - self::run('posts','units'); + self::run('posts','units' ); } } diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index 5b98c559..9bee1977 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -90,8 +90,15 @@ public function refactor(Node $node): ?Node } $tokens = $this->file->getOldTokens(); - if (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + 1]) && trim($tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text) === ',') { - $tokens[$args[$lastArgKey]->getEndTokenPos() + 1]->text = ''; + $iteration = 1; + + while (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration])) { + if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) === ',') { + $tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text = ''; + break; + } + + ++$iteration; } return $node;