Skip to content

Commit 4c907f4

Browse files
committed
[DowngradePhp73] Real patch for previous node token just swapped with trailing comma and named argument (take 2)
1 parent 0f82d7f commit 4c907f4

File tree

7 files changed

+55
-10
lines changed

7 files changed

+55
-10
lines changed

rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ public function refactor(Node $node): ?Node
8585
return null;
8686
}
8787

88-
// reprint is needed as position changed that can't rely on token position
89-
// @see https://github.com/rectorphp/rector-downgrade-php/pull/281
90-
// @see https://github.com/rectorphp/rector-downgrade-php/pull/285
9188
foreach ($args as $arg) {
89+
// reprinted, needs to remove from call like itself
9290
if ($arg->getEndTokenPos() < 0) {
93-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
94-
return $node;
91+
$hasChanged = $this->trailingCommaRemover->removeFromCallLike($this->file, $node);
92+
93+
if ($hasChanged) {
94+
return $node;
95+
}
9596
}
9697
}
9798

rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\DowngradePhp73\Tokenizer;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Expr\CallLike;
89
use Rector\ValueObject\Application\File;
910

1011
final class TrailingCommaRemover
@@ -28,4 +29,29 @@ public function remove(File $file, Node $node): void
2829
break;
2930
}
3031
}
32+
33+
public function removeFromCallLike(File $file, CallLike $node): bool
34+
{
35+
$tokens = $file->getOldTokens();
36+
$iteration = 1;
37+
38+
$hasChanged = false;
39+
while (isset($tokens[$node->getEndTokenPos() - $iteration])) {
40+
$text = trim($tokens[$node->getEndTokenPos() - $iteration]->text);
41+
42+
if (in_array($text, [')', ''], true)) {
43+
++$iteration;
44+
continue;
45+
}
46+
47+
if ($text === ',') {
48+
$tokens[$node->getEndTokenPos() - $iteration]->text = '';
49+
$hasChanged = true;
50+
}
51+
52+
break;
53+
}
54+
55+
return $hasChanged;
56+
}
3157
}

tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class Fixture
4444
}
4545
}
4646

47-
$contents = new Fixture($content->getType(), ['error' => $e->getMessage()], $content->getId());
47+
$contents = new Fixture(
48+
$content->getType(),
49+
['error' => $e->getMessage()],
50+
$content->getId()
51+
);
4852

4953
?>

tests/Set/Fixture/named_argument_trailing_comma.php.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ final class NamedArgumentTrailingComma
2929

3030
public function execute()
3131
{
32-
$this->run('foo', 'bar');
32+
$this->run(
33+
'foo',
34+
'bar'
35+
);
3336
}
3437
}
3538

tests/Set/Fixture/named_argument_trailing_comma2.php.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ final class NamedArgumentTrailingComma2
2929

3030
public function execute()
3131
{
32-
$this->run('foo', 'bar');
32+
$this->run(
33+
'foo',
34+
'bar'
35+
);
3336
}
3437
}
3538

tests/Set/Fixture/named_argument_trailing_comma3.php.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ final class NamedArgumentTrailingComma3
3030

3131
public function execute()
3232
{
33-
$this->run('foo', 'bar', 'baz');
33+
$this->run(
34+
'foo',
35+
'bar',
36+
'baz'
37+
);
3438
}
3539
}
3640

tests/Set/Fixture/named_argument_trailing_comma4.php.inc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ final class NamedArgumentTrailingComma4
3030

3131
public function execute()
3232
{
33-
$this->run('foo', 'bar', 'baz');
33+
$this->run(
34+
'foo',
35+
'bar',
36+
'baz'
37+
);
3438
}
3539
}
3640

0 commit comments

Comments
 (0)