Find the best Rector rule to solve your problem
Replace key value on specific attribute to class constant
use Doctrine\ORM\Mapping\Column;
+use Doctrine\DBAL\Types\Types;
class SomeClass
{
- #[Column(type: "string")]
+ #[Column(type: Types::STRING)]
public $name;
}
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;
}
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>
*/
private $trainings = [];
+ /**
+ * @param Collection<int, Trainer> $trainings
+ */
public function setTrainings($trainings)
{
$this->trainings = $trainings;
}
}
Make maker bundle generate DateTime property accept DateTimeInterface too
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;
}
}
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<int, \App\Product>
*/
- private $products;
+ private \Doctrine\Common\Collections\Collection $products;
}
Move default value for entity property to constructor, the safest place
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();
+ }
}
Use Collection object type for one-to-many relations of Doctrine entity/ODM document
+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 = [];
+ private Collection $items;
+
+ public function __construct()
+ {
+ $this->items = new ArrayCollection();
+ }
}
Remove empty Table attribute on entities because it's useless
<?php
namespace RectorPrefix202411;
use RectorPrefix202411\Doctrine\ORM\Mapping as ORM;
-#[ORM\Table]
#[ORM\Entity]
class Product
{
}
\class_alias('Product', 'Product', \false);