Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\Fixture;

class NewArrayItem
{
public function run($return_data, $type, $name)
{
$return_data[$type][] .= $name;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\Fixture;

class NewArrayItem
{
public function run($return_data, $type, $name)
{
$return_data[$type][] = $name;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\Fixture;

class SkipExistingKey
{
public function run($return_data, $type, $name)
{
$return_data[$type] .= $name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NewArrayItemConcatAssignToAssignRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([NewArrayItemConcatAssignToAssignRector::class]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Rector\CodeQuality\Rector\AssignOp;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp\Concat;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector\NewArrayItemConcatAssignToAssignRectorTest
*/
final class NewArrayItemConcatAssignToAssignRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change concat assign on a new array item to plain assign, as the new item is always null',
[
new CodeSample(
<<<'CODE_SAMPLE'
$values[] .= $name;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$values[] = $name;
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [Concat::class];
}

/**
* @param Concat $node
*/
public function refactor(Node $node): ?Assign
{
if (! $node->var instanceof ArrayDimFetch) {
return null;
}

if ($node->var->dim instanceof Node) {
return null;
}

return new Assign($node->var, $node->expr);
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Config\Level;

use Rector\CodeQuality\Rector\Assign\CombinedAssignRector;
use Rector\CodeQuality\Rector\AssignOp\NewArrayItemConcatAssignToAssignRector;
use Rector\CodeQuality\Rector\Attribute\SortAttributeNamedArgsRector;
use Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector;
use Rector\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector;
Expand Down Expand Up @@ -109,6 +110,7 @@ final class CodeQualityLevel
*/
public const array RULES = [
CombinedAssignRector::class,
NewArrayItemConcatAssignToAssignRector::class,
SimplifyEmptyArrayCheckRector::class,
ReplaceMultipleBooleanNotRector::class,
ReplaceConstantBooleanNotRector::class,
Expand Down
Loading