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 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;
}
Improve @var, @param
and @return
types for Doctrine collections 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>|Trainer[]
*/
private $trainings = [];
}
Initialize collection property in Entity constructor
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\OneToMany(targetEntity="MarketingEvent")
*/
private $marketingEvents = [];
+
+ public function __construct()
+ {
+ $this->marketingEvents = new ArrayCollection();
+ }
}
Make maker bundle generate DateTime property accept DateTimeInterface too
- class:
Rector\Doctrine\CodeQuality\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class User
{
/**
- * @var DateTime|null
+ * @var DateTimeInterface|null
*/
private $bornAt;
public function setBornAt(DateTimeInterface $bornAt)
{
$this->bornAt = $bornAt;
}
}
Make nullability in setter class method with respect to property
- class:
Rector\Doctrine\CodeQuality\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Product
{
/**
* @ORM\ManyToOne(targetEntity="AnotherEntity")
* @ORM\JoinColumn(nullable=false)
*/
private $anotherEntity;
- public function setAnotherEntity(?AnotherEntity $anotherEntity)
+ public function setAnotherEntity(AnotherEntity $anotherEntity)
{
$this->anotherEntity = $anotherEntity;
}
}
Move default value for entity property to constructor, the safest place
- class:
Rector\Doctrine\CodeQuality\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class User
{
/**
* @var DateTimeInterface
*
- * @ORM\Column(type="datetime", nullable=false, options={"default"="now()"})
+ * @ORM\Column(type="datetime", nullable=false)
*/
- private $when = 'now()';
+ private $when;
+
+ public function __construct()
+ {
+ $this->when = new \DateTime();
+ }
}
Replace OrderBy Attribute ASC/DESC with class constant from Criteria
use Doctrine\ORM\Mapping as ORM;
class ReplaceOrderByAscWithClassConstant
{
- #[ORM\OrderBy(['createdAt' => 'ASC'])]
+ #[ORM\OrderBy(['createdAt' => \Doctrine\Common\Collections\Criteria::ASC])]
protected \DateTimeInterface $messages;
}
?>
Remove empty Table attribute on entities because it's useless
<?php
use Doctrine\ORM\Mapping as ORM;
-#[ORM\Table]
#[ORM\Entity]
class Product
{
}
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)
{
// ...
}
}
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;
}
Add typed property based on Doctrine collection
use Doctrine\ORM\Mapping as ORM;
use App\Entity\TrainingTerm;
/**
* @ORM\Entity
*/
class DoctrineCollection
{
/**
* @ORM\OneToMany(targetEntity="App\Entity\TrainingTerm", mappedBy="training")
* @var TrainingTerm[]|Collection
*/
- private $trainingTerms;
+ private \Doctrine\Common\Collections\Collection $trainingTerms;
}
Complete @var
annotations or types based on @ORM*toMany annotations or attributes
use Doctrine\ORM\Mapping as ORM;
class SimpleColumn
{
/**
* @ORM\OneToMany(targetEntity="App\Product")
+ * @var \Doctrine\Common\Collections\Collection<\App\Product>
*/
- private $products;
+ private \Doctrine\Common\Collections\Collection $products;
}
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")
*/
- private $company;
+ private ?\App\Company\Entity\Company $company = null;
}