Skip to content

Commit

Permalink
Add iterable types
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Nov 3, 2022
1 parent b1f7bfd commit a035a32
Show file tree
Hide file tree
Showing 47 changed files with 268 additions and 53 deletions.
10 changes: 5 additions & 5 deletions benchmark/Document/HydrateDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
*/
final class HydrateDocumentBench extends BaseBench
{
/** @var array */
/** @var array<string, mixed> */
private static $data;

/** @var array */
/** @var array<string, mixed> */
private static $embedOneData;

/** @var array */
/** @var array<string, mixed[]> */
private static $embedManyData;

/** @var array */
/** @var array<string, mixed[]> */
private static $referenceOneData;

/** @var array */
/** @var array<string, mixed[]> */
private static $referenceManyData;

/** @var HydratorInterface */
Expand Down
6 changes: 6 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class Lookup extends Stage

private ?string $as = null;

/** @var array<string, string>|null */
private ?array $let = null;

/** @var array<array<string, mixed>>|null */
private ?array $pipeline = null;

private bool $excludeLocalAndForeignField = false;
Expand Down Expand Up @@ -155,6 +157,8 @@ public function foreignField(string $foreignField): self
*
* Use the variable expressions to access the fields from
* the joined collection's documents that are input to the pipeline.
*
* @param array<string, string> $let
*/
public function let(array $let): self
{
Expand All @@ -172,6 +176,8 @@ public function let(array $let): self
* The pipeline cannot directly access the joined document fields.
* Instead, define variables for the joined document fields using the let option
* and then reference the variables in the pipeline stages.
*
* @param array<array<string, mixed>> $pipeline
*/
public function pipeline(array $pipeline): self
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Iterator/HydratingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @template TValue
* @template TDocument of object
* @template-implements Iterator<TValue>
* @template-implements Iterator<TDocument>
*/
final class HydratingIterator implements Iterator
{
Expand Down
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,6 @@ parameters:
count: 1
path: tests/Doctrine/ODM/MongoDB/Tests/DocumentRepositoryTest.php

# 'strategy' offset is defined as nullable, but here there is no check here
-
message: "#^Offset 'strategy' does not exist on array\\{\\}\\|array\\{type\\?\\: string, fieldName\\: string, name\\: string, isCascadeRemove\\: bool, isCascadePersist\\: bool, isCascadeRefresh\\: bool, isCascadeMerge\\: bool, isCascadeDetach\\: bool, \\.\\.\\.\\}\\.$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/UnitOfWork.php

# When iterating over SimpleXMLElement, we cannot know the key values
-
message: "#^Parameter \\#2 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\Driver\\\\XmlDriver\\:\\:addFieldMapping\\(\\) expects array#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ protected function createExpr(): Expr
}

/**
* @param mixed $args
* @param Closure(Expr): mixed[]|mixed[] $args
*
* @return mixed[]
*/
protected function resolveArgs($args): array
{
Expand Down
6 changes: 4 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/ExprTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ExprTest extends BaseTest
use AggregationOperatorsProviderTrait;

/**
* @param array|Closure $args
* @param array<string, string> $expected
* @param Closure(Expr): mixed[]|mixed[] $args
*
* @dataProvider provideAllOperators
*/
Expand All @@ -29,7 +30,8 @@ public function testGenericOperator(array $expected, string $operator, $args): v
}

/**
* @param array|Closure $args
* @param array<string, string> $expected
* @param Closure(Expr): mixed[]|mixed[] $args
*
* @dataProvider provideAllOperators
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public function provideEmployeeAggregations(): array
}

/**
* @param Closure(Builder): GraphLookup $addGraphLookupStage
* @param array<string, string> $expectedFields
*
* @dataProvider provideEmployeeAggregations
*/
public function testGraphLookupWithEmployees(Closure $addGraphLookupStage, array $expectedFields): void
Expand Down Expand Up @@ -218,6 +221,9 @@ public function provideTravellerAggregations(): array
}

/**
* @param Closure(Builder): GraphLookup $addGraphLookupStage
* @param array<string, string> $expectedFields
*
* @dataProvider provideTravellerAggregations
*/
public function testGraphLookupWithTraveller(Closure $addGraphLookupStage, array $expectedFields): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GroupTest extends BaseTest
use AggregationOperatorsProviderTrait;

/**
* @param Closure|array $args
* @param Closure(Expr): Expr[]|mixed[] $args
*
* @dataProvider provideProxiedExprMethods
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function testMatchFromBuilder(): void
}

/**
* @param mixed[] $args
*
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods(string $method, array $args = []): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class OperatorTest extends BaseTest
use AggregationOperatorsProviderTrait;

/**
* @param Closure|array $args
* @param array<string, mixed> $expected
* @param Closure(Expr): mixed[]|mixed[] $args
*
* @dataProvider provideExpressionOperators
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function provideAccumulators(): array
}

/**
* @param string[] $args
*
* @dataProvider provideProxiedExprMethods
*/
public function testProxiedExprMethods(string $method, array $args = []): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
use Doctrine\ODM\MongoDB\Tests\Aggregation\AggregationTestTrait;
use Doctrine\ODM\MongoDB\Tests\BaseTest;

/**
* @psalm-import-type SortShape from Sort
*/
class SortTest extends BaseTest
{
use AggregationTestTrait;

/**
* @param string|array<string, string> $field
* @psalm-param SortShape $expectedSort
*
* @dataProvider provideSortOptions
*/
Expand All @@ -26,6 +30,7 @@ public function testSortStage(array $expectedSort, $field, ?string $order = null

/**
* @param string|array<string, string> $field
* @psalm-param SortShape $expectedSort
*
* @dataProvider provideSortOptions
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ protected function getConfiguration(): Configuration
* the original assertion checked.
*
* @deprecated
*
* @param mixed[] $subset
* @param mixed[] $array
*/
public static function assertArraySubset(array $subset, array $array, bool $checkForObjectIdentity = false, string $message = ''): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use BadMethodCallException;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Event\PostCollectionLoadEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
Expand Down Expand Up @@ -224,6 +225,9 @@ class MyEventListener
/** @psalm-var array<string, list<class-string>> */
public array $called = [];

/**
* @param array{LifecycleEventArgs} $args
*/
public function __call(string $method, array $args): void
{
$document = $args[0]->getDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function __construct(PreUpdateEventArgsTest $phpunit)
$this->phpunit = $phpunit;
}

/** @param list<class-string> $allowed */
public function checkOnly(array $allowed): void
{
$this->allowed = $allowed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\APM\CommandLogger;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
Expand Down Expand Up @@ -160,6 +161,9 @@ public function getSubscribedEvents(): array
];
}

/**
* @param array{LifecycleEventArgs} $args
*/
public function __call(string $eventName, array $args): void
{
$document = $args[0]->getDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ public function testPrepareQueryOrNewObjWithHashIdAndInOperators(array $hashId):
}

/**
* @param array<string, mixed> $expected
* @param array<string, mixed> $query
*
* @dataProvider queryProviderForCustomTypeId
*/
public function testPrepareQueryOrNewObjWithCustomTypedId(array $expected, array $query): void
Expand Down Expand Up @@ -503,6 +506,9 @@ public function testPrepareQueryOrNewObjWithDBRefReferenceToTargetDocumentWithHa
}

/**
* @param array<string, mixed> $expected
* @param array<string, mixed> $query
*
* @dataProvider queryProviderForComplexRefWithObjectValue
*/
public function testPrepareQueryOrNewObjWithComplexRefToTargetDocumentFieldWithObjectValue(array $expected, array $query): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,25 @@ public function rewind(): void
$this->assertCount(1, $iterator);
}

