Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ClassMetadata generic #2308

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Iterator/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace Doctrine\ODM\MongoDB\Iterator;

/**
* @template TKey
* @template TValue
* @template-extends \Iterator<TKey, TValue>
*/
interface Iterator extends \Iterator
{
/**
* @psalm-return array<TKey, TValue>
*/
public function toArray(): array;
}
18 changes: 17 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
* the serialized representation).
*
* @final
* @template T of object
* @template-implements BaseClassMetadata<T>
*/
/* final */ class ClassMetadata implements BaseClassMetadata
{
Expand Down Expand Up @@ -273,6 +275,7 @@
* READ-ONLY: The name of the document class.
*
* @var string
* @psalm-var class-string<T>
*/
public $name;

Expand All @@ -282,6 +285,7 @@
* as {@link $documentName}.
*
* @var string
* @psalm-var class-string
*/
public $rootDocumentName;

Expand All @@ -290,20 +294,23 @@
* (Optional).
*
* @var string|null
* @psalm-var class-string|null
*/
public $customRepositoryClassName;

/**
* READ-ONLY: The names of the parent classes (ancestors).
*
* @var array
* @psalm-var list<class-string>
*/
public $parentClasses = [];

/**
* READ-ONLY: The names of all subclasses (descendants).
*
* @var array
* @psalm-var list<class-string>
*/
public $subClasses = [];

Expand Down Expand Up @@ -505,6 +512,7 @@
* The ReflectionClass instance of the mapped class.
*
* @var ReflectionClass
* @psalm-var ReflectionClass<T>
*/
public $reflClass;

Expand All @@ -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<T> $documentName
*/
public function __construct(string $documentName)
{
Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 7 additions & 4 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ final class DocumentPersister
/** @var UnitOfWork */
private $uow;

/** @var ClassMetadata */
/**
* @var ClassMetadata
* @psalm-var ClassMetadata<T>
*/
private $class;

/** @var Collection|null */
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> $classMetadata The class metadata.
*/
public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $classMetadata)
{
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\<Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\<object\\>\\>\\) of method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getMetadataFactory\\(\\)$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/DocumentManager.php
Comment on lines +16 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even going to pretend I understand half of what's going on here, but just to confirm I got this somewhat right: the problem is that we can't correctly say "this manager returns a class metadata factory which creates ORM metadata instances", is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly, but in this case if I try to make ClassMetadataFactory generic, I get an error from phpstan I guess because of this generic template of generic maybe.


# 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\\(\\)$#"
Expand Down