Skip to content

Commit 1454a52

Browse files
authored
[DowngradePhp73] Real patch for previous node token just swapped with trailing comma and named argument (take 2) (#288)
* [DowngradePhp73] Real patch for previous node token just swapped with trailing comma and named argument (take 2) * clean * rectify
1 parent 0f82d7f commit 1454a52

File tree

8 files changed

+98
-11
lines changed

8 files changed

+98
-11
lines changed

rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use PhpParser\Node\Expr\StaticCall;
1212
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
1313
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
14-
use Rector\NodeTypeResolver\Node\AttributeKey;
1514
use Rector\Rector\AbstractRector;
1615
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1716
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -85,13 +84,16 @@ public function refactor(Node $node): ?Node
8584
return null;
8685
}
8786

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
9187
foreach ($args as $arg) {
88+
// reprinted, needs to remove from call like itself
9289
if ($arg->getEndTokenPos() < 0) {
93-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
94-
return $node;
90+
$hasChanged = $this->trailingCommaRemover->removeFromCallLike($this->file, $node);
91+
92+
if ($hasChanged) {
93+
return $node;
94+
}
95+
96+
return null;
9597
}
9698
}
9799

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 $callLike): bool
34+
{
35+
$tokens = $file->getOldTokens();
36+
$iteration = 1;
37+
38+
$hasChanged = false;
39+
while (isset($tokens[$callLike->getEndTokenPos() - $iteration])) {
40+
$text = trim($tokens[$callLike->getEndTokenPos() - $iteration]->text);
41+
42+
if (in_array($text, [')', ''], true)) {
43+
++$iteration;
44+
continue;
45+
}
46+
47+
if ($text === ',') {
48+
$tokens[$callLike->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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Tests\Set\Fixture;
4+
5+
final class NamedArgumentWithoutTrailingComma
6+
{
7+
public function run(string $foo, string $bar, string $baz)
8+
{}
9+
10+
public function execute()
11+
{
12+
$this->run(
13+
baz: 'baz',
14+
foo: 'foo',
15+
bar: 'bar'
16+
);
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Tests\Set\Fixture;
25+
26+
final class NamedArgumentWithoutTrailingComma
27+
{
28+
public function run(string $foo, string $bar, string $baz)
29+
{}
30+
31+
public function execute()
32+
{
33+
$this->run(
34+
'foo',
35+
'bar',
36+
'baz'
37+
);
38+
}
39+
}
40+
41+
?>

0 commit comments

Comments
 (0)