Find the best Rector rule to solve your problem. Searching through 1007 rules.
Found 56 rules:
Add column names argument to ArrayResult object
use Doctrine\DBAL\Cache\ArrayResult;
final class SomeClass
{
public function create(array $items)
{
- $result = new ArrayResult($items);
+ $result = new ArrayResult(array_keys($items[0] ?? []), $items);
return $result;
}
}
Change CompositeExpression ->addMultiple($parts) to ->with(...$parts)
use Doctrine\ORM\EntityRepository;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
class SomeRepository extends EntityRepository
{
public function getSomething($parts)
{
$compositeExpression = CompositeExpression::and('', ...$parts);
- $compositeExpression->addMultiple($parts);
+ $compositeExpression->with(...$parts);
}
}
Change executeQuery() with parameters to bindValue() with explicit values
use Doctrine\DBAL\Statement;
class SomeClass
{
public function run(Statement $statement, array $params): void
{
- $result = $statement->executeQuery($params);
+ foreach ($params as $key => $parameter) {
+ $statement->bindValue(is_int($key) ? $key + 1 : $key, $parameter);
+ }
+
+ $result = $statement->executeQuery();
}
}
Change QueryBuilder::resetQueryPart() to $queryBuilder->reset*()
class SomeRepository
{
public function resetQueryPart(\Doctrine\DBAL\Query\QueryBuilder $queryBuilder)
{
- $queryBuilder->resetQueryPart('distinct');
- $queryBuilder->resetQueryPart('where');
- $queryBuilder->resetQueryPart('groupBy');
- $queryBuilder->resetQueryPart('having');
- $queryBuilder->resetQueryPart('orderBy');
+ $queryBuilder->distinct(false);
+ $queryBuilder->resetWhere();
+ $queryBuilder->resetGroupBy();
+ $queryBuilder->resetHaving();
+ $queryBuilder->resetOrderBy();
}
}
Replace QueryBuilder::execute() with executeQuery() or executeStatement() based on the query type
$queryBuilder
->select('u.id')
->from('user', 'u')
- ->execute();
+ ->executeQuery();
Change the argument type for setParameters from array to ArrayCollection and Parameter calls
-$entityManager->createQueryBuilder()->setParameters([
- 'foo' => 'bar'
-]);
+$entityManager->createQueryBuilder()->setParameters(new \Doctrine\Common\Collections\ArrayCollection([
+ new \Doctrine\ORM\Query\Parameter('foo', 'bar')
+]));
Casts Doctrine Expr\x to string where necessary.
$statements->add(
$builder->expr()->like(
- $builder->expr()->lower($column),
- $builder->expr()->lower($builder->expr()->literal('%'.$like.'%'))
+ (string) $builder->expr()->lower($column),
+ (string) $builder->expr()->lower($builder->expr()->literal('%'.$like.'%'))
)
);
Replace Doctrine\ORM\Event\LifecycleEventArgs with specific event classes based on the function call
-use Doctrine\ORM\Event\LifecycleEventArgs;
+use Doctrine\ORM\Event\PrePersistEventArgs;
class PrePersistExample
{
- public function prePersist(LifecycleEventArgs $args)
+ public function prePersist(PrePersistEventArgs $args)
{
// ...
}
}
Change Doctrine\DBAL\Connection and Doctrine\DBAL\Driver\ResultStatement ->fetchAll() to ->fetchAllAssociative() and other replacements
use Doctrine\DBAL\Connection;
class SomeClass
{
public function run(Connection $connection)
{
- return $connection->fetchAll();
+ return $connection->fetchAllAssociative();
}
}
Extract array arg on QueryBuilder select, addSelect, groupBy, addGroupBy
function query(\Doctrine\DBAL\Query\QueryBuilder $queryBuilder)
{
- $query = $queryBuilder->select(['u.id', 'p.id']);
+ $query = $queryBuilder->select('u.id', 'p.id');
}
Change iterate() => toIterable() on query result
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Internal\Hydration\IterableResult;
class SomeRepository extends EntityRepository
{
- public function run(): IterateResult
+ public function run(): iterable
{
/** @var \Doctrine\ORM\AbstractQuery $query */
$query = $this->getEntityManager()->select('e')->from('entity')->getQuery();
- return $query->iterate();
+ return $query->toIterable();
}
}
Replace EventSubscriberInterface with #[AsDoctrineListener] attribute
+use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
-use Doctrine\Common\EventSubscriberInterface;
use Doctrine\ORM\Events;
-class MyEventSubscriber implements EventSubscriberInterface
+#[AsDoctrineListener(event: Events::postUpdate)]
+#[AsDoctrineListener(event: Events::prePersist)]
+class MyEventSubscriber
{
- public function getSubscribedEvents()
- {
- return array(
- Events::postUpdate,
- Events::prePersist,
- );
- }
-
public function postUpdate(PostUpdateEventArgs $args)
{
// ...
}
public function prePersist(PrePersistEventArgs $args)
{
// ...
}
}
Add @extends ServiceEntityRepository
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+/**
+ * @extends ServiceEntityRepository<\SomeEntity>
+ */
final class SomeRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, SomeEntity::class);
}
}
Replace ASC/DESC with enum Ordering in Criteria::orderBy method call, and remove usage of Criteria::ASC and Criteria::DESC constants elsewhere
use Doctrine\Common\Collections\Criteria;
$criteria = new Criteria();
-$criteria->orderBy(['someProperty' => 'ASC', 'anotherProperty' => 'DESC']);
+$criteria->orderBy(['someProperty' => \Doctrine\Common\Collections\Order::Ascending, 'anotherProperty' => \Doctrine\Common\Collections\Order::Descending]);
Change $this->getReference() in data fixtures to fill reference class directly
use Doctrine\Common\DataFixtures\AbstractDataFixture;
final class SomeFixture extends AbstractDataFixture
{
public function run(SomeEntity $someEntity)
{
- $someEntity->setSomePassedEntity($this->getReference('some-key'));
+ $someEntity->setSomePassedEntity($this->getReference('some-key'), SomeReference::class);
}
}
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 RectorPrefix202608\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
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)
{
}
}
Add more precise generics type to method that returns Collection
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
final class SomeClass
{
+ /**
+ * @return Collection<int, SomeClass>
+ */
public function getItems(): Collection
{
$collection = new ArrayCollection();
$collection->add(new SomeClass());
return $collection;
}
}
Change default value types to match Doctrine annotation type
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Column(name="is_old", type="boolean")
*/
- private $isOld = '0';
+ private $isOld = false;
}
Complete @var annotations or types based on @ORM\Column
use Doctrine\ORM\Mapping as ORM;
class SimpleColumn
{
/**
* @ORM\Column(type="string")
*/
- private $name;
+ private string|null $name = null;
}
Complete @var annotations or types based on @ORM\*toOne annotations or attributes
use Doctrine\ORM\Mapping as ORM;
class SimpleColumn
{
/**
* @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
* @ORM\JoinColumn(nullable=false)
*/
- private $company;
+ private ?\App\Company\Entity\Company $company = null;
}
Add full nullable type coverage for Doctrine entity based on docblocks. Useful stepping stone to add type coverage while keeping entities safe to read and write getter/setters
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class SomeEntity
{
/**
- * @var string
* @ORM\Id
* @ORM\Column(type="string")
* @ORM\GeneratedValue
*/
- private $name;
-
+ private ?string $name = null;
- /**
- * @return string
- */
- public function getName()
+ public function getName(): ?string
{
return $this->name;
}
- /**
- * @param string $name
- */
- public function setName($name): void
+ public function setName(?string $name): void
{
$this->name = $name;
}
}