Find the best Rector rule to solve your problem. Searching through 728 rules.
Found 36 rules:
Change array to new ArrayCollection() on collection typed property
use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
final class SomeClass
{
/**
- * @var ArrayCollection<int, string>
+ * @var Collection<int, string>
*/
public $items;
public function someMethod()
{
- $this->items = [];
+ $this->items = new ArrayCollection([]);
}
}
Change $this->assertNull() on Collection object to $this->assertEmpty() in tests
use Doctrine\Common\Collections\Collection;
final class SomeClass extends \PHPUnit\Framework\TestCase
{
private Collection $items;
public function test(): void
{
- $this->assertNull($this->items);
+ $this->assertEmpty($this->items);
}
}
Change $this->assertSame(5, $collection->count()) to $this->assertCount(5, $collection) in tests
use Doctrine\Common\Collections\Collection;
final class SomeClass extends \PHPUnit\Framework\TestCase
{
private Collection $items;
public function test(): void
{
- $this->assertSame(5, $this->items->count());
+ $this->assertCount(5, $this->items);
}
}
Change in_array() on Collection typed property to ->contains() call
use Doctrine\Common\Collections\Collection;
final class InArrayOnAssignedVariable
{
/**
* @var Collection<int, string>
*/
public $items;
public function run()
{
- return in_array('item', $this->items);
+ return $this->items->contains('item');
}
}
Change array_merge() on Collection to ->toArray()
use Doctrine\Common\Collections\Collection;
final class SetFirstParameterArray
{
/**
* @var Collection<int, string>
*/
public $items;
public function merge()
{
- return array_merge([], $this->items);
+ return array_merge([], $this->items->toArray());
}
}
Change current() on Collection typed property to ->toArray() call, to always provide an array
use Doctrine\Common\Collections\Collection;
final class SimpleClass
{
/**
* @var Collection<int, string>
*/
public $items;
public function merge()
{
- return current($this->items);
+ return current($this->items->toArray());
}
}
Change array_map() and array_filter() on Collection typed property to ->toArray() call, to always provide an array
use Doctrine\Common\Collections\Collection;
final class ArrayMapOnAssignedVariable
{
/**
* @var Collection<int, string>
*/
public $items;
public function merge()
{
$items = $this->items;
- return array_map(fn ($item) => $item, $items);
+ return array_map(fn ($item) => $item, $items->toArray());
}
}
Narrow union type to Collection type in property docblock and native type declaration
use Doctrine\Common\Collections\Collection;
class SomeClass
{
/**
- * @var Collection|array
+ * @var Collection
*/
private $property;
}
Add "Doctrine\Common\Collections\Collection" type declaration, based on @ORM\*toMany and @ODM\*toMany annotations/attributes
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\ManyToMany;
+use Doctrine\Common\Collections\Collection;
class SimpleColumn
{
/**
* @ORM\OneToMany(targetEntity="App\Product")
*/
- private $products;
+ private Collection $products;
#[ManyToMany(targetEntity: 'App\Car')]
- private $cars;
+ private Collection $cars;
}
Change dim assign on a Collection to clear ->set() call
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
/**
* @var Collection<int, string>
*/
public $items;
public function setItem()
{
- $this->items['key'] = 'value';
+ $this->items->set('key', 'value');
}
}
Change add dim assign on collection to an->add() call
use Doctrine\Common\Collections\Collection;
final class SetFirstParameterArray
{
/**
* @var Collection<int, string>
*/
public $items;
public function setItems($item)
{
- $this->items[] = $item;
+ $this->items->add($item);
}
}
Remove nullsafe check on method call on a Collection type
use Doctrine\Common\Collections\Collection;
class SomeClass
{
private Collection $collection;
public function run()
{
- return $this->collection?->empty();
+ return $this->collection->empty();
}
}
Convert empty() on a Collection to ->isEmpty() call
class SomeClass
{
/**
* @var Collection<int, string>
*/
private $collection;
public function someMethod()
{
- if (empty($this->collection)) {
+ if ($this->collection->isEmpty()) {
// ...
}
}
}
Remove new ArrayCollection wrap on collection typed property, as it is always assigned in the constructor
-use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
public ?Collection $items;
public function someMethod()
{
- $values = new ArrayCollection($this->items);
+ $values = $this->items;
}
}
Remove coalesce assign on collection typed property, as it is always assigned in the constructor
-use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @ORM\Entity
*/
class SomeEntity
{
private $collection;
public function run()
{
- $items = $this->collection ?? new ArrayCollection();
+ $items = $this->collection;
}
}
Remove RectorPrefix202507\PHPUnit\Framework\Assert::assertNotNull() on a Collection type
use Doctrine\Common\Collections\Collection;
class SomeClass
{
public function run(Collection $collection): void
{
- Assert::assertNotNull($collection);
}
}
Remove collection identical to null from if || condition
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
private Collection $collection;
public function someMethod()
{
- if ($this->collection === null || $this->collection->isEmpty()) {
+ if ($this->collection->isEmpty()) {
return true;
}
return false;
}
}
Remove if instance of collection on already known Collection type
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
public ?Collection $items;
public function someMethod()
{
- $items = is_array($this->items) ? $this->items : $this->items->toArray();
+ $items = $this->items->toArray();
}
}
Remove useless isEmpty() check on collection with following new ArrayCollection() instance
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
private Collection $collection;
public function someMethod()
{
- if ($this->collection->isEmpty()) {
- $this->collection = new ArrayCollection();
- }
-
return $this->collection;
}
}
Remove if instance of collection on already known Collection type
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
public ?Collection $items;
public function someMethod()
{
- if ($this->items instanceof Collection) {
- $values = $this->items;
- }
+ $values = $this->items;
}
}
Improve @param Doctrine collection types to make them useful both for PHPStan and PHPStorm
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\OneToMany(targetEntity=Trainer::class, mappedBy="trainer")
* @var Collection|Trainer[]
*/
private $trainings = [];
+ /**
+ * @param Collection<int, Trainer> $trainings
+ */
public function setTrainings($trainings)
{
$this->trainings = $trainings;
}
}
Remove nullability from instantiated ArrayCollection properties, set it to Collection
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
class SomeClass
{
- private ?Collection $trainings = null;
+ private Collection $trainings;
public function __construct()
{
$this->trainings = new ArrayCollection();
}
}
Initialize Collection property in entity/ODM __construct()
+use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\OneToMany;
-use Doctrine\ORM\Mapping\Entity;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
#[Entity]
class SomeClass
{
#[OneToMany(targetEntity: 'SomeClass')]
private $items;
+
+ public function __construct()
+ {
+ $this->items = new ArrayCollection();
+ }
}
Improve Doctrine property @var collections type to make them useful both for PHPStan and PHPStorm
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\OneToMany(targetEntity=Trainer::class, mappedBy="trainer")
- * @var Collection|Trainer[]
+ * @var Collection<int, Trainer>
*/
private $trainings = [];
public function setTrainings($trainings)
{
$this->trainings = $trainings;
}
}
Adds @return PHPDoc type to Collection property getter by *ToMany annotation/attribute
-use App\Entity\Training;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
final class Trainer
{
/**
* @ORM\OneToMany(targetEntity=Training::class)
*/
private $trainings;
+ /**
+ * @return \Doctrine\Common\Collections\Collection<int, \App\Entity\Training>
+ */
public function getTrainings()
{
return $this->trainings;
}
}
Add return Collection docblock to method that returns a collection property
use Doctrine\Common\Collections\Collection;
final class OverrideMix
{
/**
* @var Collection<int, string>
*/
public $items;
/**
- * @return Collection|string[]
+ * @return Collection<int, string>
*/
public function getItems()
{
return $this->items;
}
}
Add default key to Collection generic type if missing in @param or @return of class method
use Doctrine\Common\Collections\Collection;
final class ReturnSimpleCollection
{
/**
- * @return Collection<string>
+ * @return Collection<int, string>
*/
public function someMethod()
{
}
}
Remove null from a nullable Collection, as empty ArrayCollection is preferred instead to keep property/class method type strict and always a collection
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
private $items;
- public function setItems(?Collection $items): void
+ public function setItems(Collection $items): void
{
$this->items = $items;
}
}
Narrow union type to Collection type in method docblock
use Doctrine\Common\Collections\Collection;
+use Doctrine\Common\Collections\ArrayCollection;
final class SomeClass
{
/**
- * @return Collection|array
+ * @return Collection
*/
public function getItems()
{
return [];
}
}
Add property collection type based on param type setter
use Doctrine\Common\Collections\Collection;
final class SetFirstParameterArray
{
/**
* @var Collection<int, string>
*/
public $items;
- public function setItems(array $items)
+ public function setItems(\Doctrine\Common\Collections\Collection $items)
{
$this->items = $items;
}
}
Add native return type to a Collection getter
use Doctrine\Common\Collections\Collection;
final class ReturnPropertyCollectionWithArrayTypeDeclaration
{
/**
* @var Collection<int, string>
*/
public $items;
- public function getItems(): array
+ public function getItems(): Collection
{
return $this->items;
}
}
Add native param type to a Collection setter
use Doctrine\Common\Collections\Collection;
final class SomeClass
{
private $items;
/**
* @param Collection<int, string> $items
*/
- public function setItems($items): void
+ public function setItems(Collection $items): void
{
$this->items = $items;
}
}
Remove new ArrayCollection() assigns outside constructor
+namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\RemoveNewArrayCollectionOutsideConstructorRector\Fixture;
+
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
final class NoAssignOutsideConstructor
{
public Collection $items;
public function anotherMethod()
{
- $this->items = new ArrayCollection();
}
}
Change return [] to return new ArrayCollection([]) in a method, that returns Collection type
+use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
final class ReturnArrayItems
{
public function getItems(): Collection
{
$items = [1, 2, 3];
$items[] = 4;
- return $items;
+ return new ArrayCollection($items);
}
}
Narrow ArrayCollection to Collection in class method and property
use Doctrine\Common\Collections\ArrayCollection;
final class ArrayCollectionGeneric
{
/**
- * @return ArrayCollection<int, string>
+ * @return \Doctrine\Common\Collections\Collection<int, string>
*/
public function someMethod()
{
}
}
Narrow union param docblock type to Collection type in class method
use Doctrine\Common\Collections\Collection;
class SomeClass
{
/**
- * @param Collection|array $items
+ * @param Collection $items
*/
public function run($items)
{
}
}