-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Downgrade] [PHP 7.3] Downgrade trailing commas in function calls (#4796
) Co-authored-by: Tommy Quissens <tommy.quissens@storefront.be> Co-authored-by: rector-bot <tomas@getrector.org>
- Loading branch information
1 parent
c85ee2d
commit 8bfb36d
Showing
11 changed files
with
203 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
rules/downgrade-php73/src/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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]; | ||
} | ||
|
||
/** | ||
* @param FuncCall|MethodCall|StaticCall $node | ||
*/ | ||
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::FUNC_ARGS_TRAILING_COMMA, false); | ||
} | ||
|
||
return $node; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...eTrailingCommasInFunctionCallsRector/DowngradeTrailingCommasInFunctionCallsRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ests/Rector/FuncCall/DowngradeTrailingCommasInFunctionCallsRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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)}}"); | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters