From fc076da414d44c90b1c61a28dad19f13c8990a3a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 16 Dec 2024 21:50:51 +0700 Subject: [PATCH 1/2] Remove trailing comma by token instead of set origNode to null --- .../Fixture/fixture.php.inc | 5 ++- ...adeTrailingCommasInFunctionCallsRector.php | 21 +++---------- .../DowngradeTrailingCommasInUnsetRector.php | 8 ++--- .../Tokenizer/TrailingCommaRemover.php | 31 +++++++++++++++++++ ...owngradeTrailingCommasInParamUseRector.php | 7 +++-- ...eNewMethodCallWithoutParenthesesRector.php | 5 +-- 6 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php diff --git a/rules-tests/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector/Fixture/fixture.php.inc b/rules-tests/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector/Fixture/fixture.php.inc index 84700daf..3657a761 100644 --- a/rules-tests/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector/Fixture/fixture.php.inc +++ b/rules-tests/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector/Fixture/fixture.php.inc @@ -27,7 +27,10 @@ class Fixture { $a = ''; unset($a, $b); - unset($c, $d); + unset( + $c, + $d + ); } } diff --git a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php index d1f2daf6..6ddf5253 100644 --- a/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php +++ b/rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php @@ -10,6 +10,7 @@ use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer; +use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -20,7 +21,8 @@ final class DowngradeTrailingCommasInFunctionCallsRector extends AbstractRector { public function __construct( - private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer + private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer, + private readonly TrailingCommaRemover $trailingCommaRemover ) { } @@ -89,22 +91,7 @@ public function refactor(Node $node): ?Node return null; } - $tokens = $this->file->getOldTokens(); - $iteration = 1; - - while (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration])) { - if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) === '') { - ++$iteration; - continue; - } - - if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) !== ',') { - break; - } - - $tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text = ''; - break; - } + $this->trailingCommaRemover->remove($this->file, $lastArg); return $node; } diff --git a/rules/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector.php b/rules/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector.php index 4d1cf6b9..f6fe6cdf 100644 --- a/rules/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector.php +++ b/rules/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector.php @@ -7,7 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Unset_; use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer; -use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -18,7 +18,8 @@ final class DowngradeTrailingCommasInUnsetRector extends AbstractRector { public function __construct( - private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer + private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer, + private readonly TrailingCommaRemover $trailingCommaRemover ) { } @@ -67,8 +68,7 @@ public function refactor(Node $node): ?Node return null; } - // remove comma - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $this->trailingCommaRemover->remove($this->file, $last); return $node; } diff --git a/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php new file mode 100644 index 00000000..22b2e056 --- /dev/null +++ b/rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php @@ -0,0 +1,31 @@ +getOldTokens(); + $iteration = 1; + + while (isset($tokens[$node->getEndTokenPos() + $iteration])) { + if (trim($tokens[$node->getEndTokenPos() + $iteration]->text) === '') { + ++$iteration; + continue; + } + + if (trim($tokens[$node->getEndTokenPos() + $iteration]->text) !== ',') { + break; + } + + $tokens[$node->getEndTokenPos() + $iteration]->text = ''; + break; + } + } +} diff --git a/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php b/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php index a6a6d9ef..dac20d57 100644 --- a/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php +++ b/rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php @@ -11,7 +11,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer; -use Rector\NodeTypeResolver\Node\AttributeKey; +use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -22,7 +22,8 @@ final class DowngradeTrailingCommasInParamUseRector extends AbstractRector { public function __construct( - private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer + private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer, + private readonly TrailingCommaRemover $trailingCommaRemover ) { } @@ -122,7 +123,7 @@ private function cleanTrailingComma(Closure|ClassMethod|Function_ $node, array $ return null; } - $node->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $this->trailingCommaRemover->remove($this->file, $last); return $node; } diff --git a/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php b/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php index af4623b3..db723683 100644 --- a/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php +++ b/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php @@ -7,7 +7,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -59,7 +58,9 @@ public function refactor(Node $node): ?Node return null; } - $node->var->setAttribute(AttributeKey::ORIGINAL_NODE, null); + $oldTokens[$node->var->getStartTokenPos()]->text = '(' . $oldTokens[$node->var->getStartTokenPos()]; + $oldTokens[$node->var->getEndTokenPos()]->text .= ')'; + return $node; } } From 043ef284d2362af2992f77bfee47c16a5936646b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 16 Dec 2024 21:54:34 +0700 Subject: [PATCH 2/2] clean --- .../DowngradeNewMethodCallWithoutParenthesesRector.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php b/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php index db723683..97d3cccd 100644 --- a/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php +++ b/rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php @@ -54,7 +54,14 @@ public function refactor(Node $node): ?Node } $oldTokens = $this->file->getOldTokens(); - if (isset($oldTokens[$node->getStartTokenPos()]) && (string) $oldTokens[$node->getStartTokenPos()] === '(') { + $startTokenPos = $node->getStartTokenPos(); + $endTokenPos = $node->getEndTokenPos(); + + if (! isset($oldTokens[$startTokenPos], $oldTokens[$endTokenPos])) { + return null; + } + + if ((string) $oldTokens[$node->getStartTokenPos()] === '(') { return null; }