Skip to content

Commit 7b990bd

Browse files
authored
Remove trailing comma by token instead of set origNode to null (#259)
* Remove trailing comma by token instead of set origNode to null * clean
1 parent 4a42815 commit 7b990bd

File tree

6 files changed

+58
-28
lines changed

6 files changed

+58
-28
lines changed

rules-tests/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector/Fixture/fixture.php.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class Fixture
2727
{
2828
$a = '';
2929
unset($a, $b);
30-
unset($c, $d);
30+
unset(
31+
$c,
32+
$d
33+
);
3134
}
3235
}
3336

rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Expr\New_;
1111
use PhpParser\Node\Expr\StaticCall;
1212
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
13+
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
1314
use Rector\Rector\AbstractRector;
1415
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1516
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -20,7 +21,8 @@
2021
final class DowngradeTrailingCommasInFunctionCallsRector extends AbstractRector
2122
{
2223
public function __construct(
23-
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer
24+
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer,
25+
private readonly TrailingCommaRemover $trailingCommaRemover
2426
) {
2527
}
2628

@@ -89,22 +91,7 @@ public function refactor(Node $node): ?Node
8991
return null;
9092
}
9193

92-
$tokens = $this->file->getOldTokens();
93-
$iteration = 1;
94-
95-
while (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration])) {
96-
if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) === '') {
97-
++$iteration;
98-
continue;
99-
}
100-
101-
if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) !== ',') {
102-
break;
103-
}
104-
105-
$tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text = '';
106-
break;
107-
}
94+
$this->trailingCommaRemover->remove($this->file, $lastArg);
10895

10996
return $node;
11097
}

rules/DowngradePhp73/Rector/Unset_/DowngradeTrailingCommasInUnsetRector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\Unset_;
99
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
10-
use Rector\NodeTypeResolver\Node\AttributeKey;
10+
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
1111
use Rector\Rector\AbstractRector;
1212
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1313
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -18,7 +18,8 @@
1818
final class DowngradeTrailingCommasInUnsetRector extends AbstractRector
1919
{
2020
public function __construct(
21-
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer
21+
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer,
22+
private readonly TrailingCommaRemover $trailingCommaRemover
2223
) {
2324
}
2425

@@ -67,8 +68,7 @@ public function refactor(Node $node): ?Node
6768
return null;
6869
}
6970

70-
// remove comma
71-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
71+
$this->trailingCommaRemover->remove($this->file, $last);
7272

7373
return $node;
7474
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DowngradePhp73\Tokenizer;
6+
7+
use PhpParser\Node;
8+
use Rector\ValueObject\Application\File;
9+
10+
final class TrailingCommaRemover
11+
{
12+
public function remove(File $file, Node $node): void
13+
{
14+
$tokens = $file->getOldTokens();
15+
$iteration = 1;
16+
17+
while (isset($tokens[$node->getEndTokenPos() + $iteration])) {
18+
if (trim($tokens[$node->getEndTokenPos() + $iteration]->text) === '') {
19+
++$iteration;
20+
continue;
21+
}
22+
23+
if (trim($tokens[$node->getEndTokenPos() + $iteration]->text) !== ',') {
24+
break;
25+
}
26+
27+
$tokens[$node->getEndTokenPos() + $iteration]->text = '';
28+
break;
29+
}
30+
}
31+
}

rules/DowngradePhp80/Rector/ClassMethod/DowngradeTrailingCommasInParamUseRector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PhpParser\Node\Stmt\Function_;
1313
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
14-
use Rector\NodeTypeResolver\Node\AttributeKey;
14+
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
1515
use Rector\Rector\AbstractRector;
1616
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1717
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -22,7 +22,8 @@
2222
final class DowngradeTrailingCommasInParamUseRector extends AbstractRector
2323
{
2424
public function __construct(
25-
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer
25+
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer,
26+
private readonly TrailingCommaRemover $trailingCommaRemover
2627
) {
2728
}
2829

@@ -122,7 +123,7 @@ private function cleanTrailingComma(Closure|ClassMethod|Function_ $node, array $
122123
return null;
123124
}
124125

125-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
126+
$this->trailingCommaRemover->remove($this->file, $last);
126127

127128
return $node;
128129
}

rules/DowngradePhp84/Rector/MethodCall/DowngradeNewMethodCallWithoutParenthesesRector.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
99
use PhpParser\Node\Expr\New_;
10-
use Rector\NodeTypeResolver\Node\AttributeKey;
1110
use Rector\Rector\AbstractRector;
1211
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1312
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -55,11 +54,20 @@ public function refactor(Node $node): ?Node
5554
}
5655

5756
$oldTokens = $this->file->getOldTokens();
58-
if (isset($oldTokens[$node->getStartTokenPos()]) && (string) $oldTokens[$node->getStartTokenPos()] === '(') {
57+
$startTokenPos = $node->getStartTokenPos();
58+
$endTokenPos = $node->getEndTokenPos();
59+
60+
if (! isset($oldTokens[$startTokenPos], $oldTokens[$endTokenPos])) {
5961
return null;
6062
}
6163

62-
$node->var->setAttribute(AttributeKey::ORIGINAL_NODE, null);
64+
if ((string) $oldTokens[$node->getStartTokenPos()] === '(') {
65+
return null;
66+
}
67+
68+
$oldTokens[$node->var->getStartTokenPos()]->text = '(' . $oldTokens[$node->var->getStartTokenPos()];
69+
$oldTokens[$node->var->getEndTokenPos()]->text .= ')';
70+
6371
return $node;
6472
}
6573
}

0 commit comments

Comments
 (0)