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/multi_space.php.inc b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc new file mode 100644 index 00000000..b76b67fd --- /dev/null +++ b/rules-tests/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/multi_space.php.inc @@ -0,0 +1,29 @@ + +----- + 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..9bee1977 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; @@ -90,8 +89,17 @@ public function refactor(Node $node): ?Node return null; } - // remove comma - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $tokens = $this->file->getOldTokens(); + $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; } 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..0c1fdb69 --- /dev/null +++ b/tests/Issues/DowngradeHeredoc/Fixture/fixture.php.inc @@ -0,0 +1,115 @@ + 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]); +};