Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Downgrade PHP 7.3] Trailing commas in function calls #4273

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -18,7 +18,7 @@
"jean85/pretty-package-versions": "^1.5.1",
"nette/robot-loader": "^3.2",
"nette/utils": "^3.1",
"nikic/php-parser": "4.10.2",
"nikic/php-parser": "4.10.3",
"phpstan/phpdoc-parser": "^0.4.9",
"phpstan/phpstan": "^0.12.52",
"phpstan/phpstan-phpunit": "^0.12.16",
Expand Down
2 changes: 2 additions & 0 deletions config/set/downgrade-php73.php
Expand Up @@ -4,6 +4,7 @@

use Rector\Downgrade\Rector\LNumber\ChangePhpVersionInPlatformCheckRector;
use Rector\DowngradePhp73\Rector\List_\DowngradeListReferenceAssignmentRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
use Rector\DowngradePhp73\Rector\String_\DowngradeFlexibleHeredocSyntaxRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand All @@ -15,4 +16,5 @@
->call('configure', [[
ChangePhpVersionInPlatformCheckRector::TARGET_PHP_VERSION => 70300,
]]);
$services->set(DowngradeTrailingCommasInFunctionCallsRector::class);
};
5 changes: 5 additions & 0 deletions packages/node-type-resolver/src/Node/AttributeKey.php
Expand Up @@ -227,4 +227,9 @@ final class AttributeKey
* @var string
*/
public const IS_FRESH_NODE = 'is_fresh_node';

/**
* @var string
*/
public const TRAILING_COMMA = 'trailing_comma';
}
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp73\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\DowngradeTrailingCommasInFunctionCallsRectorTest
*/
final class DowngradeTrailingCommasInFunctionCallsRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove trailing commas in function calls', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(string $value)
{
$compacted = compact(
'posts',
'units',
);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(string $value)
{
$compacted = compact(
'posts',
'units'
);
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class, MethodCall::class, StaticCall::class];
}

public function refactor(Node $node): ?Node
{
if($node->args){
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$last = $node->args[array_key_last($node->args)];
$last->setAttribute(AttributeKey::TRAILING_COMMA, false);
}
return $node;
}
}
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;

use Iterator;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeTrailingCommasInFunctionCallsRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP >= 7.3
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return DowngradeTrailingCommasInFunctionCallsRector::class;
}
}
@@ -0,0 +1,37 @@
<?php

namespace Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\Fixture;

class FixtureClass
{
public function run()
{
compact('posts','units',);
$this->setData('posts','units',);
self::run('posts','units',);
self::run('posts',
'units',);
$this->setOnClick("[Zip ID: {$modelid}] {$e->getMessage($modelId,)}",);
}
}

?>
-----
<?php

namespace Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\Fixture;

class FixtureClass
{
public function run()
{
compact('posts', 'units');
$this->setData('posts', 'units');
self::run('posts', 'units');
self::run('posts',
'units');
$this->setOnClick("[Zip ID: {$modelid}] {$e->getMessage($modelId)}");
}
}

?>
16 changes: 16 additions & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Expand Up @@ -590,4 +590,20 @@ private function wrapValueWith(String_ $string, string $wrap): string
{
return $wrap . $string->value . $wrap;
}

protected function pCommaSeparated(array $nodes): string
{
$result = parent::pCommaSeparated($nodes);

$last = end($nodes);

if ($last instanceof Node) {
$trailingComma = $last->getAttribute(AttributeKey::TRAILING_COMMA);
if ($trailingComma === false) {
$result = rtrim($result, ',');
}
}

return $result;
}
}