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
2 changes: 2 additions & 0 deletions config/sets/typed-collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\Doctrine\TypedCollections\Rector\Assign\ArrayDimFetchAssignToAddCollectionCallRector;
use Rector\Doctrine\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompleteParamDocblockFromSetterToCollectionRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompletePropertyDocblockFromToManyRector;
use Rector\Doctrine\TypedCollections\Rector\Class_\CompleteReturnDocblockFromToManyRector;
Expand Down Expand Up @@ -47,6 +48,7 @@

// collection method calls
ArrayDimFetchAssignToAddCollectionCallRector::class,
ArrayOffsetSetToSetCollectionCallRector::class,
ArrayMapOnCollectionToArrayRector::class,
ArrayMergeOnCollectionToArrayRector::class,
EmptyOnCollectionToIsEmptyCallRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector;

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

final class ArrayOffsetSetToSetCollectionCallRectorTest 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,24 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector\Fixture;

final class SkipNonCollection
{
/**
* @var array<int, string>
*/
public $items;

public function setItems()
{
$items = $this->getItems();
$items[5] = 10;
}

private function getItems()
{
return $this->items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class SkipWithoutDim
{
/**
* @var Collection<int, string>
*/
public $items;

public function setItems()
{
$items = $this->getItems();
$items[] = 10;
}

private function getItems(): Collection
{
return $this->items;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class SomeClass
{
/**
* @var Collection<int, string>
*/
public $items;

public function setItems()
{
$items = $this->getItems();
$items[5] = 10;
}

private function getItems(): Collection
{
return $this->items;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector\Fixture;

use Doctrine\Common\Collections\Collection;

final class SomeClass
{
/**
* @var Collection<int, string>
*/
public $items;

public function setItems()
{
$items = $this->getItems();
$items->set(5, 10);
}

private function getItems(): Collection
{
return $this->items;
}
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ArrayOffsetSetToSetCollectionCallRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\TypedCollections\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use Rector\Doctrine\TypedCollections\TypeAnalyzer\CollectionTypeDetector;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Doctrine\Tests\TypedCollections\Rector\Assign\ArrayOffsetSetToSetCollectionCallRector\ArrayOffsetSetToSetCollectionCallRectorTest
*/
final class ArrayOffsetSetToSetCollectionCallRector extends AbstractRector
{
public function __construct(
private readonly CollectionTypeDetector $collectionTypeDetector
) {

}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change dim assign on a Collection to clear ->set() call',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Doctrine\Common\Collections\Collection;

final class SomeClass
{
/**
* @var Collection<int, string>
*/
public $items;

public function setItem()
{
$this->items['key'] = 'value';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\Common\Collections\Collection;

final class SomeClass
{
/**
* @var Collection<int, string>
*/
public $items;

public function setItem()
{
$this->items->set('key', 'value');
}
}
CODE_SAMPLE
)]
);
}

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

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

$arrayDimFetch = $node->var;
if (! $arrayDimFetch->dim instanceof Expr) {
return null;
}

$assignedExpr = $arrayDimFetch->var;
if (! $this->collectionTypeDetector->isCollectionType($assignedExpr)) {
return null;
}

$args = [new Arg($arrayDimFetch->dim), new Arg($node->expr)];

return new MethodCall($assignedExpr, 'set', $args);
}
}