From 9622613fd78e6515ebb1fce746e93fe4f738532c Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Sun, 20 Feb 2022 20:23:48 +0100 Subject: [PATCH] Psalm level 4 (#2409) --- .../ODM/MongoDB/Mapping/ClassMetadata.php | 2 +- .../Mapping/Driver/AnnotationDriver.php | 5 +- .../Mapping/Driver/AttributeReader.php | 2 - .../ODM/MongoDB/Mapping/Driver/XmlDriver.php | 2 +- .../DefaultPersistentCollectionGenerator.php | 1 - lib/Doctrine/ODM/MongoDB/Query/Builder.php | 2 +- lib/Doctrine/ODM/MongoDB/Types/Type.php | 3 - phpstan-baseline.neon | 5 - psalm-baseline.xml | 93 ++++++++++++++++++- psalm.xml | 2 +- .../MongoDB/Tests/DocumentRepositoryTest.php | 8 +- .../Tests/Functional/CollectionsTest.php | 2 +- .../Tests/Functional/ReferencePrimerTest.php | 12 +-- .../Tests/Functional/Ticket/GH852Test.php | 3 + .../Persisters/PersistenceBuilderTest.php | 9 +- tests/Documents/User.php | 7 +- 16 files changed, 116 insertions(+), 42 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index ce7f5809f4..c8546cbb7a 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -1082,7 +1082,7 @@ public function setDiscriminatorValue(string $value): void /** * Add a index for this Document. * - * @param array $keys + * @param array $keys * @psalm-param IndexKeys $keys * @psalm-param IndexOptions $options */ diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php index 76498eedb3..421c47dcac 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php @@ -251,7 +251,6 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C } foreach ($reflClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - assert($method instanceof ReflectionMethod); /* Filter for the declaring class only. Callbacks from parent * classes will already be registered. */ @@ -292,8 +291,8 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C } /** - * @param ClassMetadata $class - * @param string[] $keys + * @param ClassMetadata $class + * @param array $keys */ private function addIndex(ClassMetadata $class, AbstractIndex $index, array $keys = []): void { diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeReader.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeReader.php index f8bf4fbcda..5c897e7b0f 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeReader.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeReader.php @@ -12,7 +12,6 @@ use ReflectionProperty; use function assert; -use function is_string; use function is_subclass_of; /** @@ -100,7 +99,6 @@ private function convertToAttributeInstances(array $attributes): array foreach ($attributes as $attribute) { $attributeName = $attribute->getName(); - assert(is_string($attributeName)); // Make sure we only get Doctrine Annotations if (! is_subclass_of($attributeName, Annotation::class)) { continue; diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php index 0df45fbeba..0f067d4b01 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php @@ -42,7 +42,6 @@ /** * XmlDriver is a metadata driver that enables mapping through XML files. * - * @method SimpleXMLElement getElement(string $className) * @psalm-import-type FieldMappingConfig from ClassMetadata */ class XmlDriver extends FileDriver @@ -81,6 +80,7 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C { assert($metadata instanceof ClassMetadata); $xmlRoot = $this->getElement($className); + assert($xmlRoot instanceof SimpleXMLElement); if ($xmlRoot->getName() === 'document') { if (isset($xmlRoot['repository-class'])) { diff --git a/lib/Doctrine/ODM/MongoDB/PersistentCollection/DefaultPersistentCollectionGenerator.php b/lib/Doctrine/ODM/MongoDB/PersistentCollection/DefaultPersistentCollectionGenerator.php index 37963c7a1c..c278d84387 100644 --- a/lib/Doctrine/ODM/MongoDB/PersistentCollection/DefaultPersistentCollectionGenerator.php +++ b/lib/Doctrine/ODM/MongoDB/PersistentCollection/DefaultPersistentCollectionGenerator.php @@ -226,7 +226,6 @@ private function buildParametersString(ReflectionMethod $method): string $parameterDefinitions = []; foreach ($parameters as $param) { - assert($param instanceof ReflectionParameter); $parameterDefinition = ''; if ($param->hasType()) { $parameterDefinition .= $this->getParameterType($param) . ' '; diff --git a/lib/Doctrine/ODM/MongoDB/Query/Builder.php b/lib/Doctrine/ODM/MongoDB/Query/Builder.php index df7c078f08..104598088a 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Builder.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Builder.php @@ -698,7 +698,7 @@ public function getQuery(array $options = []): Query $includeMode = 0 < count(array_filter($query['select'], static function ($mode) { return $mode === 1; })); - if ($includeMode && ! isset($query['select'][$this->class->discriminatorField])) { + if ($includeMode) { $query['select'][$this->class->discriminatorField] = 1; } } diff --git a/lib/Doctrine/ODM/MongoDB/Types/Type.php b/lib/Doctrine/ODM/MongoDB/Types/Type.php index 17f2a56d1a..da5206c2bd 100644 --- a/lib/Doctrine/ODM/MongoDB/Types/Type.php +++ b/lib/Doctrine/ODM/MongoDB/Types/Type.php @@ -10,12 +10,10 @@ use InvalidArgumentException; use MongoDB\BSON\ObjectId; -use function assert; use function end; use function explode; use function gettype; use function is_object; -use function is_string; use function sprintf; use function str_replace; @@ -263,7 +261,6 @@ public function __toString() { $e = explode('\\', static::class); $className = end($e); - assert(is_string($className)); return str_replace('Type', '', $className); } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5c662f9983..6b30c745fa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -43,11 +43,6 @@ parameters: count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/FacetTest.php - - - message: "#^Class Documents\\\\Account does not have a constructor and must be instantiated without any parameters\\.$#" - count: 4 - path: tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php - - message: "#^Cannot access property \\$username on array\\|object\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 585f0c26c4..393ef04b8c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -8,15 +8,49 @@ self + + + TValue|false + + + + + TDocument|null + + + + array<string|null> + [$this->identifier => $this->getIdentifierValue($object)] - + + $mapping $mapping + + assert($attributes instanceof SimpleXMLElement) + isset($xmlRoot->field) + isset($xmlRoot->id) + isset($xmlRoot->{'default-discriminator-value'}) + isset($xmlRoot->{'discriminator-field'}) + isset($xmlRoot->{'discriminator-map'}) + isset($xmlRoot->{'embed-many'}) + isset($xmlRoot->{'embed-one'}) + isset($xmlRoot->{'indexes'}) + isset($xmlRoot->{'lifecycle-callbacks'}) + isset($xmlRoot->{'read-preference'}) + isset($xmlRoot->{'reference-many'}) + isset($xmlRoot->{'reference-one'}) + isset($xmlRoot->{'schema-validation'}) + isset($xmlRoot->{'shard-key'}) + + + isset($xmlRoot->{'also-load-methods'}) + @@ -38,6 +72,11 @@ getTypeClass + + + array_values + + $className @@ -48,6 +87,11 @@ $options + ['metadata' => (object) $metadata] + + + $datetime instanceof DateTime + + $this->identityMap @@ -77,6 +121,17 @@ $j + + + $name + $name + + + + + assertIsInt + + DocumentManager @@ -85,4 +140,40 @@ $this->dm + + + 1 + + + + + andX + + + + + new IndexInfoIteratorIterator(new ArrayIterator($indexes)) + new IndexInfoIteratorIterator(new ArrayIterator([])) + + + new IndexInfoIteratorIterator(new ArrayIterator($indexes)) + new IndexInfoIteratorIterator(new ArrayIterator([])) + + + + + assert($return instanceof DateTimeImmutable) + + + + + assert($return instanceof DateTime) + + + + + loadAll + loadAll + + diff --git a/psalm.xml b/psalm.xml index cf8ed9d09a..fb9897adcf 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ setAccount($account); $this->dm->persist($user); $this->dm->persist($account); @@ -51,7 +51,7 @@ public function testFindByRefOneFull(): void public function testFindByRefOneWithoutTargetDocumentFull(): void { $user = new User(); - $account = new Account('name'); + $account = new Account(); $user->setAccount($account); $this->dm->persist($user); $this->dm->persist($account); @@ -74,7 +74,7 @@ public function testFindByRefOneWithoutTargetDocumentFull(): void public function testFindByRefOneWithoutTargetDocumentStoredAsDbRef(): void { $user = new User(); - $account = new Account('name'); + $account = new Account(); $account->setUserDbRef($user); $this->dm->persist($user); $this->dm->persist($account); @@ -114,7 +114,7 @@ public function testFindDiscriminatedByRefManyFull(): void public function testFindByRefOneSimple(): void { $user = new User(); - $account = new Account('name'); + $account = new Account(); $user->setAccountSimple($account); $this->dm->persist($user); $this->dm->persist($account); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionsTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionsTest.php index ba5e647751..c552096a07 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionsTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/CollectionsTest.php @@ -56,9 +56,9 @@ public function testCollections(): void $this->dm->clear(); $bar = $this->dm->find(Bar::class, $bar->getId()); + $this->assertNotNull($bar); $this->assertEquals($bar->getId(), $this->dm->getUnitOfWork()->getDocumentIdentifier($bar)); - $this->assertNotNull($bar); $locations = $bar->getLocations(); $this->assertCount(3, $locations); $locations = $bar->getLocations(); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php index a872479fb0..22b1d99e49 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php @@ -251,7 +251,7 @@ public function testPrimeReferencesWithDifferentStoreAsReferences(): void foreach ($qb->getQuery() as $referenceUser) { assert($referenceUser instanceof ReferenceUser); $user = $referenceUser->getUser(); - assert($user instanceof User && $user instanceof GhostObjectInterface); + $this->assertInstanceOf(User::class, $user); $this->assertInstanceOf(GhostObjectInterface::class, $user); $this->assertTrue($user->isProxyInitialized()); @@ -263,8 +263,8 @@ public function testPrimeReferencesWithDifferentStoreAsReferences(): void } $parentUser = $referenceUser->getParentUser(); - assert($parentUser instanceof User && $parentUser instanceof GhostObjectInterface); $this->assertInstanceOf(GhostObjectInterface::class, $parentUser); + $this->assertInstanceOf(User::class, $parentUser); $this->assertTrue($parentUser->isProxyInitialized()); $this->assertCount(1, $referenceUser->getParentUsers()); @@ -275,7 +275,7 @@ public function testPrimeReferencesWithDifferentStoreAsReferences(): void } $otherUser = $referenceUser->getOtherUser(); - assert($otherUser instanceof User && $otherUser instanceof GhostObjectInterface); + $this->assertInstanceOf(User::class, $otherUser); $this->assertInstanceOf(GhostObjectInterface::class, $otherUser); $this->assertTrue($otherUser->isProxyInitialized()); @@ -503,7 +503,6 @@ public function testPrimeEmbeddedReferenceTwoLevelsDeep(): void ->field('options.money.currency')->prime(true); $query = $qb->getQuery(); - assert($query instanceof Query); $product = $query->getSingleResult(); assert($product instanceof ConfigurableProduct); @@ -515,10 +514,9 @@ public function testPrimeEmbeddedReferenceTwoLevelsDeep(): void assert($money instanceof Money); $currency = $money->getCurrency(); - assert($currency instanceof Currency && $currency instanceof GhostObjectInterface); - assert($currency instanceof Currency); $this->assertInstanceOf(GhostObjectInterface::class, $currency); + $this->assertInstanceOf(Currency::class, $currency); $this->assertTrue($currency->isProxyInitialized()); } @@ -542,7 +540,6 @@ public function testPrimeReferencesInReferenceMany(): void $this->dm->clear(); $post = $this->dm->find(BlogPost::class, $post->id); - assert($post instanceof BlogPost); $this->assertInstanceOf(BlogPost::class, $post); $comment = $post->comments->first(); @@ -570,7 +567,6 @@ public function testPrimeReferencesInReferenceManyWithRepositoryMethodEager(): v $this->dm->clear(); $post = $this->dm->find(BlogPost::class, $post->id); - assert($post instanceof BlogPost); $this->assertInstanceOf(BlogPost::class, $post); $comment = $post->repoCommentsWithPrimer->first(); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php index da86e3f938..da7abf3375 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php @@ -52,6 +52,7 @@ public function testA(Closure $idGenerator): void $this->assertEquals('parent', $parent->name); $this->assertInstanceOf(GhostObjectInterface::class, $parent->refOne); + $this->assertInstanceOf(GH852Document::class, $parent->refOne); $this->assertFalse($parent->refOne->isProxyInitialized()); $this->assertEquals($idGenerator('childA'), $parent->refOne->id); $this->assertEquals('childA', $parent->refOne->name); @@ -63,11 +64,13 @@ public function testA(Closure $idGenerator): void * by DocumentPersister::loadReferenceManyCollectionOwningSide(). */ $this->assertInstanceOf(GhostObjectInterface::class, $parent->refMany[0]); + $this->assertInstanceOf(GH852Document::class, $parent->refMany[0]); $this->assertTrue($parent->refMany[0]->isProxyInitialized()); $this->assertEquals($idGenerator('childB'), $parent->refMany[0]->id); $this->assertEquals('childB', $parent->refMany[0]->name); $this->assertInstanceOf(GhostObjectInterface::class, $parent->refMany[1]); + $this->assertInstanceOf(GH852Document::class, $parent->refMany[1]); $this->assertTrue($parent->refMany[1]->isProxyInitialized()); $this->assertEquals($idGenerator('childC'), $parent->refMany[1]->id); $this->assertEquals('childC', $parent->refMany[1]->name); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php index 9053b1422c..a953b7023c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Persisters/PersistenceBuilderTest.php @@ -6,7 +6,6 @@ use Doctrine\ODM\MongoDB\Iterator\Iterator; use Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder; -use Doctrine\ODM\MongoDB\Query\Builder; use Doctrine\ODM\MongoDB\Tests\BaseTest; use Documents\CmsArticle; use Documents\CmsComment; @@ -17,7 +16,6 @@ use MongoDB\BSON\ObjectId; use function array_keys; -use function assert; use function get_class; class PersistenceBuilderTest extends BaseTest @@ -49,7 +47,6 @@ public function testQueryBuilderUpdateWithDiscriminatorMap(): void $this->uow->computeChangeSets(); $qb = $this->dm->createQueryBuilder(SameCollection1::class); - assert($qb instanceof Builder); $qb->updateOne() ->field('ok')->set(true) ->field('test')->set('OK! TEST') @@ -95,7 +92,6 @@ public function testFindWithOrOnCollectionWithDiscriminatorMap(): void $this->uow->computeChangeSets(); $qb = $this->dm->createQueryBuilder(SameCollection2::class); - assert($qb instanceof Builder); $qb ->field('id')->in($ids) ->select('id')->hydrate(false); @@ -268,11 +264,10 @@ public function testAdvancedQueriesOnReferenceWithDiscriminatorMap(): void $this->uow->computeChangeSets(); - $articleId = (string) $article->id; - $commentId = (string) $comment->id; + $articleId = $article->id; + $commentId = $comment->id; $qb = $this->dm->createQueryBuilder(CmsComment::class); - assert($qb instanceof Builder); $qb ->field('article.id')->in([$articleId]); $query = $qb->getQuery(); diff --git a/tests/Documents/User.php b/tests/Documents/User.php index c38f2449c9..2c39e98df1 100644 --- a/tests/Documents/User.php +++ b/tests/Documents/User.php @@ -10,6 +10,7 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use MongoDB\BSON\ObjectId; +use MongoDB\BSON\UTCDateTime; use function bcadd; @@ -43,7 +44,7 @@ class User extends BaseDocument /** * @ODM\Field(type="date") * - * @var DateTimeInterface|string + * @var UTCDateTime|DateTimeInterface|string */ protected $createdAt; @@ -296,7 +297,7 @@ public function getPassword(): ?string } /** - * @param DateTimeInterface|string $createdAt + * @param UTCDateTime|DateTimeInterface|string $createdAt */ public function setCreatedAt($createdAt): void { @@ -304,7 +305,7 @@ public function setCreatedAt($createdAt): void } /** - * @return DateTime|DateTimeInterface|string + * @return UTCDateTime|DateTime|DateTimeInterface|string */ public function getCreatedAt() {