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 @@ -27,7 +27,10 @@ class Fixture
{
$a = '';
unset($a, $b);
unset($c, $d);
unset(
$c,
$d
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -20,7 +21,8 @@
final class DowngradeTrailingCommasInFunctionCallsRector extends AbstractRector
{
public function __construct(
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer,
private readonly TrailingCommaRemover $trailingCommaRemover
) {
}

Expand Down Expand Up @@ -89,22 +91,7 @@ public function refactor(Node $node): ?Node
return null;
}

$tokens = $this->file->getOldTokens();
$iteration = 1;

while (isset($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration])) {
if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) === '') {
++$iteration;
continue;
}

if (trim($tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text) !== ',') {
break;
}

$tokens[$args[$lastArgKey]->getEndTokenPos() + $iteration]->text = '';
break;
}
$this->trailingCommaRemover->remove($this->file, $lastArg);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Unset_;
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -18,7 +18,8 @@
final class DowngradeTrailingCommasInUnsetRector extends AbstractRector
{
public function __construct(
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer,
private readonly TrailingCommaRemover $trailingCommaRemover
) {
}

Expand Down Expand Up @@ -67,8 +68,7 @@ public function refactor(Node $node): ?Node
return null;
}

// remove comma
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->trailingCommaRemover->remove($this->file, $last);

return $node;
}
Expand Down
31 changes: 31 additions & 0 deletions rules/DowngradePhp73/Tokenizer/TrailingCommaRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp73\Tokenizer;

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

final class TrailingCommaRemover
{
public function remove(File $file, Node $node): void
{
$tokens = $file->getOldTokens();
$iteration = 1;

while (isset($tokens[$node->getEndTokenPos() + $iteration])) {
if (trim($tokens[$node->getEndTokenPos() + $iteration]->text) === '') {
++$iteration;
continue;
}

if (trim($tokens[$node->getEndTokenPos() + $iteration]->text) !== ',') {
break;
}

$tokens[$node->getEndTokenPos() + $iteration]->text = '';
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\DowngradePhp73\Tokenizer\FollowedByCommaAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\DowngradePhp73\Tokenizer\TrailingCommaRemover;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -22,7 +22,8 @@
final class DowngradeTrailingCommasInParamUseRector extends AbstractRector
{
public function __construct(
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer
private readonly FollowedByCommaAnalyzer $followedByCommaAnalyzer,
private readonly TrailingCommaRemover $trailingCommaRemover
) {
}

Expand Down Expand Up @@ -122,7 +123,7 @@ private function cleanTrailingComma(Closure|ClassMethod|Function_ $node, array $
return null;
}

$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->trailingCommaRemover->remove($this->file, $last);

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -55,11 +54,20 @@ public function refactor(Node $node): ?Node
}

$oldTokens = $this->file->getOldTokens();
if (isset($oldTokens[$node->getStartTokenPos()]) && (string) $oldTokens[$node->getStartTokenPos()] === '(') {
$startTokenPos = $node->getStartTokenPos();
$endTokenPos = $node->getEndTokenPos();

if (! isset($oldTokens[$startTokenPos], $oldTokens[$endTokenPos])) {
return null;
}

$node->var->setAttribute(AttributeKey::ORIGINAL_NODE, null);
if ((string) $oldTokens[$node->getStartTokenPos()] === '(') {
return null;
}

$oldTokens[$node->var->getStartTokenPos()]->text = '(' . $oldTokens[$node->var->getStartTokenPos()];
$oldTokens[$node->var->getEndTokenPos()]->text .= ')';

return $node;
}
}
Loading