-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add ChangeArrayPushToArrayAssignRector
- Loading branch information
1 parent
f9cb78b
commit e124806
Showing
5 changed files
with
153 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
packages/CodeQuality/src/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://stackoverflow.com/questions/559844/whats-better-to-use-in-php-array-value-or-array-pusharray-value | ||
* | ||
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\ChangeArrayPushToArrayAssignRector\ChangeArrayPushToArrayAssignRectorTest | ||
*/ | ||
final class ChangeArrayPushToArrayAssignRector extends AbstractRector | ||
{ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Change array_push() to direct variable assign', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$items = []; | ||
array_push($items, $item); | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$items = []; | ||
$items[] = $item; | ||
} | ||
} | ||
PHP | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [FuncCall::class]; | ||
} | ||
|
||
/** | ||
* @param FuncCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node, 'array_push')) { | ||
return null; | ||
} | ||
|
||
$arrayDimFetch = new ArrayDimFetch($node->args[0]->value); | ||
return new Assign($arrayDimFetch, $node->args[1]->value); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...or/FuncCall/ChangeArrayPushToArrayAssignRector/ChangeArrayPushToArrayAssignRectorTest.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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ChangeArrayPushToArrayAssignRector; | ||
|
||
use Iterator; | ||
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ChangeArrayPushToArrayAssignRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideDataForTest() | ||
*/ | ||
public function test(string $file): void | ||
{ | ||
$this->doTestFile($file); | ||
} | ||
|
||
public function provideDataForTest(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return ChangeArrayPushToArrayAssignRector::class; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...eQuality/tests/Rector/FuncCall/ChangeArrayPushToArrayAssignRector/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,29 @@ | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ChangeArrayPushToArrayAssignRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$items = []; | ||
array_push($items, $item); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\FuncCall\ChangeArrayPushToArrayAssignRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
$items = []; | ||
$items[] = $item; | ||
} | ||
} | ||
|
||
?> |