Skip to content

Commit 93b1aff

Browse files
authored
[Down to PHP 7.2] Handle download named argument with trailing comma (#285)
* [Down to PHP 7.2] Handle download named argument with trailing comma * more fixture * Fix * Fix
1 parent e43dd7a commit 93b1aff

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

rules/DowngradePhp73/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Expr\StaticCall;
1212
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
1313
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
14+
use Rector\NodeTypeResolver\Node\AttributeKey;
1415
use Rector\Rector\AbstractRector;
1516
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1617
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -84,6 +85,16 @@ public function refactor(Node $node): ?Node
8485
return null;
8586
}
8687

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+
8798
$lastArgKey = count($args) - 1;
8899

89100
$lastArg = $args[$lastArgKey];

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

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

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

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

0 commit comments

Comments
 (0)