Skip to content

Commit

Permalink
Fix types on EntityRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Feb 5, 2022
1 parent 0c4e739 commit 91c2042
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function lock($entity, $lockMode, $lockVersion = null)
* @return ObjectRepository|EntityRepository The repository class.
* @psalm-return EntityRepository<T>
*
* @template T
* @template T of object
*/
public function getRepository($entityName)
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface EntityManagerInterface extends ObjectManager
*
* @psalm-return EntityRepository<T>
*
* @template T
* @template T of object
*/
public function getRepository($className);

Expand Down
36 changes: 22 additions & 14 deletions lib/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM;

use BadMethodCallException;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\DBAL\LockMode;
Expand All @@ -30,28 +29,37 @@
* This class is designed for inheritance and users can subclass this class to
* write their own repositories with business-specific methods to locate entities.
*
* @template T
* @template T of object
* @template-implements Selectable<int,T>
* @template-implements ObjectRepository<T>
*/
class EntityRepository implements ObjectRepository, Selectable
{
/** @var string */
/**
* @internal This property will be private in 3.0, call {@see getEntityName()} instead.
*
* @var string
*/
protected $_entityName;

/** @var EntityManagerInterface */
/**
* @internal This property will be private in 3.0, call {@see getEntityManager()} instead.
*
* @var EntityManagerInterface
*/
protected $_em;

/** @var ClassMetadata */
/**
* @internal This property will be private in 3.0, call {@see getClassMetadata()} instead.
*
* @var ClassMetadata
*/
protected $_class;

/** @var Inflector */
/** @var Inflector|null */
private static $inflector;

/**
* Initializes a new <tt>EntityRepository</tt>.
*/
public function __construct(EntityManagerInterface $em, Mapping\ClassMetadata $class)
public function __construct(EntityManagerInterface $em, ClassMetadata $class)
{
$this->_entityName = $class->name;
$this->_em = $em;
Expand All @@ -61,8 +69,8 @@ public function __construct(EntityManagerInterface $em, Mapping\ClassMetadata $c
/**
* Creates a new QueryBuilder instance that is prepopulated for this entity name.
*
* @param string $alias
* @param string $indexBy The index for the from.
* @param string $alias
* @param string|null $indexBy The index for the from.
*
* @return QueryBuilder
*/
Expand Down Expand Up @@ -290,7 +298,7 @@ protected function getEntityManager()
}

/**
* @return Mapping\ClassMetadata
* @return ClassMetadata
*/
protected function getClassMetadata()
{
Expand All @@ -302,7 +310,7 @@ protected function getClassMetadata()
* return a new collection containing these elements.
*
* @return LazyCriteriaCollection
* @psalm-return Collection<int, T>
* @psalm-return LazyCriteriaCollection<int, T>
*/
public function matching(Criteria $criteria)
{
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/ORM/LazyCriteriaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
use Doctrine\ORM\Persisters\Entity\EntityPersister;
use ReturnTypeWillChange;

use function assert;

/**
* A lazy collection that allows a fast count when using criteria object
* Once count gets executed once without collection being initialized, result
* is cached and returned on subsequent calls until collection gets loaded,
* then returning the number of loaded results.
*
* @template TKey of array-key
* @template TValue of object
* @extends AbstractLazyCollection<TKey, TValue>
* @implements Selectable<TKey, TValue>
*/
class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
{
Expand Down Expand Up @@ -72,6 +79,7 @@ public function isEmpty()
* Do an optimized search of an element
*
* @param object $element
* @psalm-param TValue $element
*
* @return bool
*/
Expand All @@ -90,6 +98,7 @@ public function contains($element)
public function matching(Criteria $criteria)
{
$this->initialize();
assert($this->collection instanceof Selectable);

return $this->collection->matching($criteria);
}
Expand Down
14 changes: 7 additions & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,17 @@ parameters:
path: lib/Doctrine/ORM/EntityManagerInterface.php

-
message: "#^Method Doctrine\\\\ORM\\\\EntityRepository\\:\\:find\\(\\) should return T\\|null but returns object\\|null\\.$#"
message: "#^Method Doctrine\\\\ORM\\\\EntityRepository\\:\\:find\\(\\) should return T of object\\|null but returns object\\|null\\.$#"
count: 1
path: lib/Doctrine/ORM/EntityRepository.php

-
message: "#^Method Doctrine\\\\ORM\\\\EntityRepository\\:\\:findOneBy\\(\\) should return T\\|null but returns object\\|null\\.$#"
message: "#^Method Doctrine\\\\ORM\\\\EntityRepository\\:\\:findOneBy\\(\\) should return T of object\\|null but returns object\\|null\\.$#"
count: 1
path: lib/Doctrine/ORM/EntityRepository.php

-
message: "#^Method Doctrine\\\\ORM\\\\EntityRepository\\:\\:matching\\(\\) should return Doctrine\\\\ORM\\\\LazyCriteriaCollection\\<int, T of object\\> but returns Doctrine\\\\ORM\\\\LazyCriteriaCollection\\<\\(int\\|string\\), object\\>\\.$#"
count: 1
path: lib/Doctrine/ORM/EntityRepository.php

Expand Down Expand Up @@ -220,11 +225,6 @@ parameters:
count: 1
path: lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php

-
message: "#^Call to an undefined method Doctrine\\\\Common\\\\Collections\\\\Collection\\<\\(int\\|string\\), mixed\\>\\:\\:matching\\(\\)\\.$#"
count: 1
path: lib/Doctrine/ORM/LazyCriteriaCollection.php

-
message: "#^Offset 'indexes' on array\\{name\\: string, schema\\: string, indexes\\: array, uniqueConstraints\\: array, options\\: array\\<string, mixed\\>, quoted\\?\\: bool\\} in isset\\(\\) always exists and is not nullable\\.$#"
count: 1
Expand Down
11 changes: 1 addition & 10 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,6 @@
<code>$this-&gt;_entityName</code>
<code>$this-&gt;_entityName</code>
</ArgumentTypeCoercion>
<DocblockTypeContradiction occurrences="1">
<code>self::$inflector === null</code>
</DocblockTypeContradiction>
<InvalidReturnStatement occurrences="3">
<code>$persister-&gt;load($criteria, null, null, [], null, 1, $orderBy)</code>
<code>$this-&gt;_em-&gt;find($this-&gt;_entityName, $id, $lockMode, $lockVersion)</code>
Expand All @@ -468,7 +465,7 @@
<InvalidReturnType occurrences="3">
<code>?T</code>
<code>?T</code>
<code>Collection&lt;int, T&gt;</code>
<code>LazyCriteriaCollection&lt;int, T&gt;</code>
</InvalidReturnType>
<LessSpecificImplementedReturnType occurrences="1">
<code>string</code>
Expand Down Expand Up @@ -607,15 +604,9 @@
</PropertyNotSetInConstructor>
</file>
<file src="lib/Doctrine/ORM/LazyCriteriaCollection.php">
<MoreSpecificImplementedParamType occurrences="1">
<code>$element</code>
</MoreSpecificImplementedParamType>
<PropertyNotSetInConstructor occurrences="1">
<code>LazyCriteriaCollection</code>
</PropertyNotSetInConstructor>
<UndefinedInterfaceMethod occurrences="1">
<code>matching</code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php">
<ArgumentTypeCoercion occurrences="2">
Expand Down

0 comments on commit 91c2042

Please sign in to comment.