Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Expr\StaticCall;
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -85,13 +84,16 @@ public function refactor(Node $node): ?Node
return null;
}

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

if ($hasChanged) {
return $node;
}

return null;
}
}

Expand Down
26 changes: 26 additions & 0 deletions rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\DowngradePhp73\Tokenizer;

use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use Rector\ValueObject\Application\File;

final class TrailingCommaRemover
Expand All @@ -28,4 +29,29 @@ public function remove(File $file, Node $node): void
break;
}
}

public function removeFromCallLike(File $file, CallLike $callLike): bool
{
$tokens = $file->getOldTokens();
$iteration = 1;

$hasChanged = false;
while (isset($tokens[$callLike->getEndTokenPos() - $iteration])) {
$text = trim($tokens[$callLike->getEndTokenPos() - $iteration]->text);

if (in_array($text, [')', ''], true)) {
++$iteration;
continue;
}

if ($text === ',') {
$tokens[$callLike->getEndTokenPos() - $iteration]->text = '';
$hasChanged = true;
}

break;
}

return $hasChanged;
}
}
6 changes: 5 additions & 1 deletion tests/Issues/DowngradeNamedTrailing/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Fixture
}
}

$contents = new Fixture($content->getType(), ['error' => $e->getMessage()], $content->getId());
$contents = new Fixture(
$content->getType(),
['error' => $e->getMessage()],
$content->getId()
);

?>
5 changes: 4 additions & 1 deletion tests/Set/Fixture/named_argument_trailing_comma.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ final class NamedArgumentTrailingComma

public function execute()
{
$this->run('foo', 'bar');
$this->run(
'foo',
'bar'
);
}
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Set/Fixture/named_argument_trailing_comma2.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ final class NamedArgumentTrailingComma2

public function execute()
{
$this->run('foo', 'bar');
$this->run(
'foo',
'bar'
);
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Set/Fixture/named_argument_trailing_comma3.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ final class NamedArgumentTrailingComma3

public function execute()
{
$this->run('foo', 'bar', 'baz');
$this->run(
'foo',
'bar',
'baz'
);
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Set/Fixture/named_argument_trailing_comma4.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ final class NamedArgumentTrailingComma4

public function execute()
{
$this->run('foo', 'bar', 'baz');
$this->run(
'foo',
'bar',
'baz'
);
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Set/Fixture/named_argument_without_trailing_comma.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\Set\Fixture;

final class NamedArgumentWithoutTrailingComma
{
public function run(string $foo, string $bar, string $baz)
{}

public function execute()
{
$this->run(
baz: 'baz',
foo: 'foo',
bar: 'bar'
);
}
}

?>
-----
<?php

namespace Rector\Tests\Set\Fixture;

final class NamedArgumentWithoutTrailingComma
{
public function run(string $foo, string $bar, string $baz)
{}

public function execute()
{
$this->run(
'foo',
'bar',
'baz'
);
}
}

?>