Skip to content

Commit d6a7ed5

Browse files
authored
[code-quality] Add NewArrayItemConcatAssignToAssignRector (#8045)
1 parent bafb280 commit d6a7ed5

6 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\Fixture;
4+
5+
class NewArrayItem
6+
{
7+
public function run($return_data, $type, $name)
8+
{
9+
$return_data[$type][] .= $name;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\Fixture;
18+
19+
class NewArrayItem
20+
{
21+
public function run($return_data, $type, $name)
22+
{
23+
$return_data[$type][] = $name;
24+
}
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\Fixture;
4+
5+
class SkipExistingKey
6+
{
7+
public function run($return_data, $type, $name)
8+
{
9+
$return_data[$type] .= $name;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class NewArrayItemConcatAssignToAssignRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return RectorConfig::configure()
9+
->withRules([NewArrayItemConcatAssignToAssignRector::class]);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\CodeQuality\Rector\AssignOp;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ArrayDimFetch;
9+
use PhpParser\Node\Expr\Assign;
10+
use PhpParser\Node\Expr\AssignOp\Concat;
11+
use Rector\Rector\AbstractRector;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
15+
/**
16+
* @see \Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\NewArrayItemConcatAssignToAssignRectorTest
17+
*/
18+
final class NewArrayItemConcatAssignToAssignRector extends AbstractRector
19+
{
20+
public function getRuleDefinition(): RuleDefinition
21+
{
22+
return new RuleDefinition(
23+
'Change concat assign on a new array item to plain assign, as the new item is always null',
24+
[
25+
new CodeSample(
26+
<<<'CODE_SAMPLE'
27+
$values[] .= $name;
28+
CODE_SAMPLE
29+
,
30+
<<<'CODE_SAMPLE'
31+
$values[] = $name;
32+
CODE_SAMPLE
33+
),
34+
]
35+
);
36+
}
37+
38+
public function getNodeTypes(): array
39+
{
40+
return [Concat::class];
41+
}
42+
43+
/**
44+
* @param Concat $node
45+
*/
46+
public function refactor(Node $node): ?Assign
47+
{
48+
if (! $node->var instanceof ArrayDimFetch) {
49+
return null;
50+
}
51+
52+
if ($node->var->dim instanceof Node) {
53+
return null;
54+
}
55+
56+
return new Assign($node->var, $node->expr);
57+
}
58+
}

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Config\Level;
66

77
use Rector\CodeQuality\Rector\Assign\CombinedAssignRector;
8+
use Rector\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector;
89
use Rector\CodeQuality\Rector\Attribute\SortAttributeNamedArgsRector;
910
use Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector;
1011
use Rector\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector;
@@ -109,6 +110,7 @@ final class CodeQualityLevel
109110
*/
110111
public const array RULES = [
111112
CombinedAssignRector::class,
113+
NewArrayItemConcatAssignToAssignRector::class,
112114
SimplifyEmptyArrayCheckRector::class,
113115
ReplaceMultipleBooleanNotRector::class,
114116
ReplaceConstantBooleanNotRector::class,

0 commit comments

Comments
 (0)