From a00ffa744d76c3e110c9ee36d59bed0ea154d26e Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 5 Feb 2022 20:15:02 +0100 Subject: [PATCH] Add native types to EntityRepository --- lib/Doctrine/ORM/EntityRepository.php | 130 +++++++++----------------- phpstan-baseline.neon | 1 - psalm-baseline.xml | 16 +--- 3 files changed, 45 insertions(+), 102 deletions(-) diff --git a/lib/Doctrine/ORM/EntityRepository.php b/lib/Doctrine/ORM/EntityRepository.php index 2a2701ef1cc..4a32668ff45 100644 --- a/lib/Doctrine/ORM/EntityRepository.php +++ b/lib/Doctrine/ORM/EntityRepository.php @@ -35,65 +35,39 @@ */ class EntityRepository implements ObjectRepository, Selectable { - /** - * @internal This property will be private in 3.0, call {@see getEntityName()} instead. - * - * @var string - */ - protected $_entityName; - - /** - * @internal This property will be private in 3.0, call {@see getEntityManager()} instead. - * - * @var EntityManagerInterface - */ - protected $_em; + /** @psalm-var class-string */ + private string $entityName; + private static ?Inflector $inflector = null; /** - * @internal This property will be private in 3.0, call {@see getClassMetadata()} instead. - * - * @var ClassMetadata + * @psalm-param ClassMetadata $class */ - protected $_class; - - /** @var Inflector|null */ - private static $inflector; - - public function __construct(EntityManagerInterface $em, ClassMetadata $class) - { - $this->_entityName = $class->name; - $this->_em = $em; - $this->_class = $class; + public function __construct( + private EntityManagerInterface $em, + private ClassMetadata $class + ) { + $this->entityName = $class->name; } /** * Creates a new QueryBuilder instance that is prepopulated for this entity name. - * - * @param string $alias - * @param string|null $indexBy The index for the from. - * - * @return QueryBuilder */ - public function createQueryBuilder($alias, $indexBy = null) + public function createQueryBuilder(string $alias, ?string $indexBy = null): QueryBuilder { - return $this->_em->createQueryBuilder() + return $this->em->createQueryBuilder() ->select($alias) - ->from($this->_entityName, $alias, $indexBy); + ->from($this->entityName, $alias, $indexBy); } /** * Creates a new result set mapping builder for this entity. * * The column naming strategy is "INCREMENT". - * - * @param string $alias - * - * @return ResultSetMappingBuilder */ - public function createResultSetMappingBuilder($alias) + public function createResultSetMappingBuilder(string $alias): ResultSetMappingBuilder { - $rsm = new ResultSetMappingBuilder($this->_em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT); - $rsm->addRootEntityFromClassMetadata($this->_entityName, $alias); + $rsm = new ResultSetMappingBuilder($this->em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT); + $rsm->addRootEntityFromClassMetadata($this->entityName, $alias); return $rsm; } @@ -101,19 +75,18 @@ public function createResultSetMappingBuilder($alias) /** * Finds an entity by its primary key / identifier. * - * @param mixed $id The identifier. - * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants - * or NULL if no specific lock mode should be used - * during the search. - * @param int|null $lockVersion The lock version. + * @param mixed $id The identifier. + * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants + * or NULL if no specific lock mode should be used + * during the search. * @psalm-param LockMode::*|null $lockMode * * @return object|null The entity instance or NULL if the entity can not be found. * @psalm-return ?T */ - public function find($id, $lockMode = null, $lockVersion = null) + public function find($id, ?int $lockMode = null, ?int $lockVersion = null): ?object { - return $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion); + return $this->em->find($this->entityName, $id, $lockMode, $lockVersion); } /** @@ -121,7 +94,7 @@ public function find($id, $lockMode = null, $lockVersion = null) * * @psalm-return list The entities. */ - public function findAll() + public function findAll(): array { return $this->findBy([]); } @@ -137,9 +110,9 @@ public function findAll() * @return object[] The objects. * @psalm-return list */ - public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array { - $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); + $persister = $this->em->getUnitOfWork()->getEntityPersister($this->entityName); return $persister->loadAll($criteria, $orderBy, $limit, $offset); } @@ -153,9 +126,9 @@ public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $ * @return object|null The entity instance or NULL if the entity can not be found. * @psalm-return ?T */ - public function findOneBy(array $criteria, ?array $orderBy = null) + public function findOneBy(array $criteria, ?array $orderBy = null): ?object { - $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); + $persister = $this->em->getUnitOfWork()->getEntityPersister($this->entityName); return $persister->load($criteria, null, null, [], null, 1, $orderBy); } @@ -169,23 +142,20 @@ public function findOneBy(array $criteria, ?array $orderBy = null) * * @todo Add this method to `ObjectRepository` interface in the next major release */ - public function count(array $criteria = []) + public function count(array $criteria = []): int { - return $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName)->count($criteria); + return $this->em->getUnitOfWork()->getEntityPersister($this->entityName)->count($criteria); } /** * Adds support for magic method calls. * - * @param string $method * @param mixed[] $arguments * @psalm-param list $arguments * - * @return mixed The returned value from the resolved method. - * * @throws BadMethodCallException If the method called is invalid. */ - public function __call($method, $arguments) + public function __call(string $method, array $arguments): mixed { if (str_starts_with($method, 'findBy')) { return $this->resolveMagicCall('findBy', substr($method, 6), $arguments); @@ -207,47 +177,37 @@ public function __call($method, $arguments) } /** - * @return string + * @psalm-return class-string */ - protected function getEntityName() + protected function getEntityName(): string { - return $this->_entityName; + return $this->entityName; } - /** - * @return string - */ - public function getClassName() + public function getClassName(): string { return $this->getEntityName(); } - /** - * @return EntityManagerInterface - */ - protected function getEntityManager() + protected function getEntityManager(): EntityManagerInterface { - return $this->_em; + return $this->em; } - /** - * @return ClassMetadata - */ - protected function getClassMetadata() + protected function getClassMetadata(): ClassMetadata { - return $this->_class; + return $this->class; } /** * Select all elements from a selectable that match the expression and * return a new collection containing these elements. * - * @return AbstractLazyCollection * @psalm-return AbstractLazyCollection&Selectable */ - public function matching(Criteria $criteria) + public function matching(Criteria $criteria): AbstractLazyCollection { - $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); + $persister = $this->em->getUnitOfWork()->getEntityPersister($this->entityName); return new LazyCriteriaCollection($persister, $criteria); } @@ -259,26 +219,22 @@ public function matching(Criteria $criteria) * @param string $by The property name used as condition * @psalm-param list $arguments The arguments to pass at method call * - * @return mixed - * * @throws InvalidMagicMethodCall If the method called is invalid or the * requested field/association does not exist. */ - private function resolveMagicCall(string $method, string $by, array $arguments) + private function resolveMagicCall(string $method, string $by, array $arguments): mixed { if (! $arguments) { throw InvalidMagicMethodCall::onMissingParameter($method . $by); } - if (self::$inflector === null) { - self::$inflector = InflectorFactory::create()->build(); - } + self::$inflector ??= InflectorFactory::create()->build(); $fieldName = lcfirst(self::$inflector->classify($by)); - if (! ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName))) { + if (! ($this->class->hasField($fieldName) || $this->class->hasAssociation($fieldName))) { throw InvalidMagicMethodCall::becauseFieldNotFoundIn( - $this->_entityName, + $this->entityName, $fieldName, $method . $by ); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5e41948272b..ec3a928ffd0 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1374,4 +1374,3 @@ parameters: message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$subClasses\\.$#" count: 1 path: lib/Doctrine/ORM/Utility/HierarchyDiscriminatorResolver.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5546adaaf68..aa0aa1b6122 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -288,26 +288,14 @@ - - $this->_entityName - $this->_entityName - $this->_entityName - $this->_entityName - $this->_entityName - - + $persister->load($criteria, null, null, [], null, 1, $orderBy) - $this->_em->find($this->_entityName, $id, $lockMode, $lockVersion) new LazyCriteriaCollection($persister, $criteria) - - ?T + ?T AbstractLazyCollection<int, T>&Selectable<int, T> - - string -