/**
* @param T[] $items
*
* @return Generator<T>
*
* @template T
*/
private function getTraversable(array $items): Generator
{
foreach ($items as $item) {
yield $item;
}
}

/**
* @param array<mixed|Exception> $items
*
* @return Generator<mixed>
*/
private function getTraversableThatThrows(array $items): Generator
{
foreach ($items as $item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function testIterationWithEmptySet(): void
self::assertFalse($iterator->valid());
}

/**
* @param array<string, mixed>|null $items
*
* @return Generator<array<string, mixed>>
*/
private function getTraversable(?array $items = null): Generator
{
if (! is_array($items)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,23 @@ public function testToArrayAfterPartialIteration(): void
$iterator->toArray();
}

/**
* @param list<mixed> $items
*
* @return Generator<mixed>
*/
private function getTraversable(array $items): Generator
{
foreach ($items as $item) {
yield $item;
}
}

/**
* @param array<mixed|Exception> $items
*
* @return Generator<mixed>
*/
private function getTraversableThatThrows(array $items): Generator
{
foreach ($items as $item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;

/**
* @psalm-type ReadPreferenceTagShape = array{dc?: string, usage?: string}
*/
class ReadPreferenceTest extends BaseTest
{
public function setUp(): void
Expand Down Expand Up @@ -43,6 +46,8 @@ public function testHintIsNotSetByDefault(): void
}

/**
* @psalm-param ReadPreferenceTagShape[] $tags
*
* @dataProvider provideReadPreferenceHints
*/
public function testHintIsSetOnQuery(int $readPreference, array $tags = []): void
Expand Down Expand Up @@ -100,6 +105,9 @@ public function testDocumentLevelReadPreferenceCanBeOverriddenInQueryBuilder():
$this->assertReadPreferenceHint(ReadPreference::RP_SECONDARY, $query->getQuery()['readPreference']);
}

/**
* @psalm-param ReadPreferenceTagShape[] $tags
*/
private function assertReadPreferenceHint(int $mode, ReadPreference $readPreference, array $tags = []): void
{
self::assertInstanceOf(ReadPreference::class, $readPreference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,22 @@ class CommentableAction extends Action
/**
* @ODM\Field(type="collection") *
*
* @var array
* @var string[]
*/
protected $comments = [];

/**
* @param string[] $comments
*/
public function __construct(string $type, array $comments = [])
{
parent::__construct($type);
$this->comments = $comments;
}

/**
* @return string[]
*/
public function getComments(): array
{
return $this->comments;
Expand Down
Loading

0 comments on commit a035a32

Please sign in to comment.