Skip to content

Commit

Permalink
Add generic types (#2287)
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored Feb 9, 2021
1 parent 06a7f2f commit fe3913c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ public function unlock(object $document): void
* @param string $documentName The name of the Document.
*
* @return ObjectRepository The repository.
*
* @template T of object
* @psalm-param class-string<T> $documentName
*
* @psalm-return ObjectRepository<T>
*/
public function getRepository($documentName)
{
Expand Down Expand Up @@ -657,6 +662,11 @@ public function getPartialReference(string $documentName, $identifier): object
* @param mixed $id
* @param int $lockMode
* @param int $lockVersion
*
* @template T of object
* @psalm-param class-string<T> $className
*
* @psalm-return T|null
*/
public function find($className, $id, $lockMode = LockMode::NONE, $lockVersion = null): ?object
{
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
* The DocumentPersister is responsible for persisting documents.
*
* @internal
*
* @template T of object
*/
final class DocumentPersister
{
Expand Down Expand Up @@ -466,6 +468,10 @@ public function refresh(object $document): void
*
* @todo Check identity map? loadById method? Try to guess whether
* $criteria is the id?
*
* @psalm-param T|null $document
*
* @psalm-return T|null
*/
public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null): ?object
{
Expand Down Expand Up @@ -620,6 +626,10 @@ public function unlock(object $document): void
* @param array $hints Hints for document creation.
*
* @return object The filled and managed document object.
*
* @psalm-param T|null $document
*
* @psalm-return T
*/
private function createDocument(array $result, ?object $document = null, array $hints = []): object
{
Expand Down
12 changes: 11 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
*
* This class is designed for inheritance and users can subclass this class to
* write their own repositories with business-specific methods to locate documents.
*
* @template T of object
* @template-implements Selectable<int,T>
* @template-implements ObjectRepository<T>
*/
class DocumentRepository implements ObjectRepository, Selectable
{
/** @var string */
/**
* @var string
* @psalm-var class-string<T>
*/
protected $documentName;

/** @var DocumentManager */
Expand Down Expand Up @@ -227,6 +234,9 @@ public function matching(Criteria $criteria): ArrayCollection
return new ArrayCollection($iterator->toArray());
}

/**
* @psalm-return DocumentPersister<T>
*/
protected function getDocumentPersister(): DocumentPersister
{
return $this->uow->getDocumentPersister($this->documentName);
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/GridFSRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use Doctrine\Persistence\ObjectRepository;

/**
* @template T of object
* @template-extends ObjectRepository<T>
*/
interface GridFSRepository extends ObjectRepository
{
/**
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/RepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ interface RepositoryFactory
{
/**
* Gets the repository for a document class.
*
* @template T of object
* @psalm-param class-string<T> $documentName
* @psalm-return ObjectRepository<T>
*/
public function getRepository(DocumentManager $documentManager, string $documentName): ObjectRepository;
}
4 changes: 4 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/ViewRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\Persistence\ObjectRepository;

/**
* @template T of object
* @template-extends ObjectRepository<T>
*/
interface ViewRepository extends ObjectRepository
{
/**
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ public function getParentAssociation(object $document): ?array

/**
* Get the document persister instance for the given document name
*
* @template T of object
* @psalm-param class-string<T> $documentName
* @psalm-return Persisters\DocumentPersister<T>
*/
public function getDocumentPersister(string $documentName): Persisters\DocumentPersister
{
Expand Down Expand Up @@ -2611,6 +2615,11 @@ public function getClassNameForAssociation(array $mapping, $data): string

/**
* Creates a document. Used for reconstitution of documents during hydration.
*
* @template T of object
* @psalm-param class-string<T> $className
* @psalm-param T|null $document
* @psalm-return T
*/
public function getOrCreateDocument(string $className, array $data, array &$hints = [], ?object $document = null): object
{
Expand All @@ -2633,6 +2642,7 @@ public function getOrCreateDocument(string $className, array $data, array &$hint
}

if (! empty($hints[Query::HINT_READ_ONLY])) {
/** @psalm-var T $document */
$document = $class->newInstance();
$this->hydratorFactory->hydrate($document, $data, $hints);

Expand Down

0 comments on commit fe3913c

Please sign in to comment.