Skip to content

Commit

Permalink
[CodeQuality] Add ChangeArrayPushToArrayAssignRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 3, 2020
1 parent f9cb78b commit e124806
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/set/code-quality/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ services:
Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector: ~
Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector: ~
Rector\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector: ~
Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector: ~
22 changes: 21 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 414 Rectors Overview
# All 415 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -460,6 +460,26 @@ Convert [$this, "method"] to proper anonymous function

<br>

### `ChangeArrayPushToArrayAssignRector`

- class: `Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector`

Change array_push() to direct variable assign

```diff
class SomeClass
{
public function run()
{
$items = [];
- array_push($items, $item);
+ $items[] = $item;
}
}
```

<br>

### `CombinedAssignRector`

- class: `Rector\CodeQuality\Rector\Assign\CombinedAssignRector`
Expand Down
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);
}
}
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;
}
}
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;
}
}

?>

0 comments on commit e124806

Please sign in to comment.