Skip to content

Commit

Permalink
Fix generic type implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Sep 16, 2023
1 parent 85f7831 commit 61c77d1
Show file tree
Hide file tree
Showing 30 changed files with 79 additions and 29 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ parameters:
treatPhpDocTypesAsCertain: false
checkMissingVarTagTypehint: true
checkMissingTypehints: true
# @todo: Remove the "checkGenericClassInNonGenericObjectType" definition.
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- '#^Property Gedmo\\Tests\\.+\\Fixture\\[^:]+::\$\w+ is never written, only read\.$#'
- '#^Property Gedmo\\Tests\\.+\\Fixture\\[^:]+::\$\w+ is never read, only written\.$#'
Expand Down
1 change: 1 addition & 0 deletions src/AbstractTrackingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getSubscribedEvents()
* Maps additional metadata for the object.
*
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down
8 changes: 5 additions & 3 deletions src/Loggable/LoggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace Gedmo\Loggable;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Loggable\Entity\LogEntry;
Expand Down Expand Up @@ -125,6 +126,7 @@ public function getSubscribedEvents()
* Maps additional metadata
*
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down Expand Up @@ -312,7 +314,7 @@ protected function createLogEntry($action, $object, LoggableAdapter $ea)
if ($config = $this->getConfiguration($om, $meta->getName())) {
$logEntryClass = $this->getLogEntryClass($ea, $meta->getName());
$logEntryMeta = $om->getClassMetadata($logEntryClass);
/** @var LogEntryInterface $logEntry */
/** @var LogEntryInterface<T> $logEntry */
$logEntry = $logEntryMeta->newInstance();

$logEntry->setAction($action);
Expand All @@ -322,7 +324,7 @@ protected function createLogEntry($action, $object, LoggableAdapter $ea)

// check for the availability of the primary key
$uow = $om->getUnitOfWork();
if (LogEntryInterface::ACTION_CREATE === $action && ($ea->isPostInsertGenerator($meta) || ($meta instanceof ClassMetadata && $meta->isIdentifierComposite))) {
if (LogEntryInterface::ACTION_CREATE === $action && ($ea->isPostInsertGenerator($meta) || ($meta instanceof ORMClassMetadata && $meta->isIdentifierComposite))) {
$this->pendingLogEntryInserts[spl_object_id($object)] = $logEntry;
} else {
$logEntry->setObjectId($wrapped->getIdentifier(false, true));
Expand Down
1 change: 1 addition & 0 deletions src/Mapping/Driver/AbstractAnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function setOriginalDriver($driver)
* @param ClassMetadata $meta
*
* @return \ReflectionClass
* @phpstan-return \ReflectionClass<object>
*/
public function getMetaReflectionClass($meta)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Mapping/Driver/AttributeAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function __construct(AttributeReader $attributeReader, Reader $annotation
}

/**
* @phpstan-param \ReflectionClass<object> $class
*
* @return Annotation[]
*/
public function getClassAnnotations(\ReflectionClass $class): array
Expand All @@ -51,7 +53,10 @@ public function getClassAnnotations(\ReflectionClass $class): array
}

/**
* @param class-string<T> $annotationName the name of the annotation
* @param string $annotationName
*
* @phpstan-param \ReflectionClass<object> $class
* @phpstan-param class-string<T> $annotationName the name of the annotation
*
* @return T|null the Annotation or NULL, if the requested annotation does not exist
*
Expand Down
8 changes: 6 additions & 2 deletions src/Mapping/Driver/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class AttributeReader
private $isRepeatableAttribute = [];

/**
* @phpstan-param \ReflectionClass<object> $class
*
* @return array<Annotation|Annotation[]>
*/
public function getClassAnnotations(\ReflectionClass $class): array
Expand All @@ -30,6 +32,7 @@ public function getClassAnnotations(\ReflectionClass $class): array
}

/**
* @phpstan-param \ReflectionClass<object> $class
* @phpstan-param class-string $annotationName
*
* @return Annotation|Annotation[]|null
Expand Down Expand Up @@ -58,11 +61,12 @@ public function getPropertyAnnotation(\ReflectionProperty $property, string $ann
}

/**
* @param array<\ReflectionAttribute> $attributes
* @param iterable<\ReflectionAttribute> $attributes
* @phpstan-param iterable<\ReflectionAttribute<object>> $attributes
*
* @return array<string, Annotation|Annotation[]>
*/
private function convertToAttributeInstances(array $attributes): array
private function convertToAttributeInstances(iterable $attributes): array
{
$instances = [];

Expand Down
3 changes: 3 additions & 0 deletions src/ReferenceIntegrity/ReferenceIntegrityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Doctrine\Common\EventArgs;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Exception\ReferenceIntegrityStrictException;
use Gedmo\Mapping\MappedEventSubscriber;
Expand Down Expand Up @@ -40,6 +42,7 @@ public function getSubscribedEvents()
* Maps additional metadata for the Document
*
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down
2 changes: 2 additions & 0 deletions src/References/ReferencesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\EventArgs;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Mapping\MappedEventSubscriber;

Expand Down Expand Up @@ -61,6 +62,7 @@ public function __construct(array $managers = [])

/**
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Doctrine\Common\EventArgs;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Mapping\MappedEventSubscriber;
Expand Down Expand Up @@ -225,6 +226,7 @@ public function removeManagedFilter($name)
* Mapps additional metadata
*
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down
3 changes: 3 additions & 0 deletions src/SoftDeleteable/SoftDeleteableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Mapping\MappedEventSubscriber;

/**
Expand Down Expand Up @@ -108,6 +110,7 @@ public function onFlush(EventArgs $args)
* Maps additional metadata
*
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Sortable/Entity/Repository/SortableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* Sortable Repository
*
* @author Lukas Botsch <[email protected]>
*
* @phpstan-extends EntityRepository<object>
*/
class SortableRepository extends EntityRepository
{
Expand Down
1 change: 1 addition & 0 deletions src/Sortable/SortableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function getSubscribedEvents()
* Maps additional metadata
*
* @param LoadClassMetadataEventArgs $args
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $args
*
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Tool/Wrapper/AbstractWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class AbstractWrapper implements WrapperInterface
*
* @throws UnsupportedObjectManagerException
*
* @return WrapperInterface
* @return WrapperInterface<ClassMetadata>
*/
public static function wrap($object, ObjectManager $om)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tool/WrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Interface for a wrapper of a managed object.
*
* @phpstan-template TClassMetadata of ClassMetadata
* @phpstan-template-covariant TClassMetadata of ClassMetadata
*
* @author Gediminas Morkevicius <[email protected]>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* to interact with translations.
*
* @author Gediminas Morkevicius <[email protected]>
*
* @phpstan-extends DocumentRepository<object>
*/
class TranslationRepository extends DocumentRepository
{
Expand Down
2 changes: 2 additions & 0 deletions src/Translatable/Entity/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* to interact with translations.
*
* @author Gediminas Morkevicius <[email protected]>
*
* @phpstan-extends EntityRepository<object>
*/
class TranslationRepository extends EntityRepository
{
Expand Down
3 changes: 3 additions & 0 deletions src/Translatable/Mapping/Event/TranslatableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Gedmo\Translatable\Mapping\Event;

use Doctrine\Persistence\Mapping\ClassMetadata;
use Gedmo\Mapping\Event\AdapterInterface;
use Gedmo\Tool\Wrapper\AbstractWrapper;

Expand Down Expand Up @@ -61,6 +62,7 @@ public function loadTranslations($object, $translationClass, $locale, $objectCla
* @param string $translationClass
* @param string $objectClass
*
* @phpstan-param AbstractWrapper<ClassMetadata<object>> $wrapped
* @phpstan-param class-string $translationClass
* @phpstan-param class-string $objectClass
*
Expand All @@ -74,6 +76,7 @@ public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $tran
* @param string $transClass
* @param string $objectClass
*
* @phpstan-param AbstractWrapper<ClassMetadata<object>> $wrapped
* @phpstan-param class-string $transClass
* @phpstan-param class-string $objectClass
*
Expand Down
1 change: 1 addition & 0 deletions src/Translatable/TranslatableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public function addPendingTranslationInsert($oid, $translation)
* Maps additional metadata
*
* @param LoadClassMetadataEventArgs $eventArgs
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use Gedmo\Tree\RepositoryUtilsInterface;
use Gedmo\Tree\TreeListener;

/**
* @phpstan-extends DocumentRepository<object>
*/
abstract class AbstractTreeRepository extends DocumentRepository implements RepositoryInterface
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function getTreeQuery($rootNode = null)
* Get tree
*
* @param object|null $rootNode
*
* @phpstan-return Iterator<object>
*/
public function getTree($rootNode = null): Iterator
{
Expand Down
3 changes: 3 additions & 0 deletions src/Tree/Entity/Repository/AbstractTreeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Gedmo\Tree\RepositoryUtilsInterface;
use Gedmo\Tree\TreeListener;

/**
* @phpstan-extends EntityRepository<object>
*/
abstract class AbstractTreeRepository extends EntityRepository implements RepositoryInterface
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Tree/TreeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Exception\UnexpectedValueException;
Expand Down Expand Up @@ -288,6 +289,8 @@ public function postRemove(EventArgs $args)
*
* @param LoadClassMetadataEventArgs $eventArgs
*
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
public function loadClassMetadata(EventArgs $eventArgs)
Expand Down
3 changes: 3 additions & 0 deletions src/Uploadable/UploadableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\NotifyPropertyChanged;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Exception\UploadableCantWriteException;
use Gedmo\Exception\UploadableCouldntGuessMimeTypeException;
Expand Down Expand Up @@ -523,6 +524,8 @@ public function doMoveFile($source, $dest, $isUploadedFile = true)
*
* @param LoadClassMetadataEventArgs $eventArgs
*
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $eventArgs
*
* @return void
*/
public function loadClassMetadata(EventArgs $eventArgs)
Expand Down
11 changes: 4 additions & 7 deletions tests/Gedmo/Mapping/Fixture/Yaml/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class Category extends BaseCategory
private $slug;

/**
* @var Collection<int, Category>
* @var Collection<int, self>
*/
private $children;

/**
* @var Category
* @var self
*/
private $parent;

Expand Down Expand Up @@ -82,16 +82,13 @@ public function getSlug(): string
return $this->slug;
}

/**
* @param Category $children
*/
public function addChildren(self $children): void
{
$this->children[] = $children;
}

/**
* @return Collection $children
* @return Collection<int, self> $children
*/
public function getChildren()
{
Expand All @@ -104,7 +101,7 @@ public function setParent(self $parent): void
}

/**
* @return Category $parent
* @return self $parent
*/
public function getParent(): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Doctrine\Common\EventArgs;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Mapping\Event\AdapterInterface as EventAdapterInterface;
use Gedmo\Mapping\MappedEventSubscriber;

Expand All @@ -26,6 +28,9 @@ public function getSubscribedEvents(): array
];
}

/**
* @phpstan-param LoadClassMetadataEventArgs<ClassMetadata<object>, ObjectManager> $args
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $args): void
{
$ea = $this->getEventAdapter($args);
Expand Down
3 changes: 3 additions & 0 deletions tests/Gedmo/Timestampable/Fixture/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ public function addComment(Comment $comment): void
$this->comments[] = $comment;
}

/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
Expand Down
Loading

0 comments on commit 61c77d1

Please sign in to comment.