Skip to content

Commit 57f9d5e

Browse files
committed
[DowngradePhp73] Real patch for previous node token just swapped with trailing comma and named argument
1 parent 93b1aff commit 57f9d5e

File tree

7 files changed

+60
-29
lines changed

7 files changed

+60
-29
lines changed

rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,15 @@ 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
91-
foreach ($args as $arg) {
92-
if ($arg->getEndTokenPos() < 0) {
93-
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
94-
return $node;
95-
}
96-
}
97-
9888
$lastArgKey = count($args) - 1;
9989

10090
$lastArg = $args[$lastArgKey];
10191
if (! $this->followedByCommaAnalyzer->isFollowed($this->file, $lastArg)) {
10292
return null;
10393
}
10494

105-
$this->trailingCommaRemover->remove($this->file, $lastArg);
95+
$previousArg = $args[$lastArgKey - 1] ?? null;
96+
$this->trailingCommaRemover->remove($this->file, $lastArg, $previousArg);
10697

10798
return $node;
10899
}

rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
final class TrailingCommaRemover
1111
{
12-
public function remove(File $file, Node $node): void
12+
public function remove(File $file, Node $node, ?Node $previousNode = null): void
1313
{
1414
$tokens = $file->getOldTokens();
1515
$iteration = 1;
@@ -24,6 +24,12 @@ public function remove(File $file, Node $node): void
2424
break;
2525
}
2626

27+
// position just swapped
28+
if ($previousNode instanceof Node && $previousNode->getEndTokenPos() > $node->getEndTokenPos()) {
29+
$this->remove($file, $previousNode);
30+
break;
31+
}
32+
2733
$tokens[$node->getEndTokenPos() + $iteration]->text = '';
2834
break;
2935
}

rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@ public function fillFromNamedArgs(
5252
continue;
5353
}
5454

55-
$unnamedArgs[$paramPosition] = new Arg(
56-
$currentArg->value,
57-
$currentArg->byRef,
58-
$currentArg->unpack,
59-
[],
60-
null
61-
);
55+
$currentArg->name = null;
56+
$unnamedArgs[$paramPosition] = $currentArg;
6257
}
6358
}
6459

rules/DowngradePhp80/NodeAnalyzer/UnnamedArgumentResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function resolveFromReflection(
4545

4646
foreach ($currentArgs as $key => $arg) {
4747
if (! $arg->name instanceof Identifier) {
48-
$unnamedArgs[$key] = new Arg($arg->value, $arg->byRef, $arg->unpack, [], null);
48+
$unnamedArgs[$key] = $arg;
4949

5050
continue;
5151
}

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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ final class NamedArgumentTrailingComma
1313
foo: 'foo',
1414
bar: 'bar',
1515
);
16-
17-
$this->run(
18-
bar: 'bar',
19-
foo: 'foo',
20-
);
2116
}
2217
}
2318

@@ -34,9 +29,10 @@ final class NamedArgumentTrailingComma
3429

3530
public function execute()
3631
{
37-
$this->run('foo', 'bar');
38-
39-
$this->run('foo', 'bar');
32+
$this->run(
33+
'foo',
34+
'bar'
35+
);
4036
}
4137
}
4238

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

0 commit comments

Comments
 (0)