Skip to content

Commit

Permalink
[Downgrade PHP 7.3] Trailing commas in function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Quissens committed Sep 23, 2020
1 parent a73fc88 commit 838d315
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -150,6 +150,7 @@
"Rector\\NetteCodeQuality\\": "rules/nette-code-quality/src",
"Rector\\DowngradePhp71\\": "rules/downgrade-php71/src",
"Rector\\DowngradePhp72\\": "rules/downgrade-php72/src",
"Rector\\DowngradePhp73\\": "rules/downgrade-php73/src",
"Rector\\DowngradePhp74\\": "rules/downgrade-php74/src",
"Rector\\DowngradePhp80\\": "rules/downgrade-php80/src",
"Rector\\SymfonyPhpConfig\\": "rules/symfony-php-config/src"
Expand Down Expand Up @@ -238,6 +239,7 @@
"Rector\\NetteCodeQuality\\Tests\\": "rules/nette-code-quality/tests",
"Rector\\DowngradePhp71\\Tests\\": "rules/downgrade-php71/tests",
"Rector\\DowngradePhp72\\Tests\\": "rules/downgrade-php72/tests",
"Rector\\DowngradePhp73\\Tests\\": "rules/downgrade-php73/tests",
"Rector\\DowngradePhp74\\Tests\\": "rules/downgrade-php74/tests",
"Rector\\DowngradePhp80\\Tests\\": "rules/downgrade-php80/tests",
"Rector\\SymfonyPhpConfig\\Tests\\": "rules/symfony-php-config/tests",
Expand Down
11 changes: 11 additions & 0 deletions config/set/downgrade-php73.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeTrailingCommasInFunctionCallsRector::class);
};
@@ -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\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
* @see \Rector\DowngradePhp73\Tests\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector\DowngradeTrailingCommasInFunctionCallsRectorTest
*/
final class DowngradeTrailingCommasInFunctionCallsRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'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){
if(! $node->getAttribute(AttributeKey::PARENT_NODE) instanceof Node\Scalar\Encapsed){
$node->setAttribute(AttributeKey::ORIGINAL_NODE,null);
}
}
return $node;
}
}
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

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

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
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;
}

protected function getPhpVersion(): string
{
return '7.2';
// TODO: create constant
// return PhpVersionFeature::;
}
}
@@ -0,0 +1,33 @@
<?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',);
$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');
$this->setOnClick("[Zip ID: {$modelid}] {$e->getMessage($modelId)}");
}
}

?>

0 comments on commit 838d315

Please sign in to comment.