Skip to content
Closed
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,24 +84,15 @@ 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) {
if ($arg->getEndTokenPos() < 0) {
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
return $node;
}
}

$lastArgKey = count($args) - 1;

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

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

return $node;
}
Expand Down
8 changes: 7 additions & 1 deletion rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class TrailingCommaRemover
{
public function remove(File $file, Node $node): void
public function remove(File $file, Node $node, ?Node $previousNode = null): void
{
$tokens = $file->getOldTokens();
$iteration = 1;
Expand All @@ -24,6 +24,12 @@ public function remove(File $file, Node $node): void
break;
}

// position just swapped
if ($previousNode instanceof Node && $previousNode->getEndTokenPos() > $node->getEndTokenPos()) {
$this->remove($file, $previousNode);
break;
}

$tokens[$node->getEndTokenPos() + $iteration]->text = '';
break;
}
Expand Down
9 changes: 2 additions & 7 deletions rules/DowngradePhp80/NodeAnalyzer/NamedToUnnamedArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,8 @@ public function fillFromNamedArgs(
continue;
}

$unnamedArgs[$paramPosition] = new Arg(
$currentArg->value,
$currentArg->byRef,
$currentArg->unpack,
[],
null
);
$currentArg->name = null;
$unnamedArgs[$paramPosition] = $currentArg;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function resolveFromReflection(

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

continue;
}
Expand Down
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()
);

?>
12 changes: 4 additions & 8 deletions tests/Set/Fixture/named_argument_trailing_comma.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ final class NamedArgumentTrailingComma
foo: 'foo',
bar: 'bar',
);

$this->run(
bar: 'bar',
foo: 'foo',
);
}
}

Expand All @@ -34,9 +29,10 @@ final class NamedArgumentTrailingComma

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

$this->run('foo', 'bar');
$this->run(
'foo',
'bar'
);
}
}

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

namespace Rector\Tests\Set\Fixture;

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

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

?>
-----
<?php

namespace Rector\Tests\Set\Fixture;

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

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

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

namespace Rector\Tests\Set\Fixture;

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

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

?>
-----
<?php

namespace Rector\Tests\Set\Fixture;

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

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

?>
Loading