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: 1 addition & 1 deletion config/sets/typed-collections-docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

declare(strict_types=1);

use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector;
use Rector\Config\RectorConfig;
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Source\RandomHouse;

final class MultipleItemsAdded
{
public function getItems(): Collection
{
$collection = new ArrayCollection();
$collection->add(new RandomHouse());

$collection->add(new RandomHouse());

return $collection;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Fixture;

use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Source\RandomHouse;

final class MultipleItemsAdded
{
/**
* @return \Doctrine\Common\Collections\Collection<int, \Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Source\RandomHouse>
*/
public function getItems(): Collection
{
$collection = new ArrayCollection();
$collection->add(new RandomHouse());

$collection->add(new RandomHouse());

return $collection;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,7 @@ public function refactor(Node $node): ?ClassMethod
return null;
}

$setTypeClasses = [];

foreach ($collectionAddMethodCalls as $collectionAddMethodCall) {
$setArg = $collectionAddMethodCall->getArgs()[0];
$setType = $this->getType($setArg->value);

if (! isset($setType->getObjectClassNames()[0])) {
continue;
}

$setTypeClasses[] = $setType->getObjectClassNames()[0];
}

$setTypeClasses = $this->resolveSetTypeClasses($collectionAddMethodCalls);
if (count($setTypeClasses) !== 1) {
return null;
}
Expand All @@ -175,4 +163,26 @@ public function refactor(Node $node): ?ClassMethod

return null;
}

/**
* @param MethodCall[] $collectionAddMethodCalls
* @return string[]
*/
private function resolveSetTypeClasses(array $collectionAddMethodCalls): array
{
$setTypeClasses = [];

foreach ($collectionAddMethodCalls as $collectionAddMethodCall) {
$setArg = $collectionAddMethodCall->getArgs()[0];
$setType = $this->getType($setArg->value);

if (! isset($setType->getObjectClassNames()[0])) {
continue;
}

$setTypeClasses[] = $setType->getObjectClassNames()[0];
}

return array_unique($setTypeClasses);
}
}