Find the best Rector rule to solve your problem. Searching through 661 rules.
Found 19 rules:
Turns method names to new ones.
$someObject = new SomeExampleClass;
-$someObject->oldMethod();
+$someObject->newMethod();
Replaces defined class constants in their calls.
-$value = SomeClass::OLD_CONSTANT;
-$value = SomeClass::OTHER_OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
+$value = DifferentClass::NEW_CONSTANT;
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 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 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 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);
}
}
Replace EventSubscriberInterface with AsDoctrineListener attribute(s)
+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)
{
// ...
}
}
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 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;
}
}
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 RectorPrefix202503;
use RectorPrefix202503\Doctrine\ORM\Mapping as ORM;
-#[ORM\Table]
#[ORM\Entity]
class Product
{
}
\class_alias('Product', 'Product', \false);
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;
}
}