Skip to content

Commit

Permalink
Psalm level 4 (#2409)
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored Feb 20, 2022
1 parent 9d22a8f commit 9622613
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ public function setDiscriminatorValue(string $value): void
/**
* Add a index for this Document.
*
* @param array<string, mixed> $keys
* @param array<string, int|string> $keys
* @psalm-param IndexKeys $keys
* @psalm-param IndexOptions $options
*/
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -292,8 +291,8 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C
}

/**
* @param ClassMetadata<object> $class
* @param string[] $keys
* @param ClassMetadata<object> $class
* @param array<string, int|string> $keys
*/
private function addIndex(ClassMetadata $class, AbstractIndex $index, array $keys = []): void
{
Expand Down
2 changes: 0 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use ReflectionProperty;

use function assert;
use function is_string;
use function is_subclass_of;

/**
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) . ' ';
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/Doctrine/ODM/MongoDB/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -263,7 +261,6 @@ public function __toString()
{
$e = explode('\\', static::class);
$className = end($e);
assert(is_string($className));

return str_replace('Type', '', $className);
}
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 92 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,49 @@
<code>self</code>
</InvalidReturnType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Iterator/CachingIterator.php">
<ImplementedReturnTypeMismatch occurrences="1">
<code>TValue|false</code>
</ImplementedReturnTypeMismatch>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Iterator/HydratingIterator.php">
<ImplementedReturnTypeMismatch occurrences="1">
<code>TDocument|null</code>
</ImplementedReturnTypeMismatch>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php">
<ImplementedReturnTypeMismatch occurrences="1">
<code>array&lt;string|null&gt;</code>
</ImplementedReturnTypeMismatch>
<InvalidArrayOffset occurrences="1">
<code>[$this-&gt;identifier =&gt; $this-&gt;getIdentifierValue($object)]</code>
</InvalidArrayOffset>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php">
<InvalidArgument occurrences="1">
<InvalidArgument occurrences="2">
<code>$mapping</code>
<code>$mapping</code>
</InvalidArgument>
<RedundantCondition occurrences="15">
<code>assert($attributes instanceof SimpleXMLElement)</code>
<code>isset($xmlRoot-&gt;field)</code>
<code>isset($xmlRoot-&gt;id)</code>
<code>isset($xmlRoot-&gt;{'default-discriminator-value'})</code>
<code>isset($xmlRoot-&gt;{'discriminator-field'})</code>
<code>isset($xmlRoot-&gt;{'discriminator-map'})</code>
<code>isset($xmlRoot-&gt;{'embed-many'})</code>
<code>isset($xmlRoot-&gt;{'embed-one'})</code>
<code>isset($xmlRoot-&gt;{'indexes'})</code>
<code>isset($xmlRoot-&gt;{'lifecycle-callbacks'})</code>
<code>isset($xmlRoot-&gt;{'read-preference'})</code>
<code>isset($xmlRoot-&gt;{'reference-many'})</code>
<code>isset($xmlRoot-&gt;{'reference-one'})</code>
<code>isset($xmlRoot-&gt;{'schema-validation'})</code>
<code>isset($xmlRoot-&gt;{'shard-key'})</code>
</RedundantCondition>
<TypeDoesNotContainType occurrences="1">
<code>isset($xmlRoot-&gt;{'also-load-methods'})</code>
</TypeDoesNotContainType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/PersistentCollection/AbstractPersistentCollectionFactory.php">
<InvalidArgument occurrences="1">
Expand All @@ -38,6 +72,11 @@
<code>getTypeClass</code>
</InvalidReturnType>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php">
<RedundantFunctionCall occurrences="1">
<code>array_values</code>
</RedundantFunctionCall>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Proxy/Resolver/ProxyManagerClassNameResolver.php">
<MoreSpecificImplementedParamType occurrences="1">
<code>$className</code>
Expand All @@ -48,6 +87,11 @@
<code>$options + ['metadata' =&gt; (object) $metadata]</code>
</InvalidReturnStatement>
</file>
<file src="lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php">
<RedundantCondition occurrences="1">
<code>$datetime instanceof DateTime</code>
</RedundantCondition>
</file>
<file src="lib/Doctrine/ODM/MongoDB/UnitOfWork.php">
<InvalidPropertyAssignmentValue occurrences="2">
<code>$this-&gt;identityMap</code>
Expand Down Expand Up @@ -77,6 +121,17 @@
<code>$j</code>
</InvalidArgument>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Functional/NestedDocumentsTest.php">
<InvalidScalarArgument occurrences="2">
<code>$name</code>
<code>$name</code>
</InvalidScalarArgument>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH245Test.php">
<RedundantCondition occurrences="1">
<code>assertIsInt</code>
</RedundantCondition>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM81Test.php">
<InvalidNullableReturnType occurrences="1">
<code>DocumentManager</code>
Expand All @@ -85,4 +140,40 @@
<code>$this-&gt;dm</code>
</NullableReturnStatement>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php">
<InvalidScalarArgument occurrences="1">
<code>1</code>
</InvalidScalarArgument>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Query/QueryExpressionVisitorTest.php">
<TooManyArguments occurrences="1">
<code>andX</code>
</TooManyArguments>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php">
<InternalClass occurrences="2">
<code>new IndexInfoIteratorIterator(new ArrayIterator($indexes))</code>
<code>new IndexInfoIteratorIterator(new ArrayIterator([]))</code>
</InternalClass>
<InternalMethod occurrences="2">
<code>new IndexInfoIteratorIterator(new ArrayIterator($indexes))</code>
<code>new IndexInfoIteratorIterator(new ArrayIterator([]))</code>
</InternalMethod>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Types/DateImmutableTypeTest.php">
<TypeDoesNotContainType occurrences="1">
<code>assert($return instanceof DateTimeImmutable)</code>
</TypeDoesNotContainType>
</file>
<file src="tests/Doctrine/ODM/MongoDB/Tests/Types/DateTypeTest.php">
<TypeDoesNotContainType occurrences="1">
<code>assert($return instanceof DateTime)</code>
</TypeDoesNotContainType>
</file>
<file src="tests/Documents/CommentRepository.php">
<InternalMethod occurrences="2">
<code>loadAll</code>
<code>loadAll</code>
</InternalMethod>
</file>
</files>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
errorLevel="4"
findUnusedPsalmSuppress="true"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
8 changes: 4 additions & 4 deletions tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testMatchingAcceptsCriteriaWithNullWhereExpression(): void
public function testFindByRefOneFull(): void
{
$user = new User();
$account = new Account('name');
$account = new Account();
$user->setAccount($account);
$this->dm->persist($user);
$this->dm->persist($account);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());
Expand All @@ -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());

Expand Down Expand Up @@ -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);
Expand All @@ -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());
}

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 9622613

Please sign in to comment.