From b374fa61518501ecda4bd78beaa60800eaee68a3 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Fri, 21 May 2021 00:39:04 +0200 Subject: [PATCH 1/2] Make ClassMetadata generic --- .../ODM/MongoDB/Mapping/ClassMetadata.php | 18 +++++++++++++++++- .../MongoDB/Persisters/DocumentPersister.php | 11 +++++++---- .../MongoDB/Repository/DocumentRepository.php | 2 ++ phpstan-baseline.neon | 6 ++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index 891bc51bcb..dd00cc49f4 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -58,6 +58,8 @@ * the serialized representation). * * @final + * @template T of object + * @template-implements BaseClassMetadata */ /* final */ class ClassMetadata implements BaseClassMetadata { @@ -273,6 +275,7 @@ * READ-ONLY: The name of the document class. * * @var string + * @psalm-var class-string */ public $name; @@ -282,6 +285,7 @@ * as {@link $documentName}. * * @var string + * @psalm-var class-string */ public $rootDocumentName; @@ -290,6 +294,7 @@ * (Optional). * * @var string|null + * @psalm-var class-string|null */ public $customRepositoryClassName; @@ -297,6 +302,7 @@ * READ-ONLY: The names of the parent classes (ancestors). * * @var array + * @psalm-var list */ public $parentClasses = []; @@ -304,6 +310,7 @@ * READ-ONLY: The names of all subclasses (descendants). * * @var array + * @psalm-var list */ public $subClasses = []; @@ -505,6 +512,7 @@ * The ReflectionClass instance of the mapped class. * * @var ReflectionClass + * @psalm-var ReflectionClass */ public $reflClass; @@ -521,12 +529,17 @@ /** @var ReflectionService */ private $reflectionService; - /** @var string|null */ + /** + * @var string|null + * @psalm-var class-string|null + */ private $rootClass; /** * Initializes a new ClassMetadata instance that will hold the object-document mapping * metadata of the class with the given name. + * + * @psalm-param class-string $documentName */ public function __construct(string $documentName) { @@ -2165,9 +2178,12 @@ public function __wakeup() /** * Creates a new instance of the mapped class, without invoking the constructor. + * + * @psalm-return T */ public function newInstance(): object { + /** @psalm-var T */ return $this->instantiator->instantiate($this->name); } diff --git a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php index ca0cbe3154..47badc4127 100644 --- a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php +++ b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php @@ -80,7 +80,10 @@ final class DocumentPersister /** @var UnitOfWork */ private $uow; - /** @var ClassMetadata */ + /** + * @var ClassMetadata + * @psalm-var ClassMetadata + */ private $class; /** @var Collection|null */ @@ -621,9 +624,9 @@ public function unlock(object $document): void /** * Creates or fills a single document object from an query result. * - * @param array $result The query result. - * @param object $document The document object to fill, if any. - * @param array $hints Hints for document creation. + * @param array $result The query result. + * @param object|null $document The document object to fill, if any. + * @param array $hints Hints for document creation. * * @return object The filled and managed document object. * diff --git a/lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php b/lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php index 39c02b82ae..53f25624ac 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php @@ -58,6 +58,8 @@ class DocumentRepository implements ObjectRepository, Selectable * @param DocumentManager $dm The DocumentManager to use. * @param UnitOfWork $uow The UnitOfWork to use. * @param ClassMetadata $classMetadata The class metadata. + * + * @psalm-param ClassMetadata $classMetadata The class metadata. */ public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $classMetadata) { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 37516471fd..61de201575 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -13,6 +13,12 @@ parameters: - lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php - lib/Doctrine/ODM/MongoDB/DocumentManager.php + # This cannot be solved the way it is, see https://github.com/vimeo/psalm/issues/5788 + - + message: "#^Return type \\(Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadataFactory\\) of method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getMetadataFactory\\(\\) should be compatible with return type \\(Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadataFactory\\\\>\\) of method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getMetadataFactory\\(\\)$#" + count: 1 + path: lib/Doctrine/ODM/MongoDB/DocumentManager.php + # The limit option in GeoNear has been removed in MongoDB 4.2 in favor of $limit stage - message: "#^Return type \\(Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\GeoNear\\) of method Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\GeoNear\\:\\:limit\\(\\) should be compatible with return type \\(Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\\\Limit\\) of method Doctrine\\\\ODM\\\\MongoDB\\\\Aggregation\\\\Stage\\:\\:limit\\(\\)$#" From 23b3bcfa8d59e646cc0bf225fb132d18f9f89343 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Fri, 21 May 2021 00:53:47 +0200 Subject: [PATCH 2/2] Add generics to Iterator --- lib/Doctrine/ODM/MongoDB/Iterator/Iterator.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/Doctrine/ODM/MongoDB/Iterator/Iterator.php b/lib/Doctrine/ODM/MongoDB/Iterator/Iterator.php index d58fba5565..e4789c8b41 100644 --- a/lib/Doctrine/ODM/MongoDB/Iterator/Iterator.php +++ b/lib/Doctrine/ODM/MongoDB/Iterator/Iterator.php @@ -4,7 +4,15 @@ namespace Doctrine\ODM\MongoDB\Iterator; +/** + * @template TKey + * @template TValue + * @template-extends \Iterator + */ interface Iterator extends \Iterator { + /** + * @psalm-return array + */ public function toArray(): array; }