diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php index 519aa50056..cab9b2b659 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php @@ -31,7 +31,7 @@ class Expr /** @var ClassMetadata */ private $class; - /** @var array */ + /** @var array */ private $expr = []; /** @@ -537,6 +537,9 @@ public function floor($number): self return $this->operator('$floor', $number); } + /** + * @return array + */ public function getExpression(): array { return $this->expr; diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage.php index e76f86d79f..3575bd67f1 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage.php @@ -26,6 +26,8 @@ public function __construct(Builder $builder) /** * Assembles the aggregation stage + * + * @return array */ abstract public function getExpression(): array; diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php index 863099ace1..3b2004377e 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/MatchStage.php @@ -43,8 +43,8 @@ public function __clone() * @see Expr::addAnd() * @see https://docs.mongodb.com/manual/reference/operator/and/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param mixed[]|Expr $expression + * @param mixed[]|Expr ...$expressions * * @return static */ @@ -64,8 +64,8 @@ public function addAnd($expression, ...$expressions): self * @see Expr::addNor() * @see https://docs.mongodb.com/manual/reference/operator/nor/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param mixed[]|Expr $expression + * @param mixed[]|Expr ...$expressions * * @return static */ @@ -85,8 +85,8 @@ public function addNor($expression, ...$expressions): self * @see Expr::addOr() * @see https://docs.mongodb.com/manual/reference/operator/or/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param mixed[]|Expr $expression + * @param mixed[]|Expr ...$expressions * * @return static */ @@ -121,7 +121,7 @@ public function all(array $values): self * @see Expr::elemMatch() * @see https://docs.mongodb.com/manual/reference/operator/elemMatch/ * - * @param array|Expr $expression + * @param mixed[]|Expr $expression * * @return static */ @@ -452,7 +452,7 @@ public function mod($divisor, $remainder = 0): self * @see Expr::not() * @see https://docs.mongodb.com/manual/reference/operator/not/ * - * @param array|Expr $expression + * @param mixed[]|Expr $expression * * @return static */ diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Operator.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Operator.php index d2fc0b9f89..2f2c8fd1d1 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Operator.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Operator.php @@ -30,6 +30,9 @@ public function __construct(Builder $builder) $this->expr = $builder->expr(); } + /** + * @param mixed[] $args + */ public function __call(string $method, array $args): self { $this->expr->$method(...$args); @@ -86,8 +89,8 @@ public function add($expression1, $expression2, ...$expressions): self * @see https://docs.mongodb.com/manual/reference/operator/aggregation/and/ * @see Expr::addAnd * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param mixed[]|Expr $expression + * @param mixed[]|Expr ...$expressions * * @return static */ @@ -104,8 +107,8 @@ public function addAnd($expression, ...$expressions): self * @see https://docs.mongodb.com/manual/reference/operator/aggregation/or/ * @see Expr::addOr * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param mixed[]|Expr $expression + * @param mixed[]|Expr ...$expressions * * @return static */ @@ -145,7 +148,7 @@ public function allElementsTrue($expression): self * @see https://docs.mongodb.com/manual/reference/operator/aggregation/anyElementTrue/ * @see Expr::anyElementTrue * - * @param array|Expr $expression + * @param mixed[]|Expr $expression * * @return static */ diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Project.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Project.php index 0b0bbc671f..08e997c53f 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Project.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Project.php @@ -40,6 +40,8 @@ public function avg($expression1, ...$expressions): self /** * Shorthand method to define which fields to be included. + * + * @param string[] $fields */ public function includeFields(array $fields): self { @@ -55,6 +57,8 @@ public function includeFields(array $fields): self * * If you specify the exclusion of a field other than _id, you cannot employ * any other $project specification forms. + * + * @param string[] $fields */ public function excludeFields(array $fields): self { diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php index 781780b455..a5058fcb7c 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php @@ -24,11 +24,11 @@ class ReplaceRoot extends Operator /** @var ClassMetadata */ private $class; - /** @var string|array|Expr|null */ + /** @var string|mixed[]|Expr|null */ private $expression; /** - * @param string|array|Expr|null $expression + * @param string|mixed[]|Expr|null $expression */ public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class, $expression = null) { @@ -51,9 +51,9 @@ public function getExpression(): array } /** - * @param array|string|mixed $expression + * @param mixed[]|string|mixed $expression * - * @return array|string|mixed + * @return mixed[]|string|mixed */ private function convertExpression($expression) { diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Sort.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Sort.php index a88614a294..b930d8e9e3 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Sort.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Sort.php @@ -17,12 +17,12 @@ */ class Sort extends Stage { - /** @var array */ + /** @var array */ private $sort = []; /** - * @param array|string $fieldName Field name or array of field/order pairs - * @param int|string $order Field order (if one field is specified) + * @param array|string $fieldName Field name or array of field/order pairs + * @param int|string $order Field order (if one field is specified) */ public function __construct(Builder $builder, $fieldName, $order = null) { diff --git a/lib/Doctrine/ODM/MongoDB/Query/Builder.php b/lib/Doctrine/ODM/MongoDB/Query/Builder.php index 104598088a..c7212c07a7 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Builder.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Builder.php @@ -28,6 +28,8 @@ /** * Query builder for ODM. + * + * @psalm-import-type QueryShape from Query */ class Builder { @@ -70,7 +72,7 @@ class Builder /** * Array of primer Closure instances. * - * @var array + * @var array */ private $primers = []; @@ -95,6 +97,7 @@ class Builder * Array containing the query data. * * @var array + * @psalm-var QueryShape */ private $query = ['type' => Query::TYPE_FIND]; @@ -137,8 +140,8 @@ public function __clone() * @see Expr::addAnd() * @see https://docs.mongodb.com/manual/reference/operator/and/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param array|Expr $expression + * @param array|Expr ...$expressions */ public function addAnd($expression, ...$expressions): self { @@ -155,8 +158,8 @@ public function addAnd($expression, ...$expressions): self * @see Expr::addNor() * @see https://docs.mongodb.com/manual/reference/operator/nor/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param array|Expr $expression + * @param array|Expr ...$expressions */ public function addNor($expression, ...$expressions): self { @@ -173,8 +176,8 @@ public function addNor($expression, ...$expressions): self * @see Expr::addOr() * @see https://docs.mongodb.com/manual/reference/operator/or/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param array|Expr $expression + * @param array|Expr ...$expressions */ public function addOr($expression, ...$expressions): self { @@ -212,6 +215,8 @@ public function addToSet($valueOrExpression): self * * @see Expr::all() * @see https://docs.mongodb.com/manual/reference/operator/all/ + * + * @param mixed[] $values */ public function all(array $values): self { @@ -255,7 +260,7 @@ public function bitOr(int $value): self * @see Expr::bitsAllClear() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAllClear/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAllClear($value): self { @@ -271,7 +276,7 @@ public function bitsAllClear($value): self * @see Expr::bitsAllSet() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAllSet/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAllSet($value): self { @@ -287,7 +292,7 @@ public function bitsAllSet($value): self * @see Expr::bitsAnyClear() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAnyClear/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAnyClear($value): self { @@ -303,7 +308,7 @@ public function bitsAnyClear($value): self * @see Expr::bitsAnySet() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAnySet/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAnySet($value): self { @@ -432,7 +437,7 @@ public function distinct(string $field): self * @see Expr::elemMatch() * @see https://docs.mongodb.com/manual/reference/operator/elemMatch/ * - * @param array|Expr $expression + * @param array|Expr $expression */ public function elemMatch($expression): self { @@ -461,7 +466,7 @@ public function equals($value): self * If fields have been selected for inclusion, only the "_id" field may be * excluded. * - * @param array|string $fieldName,... + * @param string[]|string $fieldName,... */ public function exclude($fieldName = null): self { @@ -549,7 +554,7 @@ public function findAndUpdate(?string $documentName = null): self * @see Expr::geoIntersects() * @see https://docs.mongodb.com/manual/reference/operator/geoIntersects/ * - * @param array|Geometry $geometry + * @param array|Geometry $geometry */ public function geoIntersects($geometry): self { @@ -567,7 +572,7 @@ public function geoIntersects($geometry): self * @see Expr::geoWithin() * @see https://docs.mongodb.com/manual/reference/operator/geoWithin/ * - * @param array|Geometry $geometry + * @param array|Geometry $geometry */ public function geoWithin($geometry): self { @@ -640,10 +645,10 @@ public function geoWithinCenterSphere(float $x, float $y, float $radius): self * @see Expr::geoWithinPolygon() * @see https://docs.mongodb.com/manual/reference/operator/polygon/ * - * @param array $point1 First point of the polygon - * @param array $point2 Second point of the polygon - * @param array $point3 Third point of the polygon - * @param array ...$points Additional points of the polygon + * @param array{int|float, int|float} $point1 First point of the polygon + * @param array{int|float, int|float} $point2 Second point of the polygon + * @param array{int|float, int|float} $point3 Third point of the polygon + * @param array{int|float, int|float} ...$points Additional points of the polygon */ public function geoWithinPolygon($point1, $point2, $point3, ...$points): self { @@ -656,6 +661,8 @@ public function geoWithinPolygon($point1, $point2, $point3, ...$points): self * Return the expression's "new object". * * @see Expr::getNewObj() + * + * @return array */ public function getNewObj(): array { @@ -664,6 +671,8 @@ public function getNewObj(): array /** * Gets the Query executable. + * + * @param array $options */ public function getQuery(array $options = []): Query { @@ -730,6 +739,8 @@ public function getQuery(array $options = []): Query * Return the expression's query criteria. * * @see Expr::getQuery() + * + * @return array */ public function getQueryArray(): array { @@ -777,7 +788,7 @@ public function gte($value): self /** * Set the index hint for the query. * - * @param array|string $index + * @param array|string $index */ public function hint($index): self { @@ -808,6 +819,8 @@ public function immortal(bool $bool = true): self * * @see Expr::in() * @see https://docs.mongodb.com/manual/reference/operator/in/ + * + * @param mixed[] $values */ public function in(array $values): self { @@ -990,8 +1003,8 @@ public function mul($value): self * @see Expr::near() * @see https://docs.mongodb.com/manual/reference/operator/near/ * - * @param float|array|Point $x - * @param float $y + * @param float|array|Point $x + * @param float $y */ public function near($x, $y = null): self { @@ -1010,8 +1023,8 @@ public function near($x, $y = null): self * @see Expr::nearSphere() * @see https://docs.mongodb.com/manual/reference/operator/nearSphere/ * - * @param float|array|Point $x - * @param float $y + * @param float|array|Point $x + * @param float $y */ public function nearSphere($x, $y = null): self { @@ -1058,7 +1071,7 @@ public function notEqual($value): self * @see Expr::notIn() * @see https://docs.mongodb.com/manual/reference/operator/nin/ * - * @param array $values + * @param mixed[] $values */ public function notIn(array $values): self { @@ -1150,6 +1163,8 @@ public function pull($valueOrExpression): self * * @see Expr::pullAll() * @see https://docs.mongodb.com/manual/reference/operator/pullAll/ + * + * @param mixed[] $values */ public function pullAll(array $values): self { @@ -1255,7 +1270,7 @@ public function returnNew(bool $bool = true): self /** * Set one or more fields to be included in the query projection. * - * @param array|string $fieldName,... + * @param string[]|string $fieldName,... */ public function select($fieldName = null): self { @@ -1278,7 +1293,7 @@ public function select($fieldName = null): self * * @see https://docs.mongodb.com/manual/reference/projection/elemMatch/ * - * @param array|Expr $expression + * @param array|Expr $expression */ public function selectElemMatch(string $fieldName, $expression): self { @@ -1347,6 +1362,8 @@ public function set($value, bool $atomic = true): self * Set the expression's "new object". * * @see Expr::setNewObj() + * + * @param array $newObj */ public function setNewObj(array $newObj): self { @@ -1401,6 +1418,8 @@ public function setRewindable(bool $rewindable = true): self * Set the expression's query criteria. * * @see Expr::setQuery() + * + * @param array $query */ public function setQueryArray(array $query): self { @@ -1453,8 +1472,8 @@ public function snapshot(bool $bool = true): self * If sorting by multiple fields, the first argument should be an array of * field name (key) and order (value) pairs. * - * @param array|string $fieldName Field name or array of field/order pairs - * @param int|string $order Field order (if one field is specified) + * @param array|string $fieldName Field name or array of field/order pairs + * @param int|string $order Field order (if one field is specified) */ public function sort($fieldName, $order = 1): self { @@ -1593,6 +1612,8 @@ public function where($javascript): self * * @psalm-param class-string[] $classNames * + * @return array + * * @throws InvalidArgumentException If the number of found collections > 1. */ private function getDiscriminatorValues(array $classNames): array diff --git a/lib/Doctrine/ODM/MongoDB/Query/CriteriaMerger.php b/lib/Doctrine/ODM/MongoDB/Query/CriteriaMerger.php index 9f3b945c54..9ec199cfd6 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/CriteriaMerger.php +++ b/lib/Doctrine/ODM/MongoDB/Query/CriteriaMerger.php @@ -20,7 +20,9 @@ final class CriteriaMerger /** * Combines any number of criteria arrays as clauses of an "$and" query. * - * @param array ...$criterias Any number of query criteria arrays + * @param array ...$criterias Any number of query criteria arrays + * + * @return array */ public function merge(...$criterias): array { diff --git a/lib/Doctrine/ODM/MongoDB/Query/Expr.php b/lib/Doctrine/ODM/MongoDB/Query/Expr.php index 9a75afcb71..2114e59475 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Expr.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Expr.php @@ -32,13 +32,15 @@ /** * Query expression builder for ODM. + * + * @psalm-import-type FieldMapping from ClassMetadata */ class Expr { /** * The query criteria array. * - * @var array + * @var array */ private $query = []; @@ -46,9 +48,9 @@ class Expr * The "new object" array containing either a full document or a number of * atomic update operators. * - * @see docs.mongodb.org/manual/reference/method/db.collection.update/#update-parameter + * @see https://docs.mongodb.org/manual/reference/method/db.collection.update/#update-parameter * - * @var array + * @var array */ private $newObj = []; @@ -84,8 +86,8 @@ public function __construct(DocumentManager $dm) * @see Builder::addAnd() * @see https://docs.mongodb.com/manual/reference/operator/and/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param array|Expr $expression + * @param array|Expr ...$expressions */ public function addAnd($expression, ...$expressions): self { @@ -107,8 +109,8 @@ public function addAnd($expression, ...$expressions): self * @see Builder::addNor() * @see https://docs.mongodb.com/manual/reference/operator/nor/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param array|Expr $expression + * @param array|Expr ...$expressions */ public function addNor($expression, ...$expressions): self { @@ -130,8 +132,8 @@ public function addNor($expression, ...$expressions): self * @see Builder::addOr() * @see https://docs.mongodb.com/manual/reference/operator/or/ * - * @param array|Expr $expression - * @param array|Expr ...$expressions + * @param array|Expr $expression + * @param array|Expr ...$expressions */ public function addOr($expression, ...$expressions): self { @@ -177,6 +179,8 @@ public function addToSet($valueOrExpression): self * * @see Builder::all() * @see https://docs.mongodb.com/manual/reference/operator/all/ + * + * @param mixed[] $values */ public function all(array $values): self { @@ -225,7 +229,7 @@ public function bitOr(int $value): self * @see Builder::bitsAllClear() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAllClear/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAllClear($value): self { @@ -241,7 +245,7 @@ public function bitsAllClear($value): self * @see Builder::bitsAllSet() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAllSet/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAllSet($value): self { @@ -257,7 +261,7 @@ public function bitsAllSet($value): self * @see Builder::bitsAnyClear() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAnyClear/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAnyClear($value): self { @@ -273,7 +277,7 @@ public function bitsAnyClear($value): self * @see Builder::bitsAnySet() * @see https://docs.mongodb.com/manual/reference/operator/query/bitsAnySet/ * - * @param int|array|Binary $value + * @param int|list|Binary $value */ public function bitsAnySet($value): self { @@ -385,6 +389,8 @@ public function diacriticSensitive(bool $diacriticSensitive): self * * @see Expr::push() * @see https://docs.mongodb.com/manual/reference/operator/each/ + * + * @param mixed[] $values */ public function each(array $values): self { @@ -397,7 +403,7 @@ public function each(array $values): self * @see Builder::elemMatch() * @see https://docs.mongodb.com/manual/reference/operator/elemMatch/ * - * @param array|Expr $expression + * @param array|Expr $expression */ public function elemMatch($expression): self { @@ -454,7 +460,7 @@ public function field(string $field): self * @see Builder::geoIntersects() * @see https://docs.mongodb.com/manual/reference/operator/geoIntersects/ * - * @param array|Geometry $geometry + * @param array|Geometry $geometry */ public function geoIntersects($geometry): self { @@ -474,7 +480,7 @@ public function geoIntersects($geometry): self * @see Builder::geoWithin() * @see https://docs.mongodb.com/manual/reference/operator/geoIntersects/ * - * @param array|Geometry $geometry + * @param array|Geometry $geometry */ public function geoWithin($geometry): self { @@ -549,10 +555,10 @@ public function geoWithinCenterSphere(float $x, float $y, float $radius): self * @see Builder::geoWithinPolygon() * @see https://docs.mongodb.com/manual/reference/operator/polygon/ * - * @param array $point1 First point of the polygon - * @param array $point2 Second point of the polygon - * @param array $point3 Third point of the polygon - * @param array ...$points Additional points of the polygon + * @param array{int|float, int|float} $point1 First point of the polygon + * @param array{int|float, int|float} $point2 Second point of the polygon + * @param array{int|float, int|float} $point3 Third point of the polygon + * @param array{int|float, int|float} ...$points Additional points of the polygon * * @throws InvalidArgumentException If less than three points are given. */ @@ -573,6 +579,8 @@ public function getCurrentField(): ?string /** * Gets prepared newObj part of expression. + * + * @return array */ public function getNewObj(): array { @@ -583,6 +591,8 @@ public function getNewObj(): array /** * Gets prepared query part of expression. + * + * @return array */ public function getQuery(): array { @@ -622,6 +632,8 @@ public function gte($value): self * * @see Builder::in() * @see https://docs.mongodb.com/manual/reference/operator/in/ + * + * @param mixed[] $values */ public function in(array $values): self { @@ -813,8 +825,8 @@ public function mul($value): self * @see Builder::near() * @see https://docs.mongodb.com/manual/reference/operator/near/ * - * @param float|array|Point $x - * @param float $y + * @param float|array|Point $x + * @param float $y */ public function near($x, $y = null): self { @@ -839,8 +851,8 @@ public function near($x, $y = null): self * @see Builder::nearSphere() * @see https://docs.mongodb.com/manual/reference/operator/nearSphere/ * - * @param float|array|Point $x - * @param float $y + * @param float|array|Point $x + * @param float $y */ public function nearSphere($x, $y = null): self { @@ -886,6 +898,8 @@ public function notEqual($value): self * * @see Builder::notIn() * @see https://docs.mongodb.com/manual/reference/operator/nin/ + * + * @param mixed[] $values */ public function notIn(array $values): self { @@ -977,6 +991,8 @@ public function pull($valueOrExpression): self * * @see Builder::pullAll() * @see https://docs.mongodb.com/manual/reference/operator/pullAll/ + * + * @param mixed[] $values */ public function pullAll(array $values): self { @@ -1148,6 +1164,8 @@ public function setClassMetadata(ClassMetadata $class): void * Set the "new object". * * @see Builder::setNewObj() + * + * @param array $newObj */ public function setNewObj(array $newObj): self { @@ -1182,6 +1200,8 @@ public function setOnInsert($value): self * Set the query criteria. * * @see Builder::setQueryArray() + * + * @param array $query */ public function setQuery(array $query): self { @@ -1227,8 +1247,8 @@ public function slice(int $slice): self * * @see https://docs.mongodb.com/manual/reference/operator/sort/ * - * @param array|string $fieldName Field name or array of field/order pairs - * @param int|string $order Field order (if one field is specified) + * @param array|string $fieldName Field name or array of field/order pairs + * @param int|string $order Field order (if one field is specified) */ public function sort($fieldName, $order = null): self { @@ -1301,6 +1321,8 @@ public function where($javascript): self /** * Gets reference mapping for current field from current class or its descendants. * + * @return FieldMapping + * * @throws MappingException */ private function getReferenceMapping(): array @@ -1404,6 +1426,11 @@ private function wrapEqualityCriteria(): void $query = ['$in' => [$query]]; } + /** + * @param array|array> $query + * + * @return array + */ private function convertExpressions(array $query, ?ClassMetadata $classMetadata = null): array { if ($classMetadata === null) { @@ -1437,7 +1464,7 @@ private function convertExpressions(array $query, ?ClassMetadata $classMetadata * * @param Expr|mixed $expression * - * @return array|mixed + * @return array|mixed */ private static function convertExpression($expression, ClassMetadata $classMetadata) { diff --git a/lib/Doctrine/ODM/MongoDB/Query/Filter/BsonFilter.php b/lib/Doctrine/ODM/MongoDB/Query/Filter/BsonFilter.php index d3194c1f31..07c16eef0b 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Filter/BsonFilter.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Filter/BsonFilter.php @@ -23,7 +23,7 @@ abstract class BsonFilter /** * Parameters for the filter. * - * @var array + * @var array */ protected $parameters = []; @@ -68,6 +68,8 @@ final public function getParameter(string $name) * If there is no criteria for the class, an empty array should be returned. * * @param ClassMetadata $class Target document metadata. + * + * @return array */ abstract public function addFilterCriteria(ClassMetadata $class): array; } diff --git a/lib/Doctrine/ODM/MongoDB/Query/FilterCollection.php b/lib/Doctrine/ODM/MongoDB/Query/FilterCollection.php index afc0bc92d8..638f5e56b0 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/FilterCollection.php +++ b/lib/Doctrine/ODM/MongoDB/Query/FilterCollection.php @@ -144,6 +144,8 @@ public function isEnabled(string $name): bool /** * Gets enabled filter criteria. + * + * @return array */ public function getFilterCriteria(ClassMetadata $class): array { diff --git a/lib/Doctrine/ODM/MongoDB/Query/Query.php b/lib/Doctrine/ODM/MongoDB/Query/Query.php index 459d1b2241..dfc2b8bce7 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Query.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Query.php @@ -13,10 +13,12 @@ use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\MongoDBException; +use Doctrine\ODM\MongoDB\UnitOfWork; use InvalidArgumentException; use IteratorAggregate; use MongoDB\Collection; use MongoDB\DeleteResult; +use MongoDB\Driver\ReadPreference; use MongoDB\InsertOneResult; use MongoDB\Operation\FindOneAndUpdate; use MongoDB\UpdateResult; @@ -31,7 +33,6 @@ use function array_map; use function array_merge; use function array_values; -use function assert; use function is_array; use function is_callable; use function is_string; @@ -41,6 +42,23 @@ /** * ODM Query wraps the raw Doctrine MongoDB queries to add additional functionality * and to hydrate the raw arrays of data to Doctrine document objects. + * + * @psalm-type QueryShape = array{ + * distinct?: string, + * hint?: string|array, + * limit?: int, + * multiple?: bool, + * new?: bool, + * newObj?: array, + * query?: array, + * readPreference?: ReadPreference, + * select?: array>, + * skip?: int, + * sort?: array, + * type?: Query::TYPE_*, + * upsert?: bool, + * } + * @psalm-import-type Hints from UnitOfWork */ final class Query implements IteratorAggregate { @@ -82,7 +100,7 @@ final class Query implements IteratorAggregate /** * Array of primer Closure instances. * - * @var array + * @var array */ private $primers = []; @@ -93,6 +111,7 @@ final class Query implements IteratorAggregate * Hints for UnitOfWork behavior. * * @var array + * @psalm-var Hints */ private $unitOfWorkHints = []; @@ -107,6 +126,7 @@ final class Query implements IteratorAggregate * Query structure generated by the Builder class. * * @var array + * @psalm-var QueryShape */ private $query; @@ -116,10 +136,15 @@ final class Query implements IteratorAggregate /** * Query options * - * @var array + * @var array */ private $options; + /** + * @param QueryShape $query + * @param array $options + * @param array $primers + */ public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = [], array $options = [], bool $hydrate = true, bool $refresh = false, array $primers = [], bool $readOnly = false, bool $rewindable = true) { $primers = array_filter($primers); @@ -180,7 +205,7 @@ public function debug(?string $name = null) /** * Execute the query and returns the results. * - * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null + * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null * * @throws MongoDBException */ @@ -269,6 +294,8 @@ public function getIterator(): Iterator /** * Return the query structure. + * + * @return QueryShape */ public function getQuery(): array { @@ -278,7 +305,7 @@ public function getQuery(): array /** * Execute the query and return the first result. * - * @return array|object|null + * @return array|object|null */ public function getSingleResult() { @@ -338,6 +365,8 @@ public function setRewindable(bool $rewindable = true): void * Execute the query and return its results as an array. * * @see IteratorAggregate::toArray() + * + * @return mixed[] */ public function toArray(): array { @@ -347,6 +376,8 @@ public function toArray(): array /** * Returns an array containing the specified keys and their values from the * query array, provided they exist and are not null. + * + * @return array */ private function getQueryOptions(string ...$keys): array { @@ -365,6 +396,8 @@ static function ($value) { * accept Traversable for testing purposes since Cursor cannot be mocked. * HydratingIterator, CachingIterator, and BaseIterator expect a Traversable * so this should not have any adverse effects. + * + * @param Traversable $cursor */ private function makeIterator(Traversable $cursor): Iterator { @@ -385,7 +418,10 @@ private function makeIterator(Traversable $cursor): Iterator /** * Returns an array with its keys renamed based on the translation map. * - * @return array $rename Translation map (from => to) for renaming keys + * @param array $options + * @param array $rename + * + * @return array $rename Translation map (from => to) for renaming keys */ private function renameQueryOptions(array $options, array $rename): array { @@ -403,9 +439,6 @@ static function ($key) use ($rename) { array_values($options) ); - // Necessary because of https://github.com/phpstan/phpstan/issues/1580 - assert($options !== false); - return $options; } @@ -418,7 +451,7 @@ static function ($key) use ($rename) { * on the driver's write concern. Queries and some mapReduce commands will * return an Iterator. * - * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null + * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null */ private function runQuery() { diff --git a/lib/Doctrine/ODM/MongoDB/Query/QueryExpressionVisitor.php b/lib/Doctrine/ODM/MongoDB/Query/QueryExpressionVisitor.php index a14005d119..267ba5e6e1 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/QueryExpressionVisitor.php +++ b/lib/Doctrine/ODM/MongoDB/Query/QueryExpressionVisitor.php @@ -22,7 +22,7 @@ final class QueryExpressionVisitor extends ExpressionVisitor * Map Criteria API comparison operators to query builder methods * * @todo Implement support for Comparison::CONTAINS - * @var array + * @var array */ private static $operatorMethods = [ Comparison::EQ => 'equals', @@ -39,6 +39,7 @@ final class QueryExpressionVisitor extends ExpressionVisitor * Map Criteria API composite types to query builder methods * * @var array + * @psalm-var array */ private static $compositeMethods = [ CompositeExpression::TYPE_AND => 'addAnd', diff --git a/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php b/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php index ba65b840e1..44131380c9 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php +++ b/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php @@ -39,6 +39,9 @@ * the referenced identifiers are not immediately available on an inverse side. * * @internal + * + * @psalm-import-type FieldMapping from ClassMetadata + * @psalm-import-type Hints from UnitOfWork */ final class ReferencePrimer { @@ -93,11 +96,12 @@ public function __construct(DocumentManager $dm, UnitOfWork $uow) * the default primer defined in the constructor. If $primer is not * callable, the default primer will be used. * - * @param ClassMetadata $class Class metadata for the document - * @param array|Traversable $documents Documents containing references to prime - * @param string $fieldName Field name containing references to prime - * @param array $hints UnitOfWork hints for priming queries - * @param callable|null $primer Optional primer callable + * @param ClassMetadata $class Class metadata for the document + * @param array|Traversable $documents Documents containing references to prime + * @param string $fieldName Field name containing references to prime + * @param array $hints UnitOfWork hints for priming queries + * @param callable|null $primer Optional primer callable + * @psalm-param Hints $hints * * @throws InvalidArgumentException If the mapped field is not the owning * side of a reference relationship. @@ -163,7 +167,10 @@ public function primeReferences(ClassMetadata $class, $documents, string $fieldN * ... but you cannot prime this: myDocument.embeddedDocument.referencedDocuments.referencedDocument(s) * This addresses Issue #624. * - * @param array|Traversable $documents + * @param array|Traversable $documents + * @param FieldMapping|null $mapping + * + * @return array{fieldName: string, class: ClassMetadata, documents: array|Traversable, mapping: FieldMapping} */ private function parseDotSyntaxForPrimer(string $fieldName, ClassMetadata $class, $documents, ?array $mapping = null): array { @@ -237,6 +244,8 @@ private function parseDotSyntaxForPrimer(string $fieldName, ClassMetadata $class * If the relation contains simple references, the mapping is assumed to * have a target document class defined. Without that, there is no way to * infer the class of the referenced documents. + * + * @psalm-param array> $groupedIds */ private function addManyReferences(PersistentCollectionInterface $persistentCollection, array &$groupedIds): void { diff --git a/lib/Doctrine/ODM/MongoDB/SchemaManager.php b/lib/Doctrine/ODM/MongoDB/SchemaManager.php index 77ce9aaf4d..0a580ba6ac 100644 --- a/lib/Doctrine/ODM/MongoDB/SchemaManager.php +++ b/lib/Doctrine/ODM/MongoDB/SchemaManager.php @@ -32,6 +32,7 @@ /** * @psalm-import-type IndexMapping from ClassMetadata + * @psalm-import-type IndexOptions from ClassMetadata */ final class SchemaManager { @@ -692,6 +693,9 @@ private function isEquivalentIndexOptions(IndexInfo $mongoIndex, array $document * * Options added to the ALLOWED_MISSING_INDEX_OPTIONS constant are ignored * and are expected to be checked later + * + * @psalm-param IndexOptions $mongoIndexOptions + * @psalm-param IndexOptions $documentIndexOptions */ private function indexOptionsAreMissing(array $mongoIndexOptions, array $documentIndexOptions): bool { @@ -900,6 +904,11 @@ private function collectionIsSharded(string $documentName): bool return (bool) ($stats['sharded'] ?? false); } + /** + * @param array $options + * + * @return array + */ private function getWriteOptions(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null, array $options = []): array { unset($options['maxTimeMs'], $options['writeConcern']); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7ddfdda979..ac59535f61 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -43,121 +43,21 @@ parameters: count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/FacetTest.php - - - message: "#^Cannot access property \\$username on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomFieldNameTest.php - - - - message: "#^Cannot call method current\\(\\) on array\\|int\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php - - - - message: "#^Cannot call method next\\(\\) on array\\|int\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php - - - - message: "#^Cannot call method valid\\(\\) on array\\|int\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php - - - - message: "#^Cannot call method getCount\\(\\) on array\\|int\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/FindAndModifyTest.php - - - - message: "#^Cannot call method getUsername\\(\\) on array\\|int\\|object\\.$#" - count: 3 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/FindAndModifyTest.php - - - - message: "#^Cannot call method toArray\\(\\) on array\\|int\\|object\\.$#" - count: 2 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php - - message: "#^Expression \"\\$groups\\[0\\]\" on a separate line does not do anything\\.$#" count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php - - - message: "#^Cannot call method getCount\\(\\) on array\\|int\\|object\\.$#" - count: 3 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php - - - - message: "#^Cannot call method getProfile\\(\\) on array\\|object\\.$#" - count: 2 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/InheritanceTest.php - - - - message: "#^Cannot access property \\$blogPosts on array\\|object\\.$#" - count: 2 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/OwningAndInverseReferencesTest.php - - - - message: "#^Cannot access property \\$name on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/OwningAndInverseReferencesTest.php - - - - message: "#^Cannot call method getUsername\\(\\) on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php - - - - message: "#^Cannot call method toArray\\(\\) on array\\|int\\|object\\.$#" - count: 2 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php - - - - message: "#^Cannot call method current\\(\\) on array\\|int\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php - - - - message: "#^Cannot access property \\$ref on array\\|object\\.$#" - count: 2 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php - - message: "#^Unreachable statement \\- code above always terminates\\.$#" count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH580Test.php - - - message: "#^Cannot call method current\\(\\) on array\\|int\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php - - - - message: "#^Cannot call method getBody\\(\\) on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php - - - - message: "#^Cannot call method getTitle\\(\\) on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php - - - - message: "#^Cannot call method setTitle\\(\\) on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php - - message: "#^Parameter \\#1 \\$primer of method Doctrine\\\\ODM\\\\MongoDB\\\\Query\\\\Builder\\:\\:prime\\(\\) expects bool\\|\\(callable\\(\\)\\: mixed\\), 1 given\\.$#" count: 1 path: tests/Doctrine/ODM/MongoDB/Tests/Query/BuilderTest.php - - - message: "#^Cannot access property \\$pet on array\\|object\\.$#" - count: 1 - path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php - - message: "#^Method Doctrine\\\\ODM\\\\MongoDB\\\\Tests\\\\SchemaManagerTest\\:\\:writeOptions\\(\\) should return PHPUnit\\\\Framework\\\\Constraint\\\\Constraint but returns PHPUnit\\\\Framework\\\\Constraint\\\\ArraySubset\\.$#" count: 1 @@ -280,3 +180,8 @@ parameters: - message: "#Property .+ is unused.#" path: tests + + - + message: "#^Parameter \\#4 \\$query of class Doctrine\\\\ODM\\\\MongoDB\\\\Query\\\\Query constructor expects array\\{distinct\\?\\: string, hint\\?\\: array\\\\|string, limit\\?\\: int, multiple\\?\\: bool, new\\?\\: bool, newObj\\?\\: array\\, query\\?\\: array\\, readPreference\\?\\: MongoDB\\\\Driver\\\\ReadPreference, \\.\\.\\.\\}, array\\{type\\: \\-1\\} given\\.$#" + count: 1 + path: tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 53bb92e1cc..db97346db3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -92,6 +92,12 @@ $className + + + $this->query + $this->query + + $options + ['metadata' => (object) $metadata] @@ -165,6 +171,11 @@ andX + + + ['type' => -1] + + new IndexInfoIteratorIterator(new ArrayIterator($indexes)) diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomFieldNameTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomFieldNameTest.php index 5dcca35405..4249d429e9 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomFieldNameTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/CustomFieldNameTest.php @@ -95,7 +95,7 @@ public function testQueryBuilderAndDqlArePrepared(): void $qb = $this->dm->createQueryBuilder(CustomFieldName::class)->field('username')->equals('test'); $query = $qb->getQuery(); $test = $query->getSingleResult(); - $this->assertNotNull($test); + $this->assertInstanceOf(CustomFieldName::class, $test); $this->assertEquals('test', $test->username); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php index fcf128bb74..09f87cfaaf 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/EcommerceTest.php @@ -4,6 +4,7 @@ namespace Doctrine\ODM\MongoDB\Tests\Functional; +use Doctrine\ODM\MongoDB\Iterator\Iterator; use Doctrine\ODM\MongoDB\Tests\BaseTest; use Documents\Ecommerce\ConfigurableProduct; use Documents\Ecommerce\Currency; @@ -92,6 +93,9 @@ protected function getProduct(): ConfigurableProduct ->createQueryBuilder() ->getQuery() ->execute(); + + $this->assertInstanceOf(Iterator::class, $products); + $products->valid() ?: $products->next(); return $products->current(); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FindAndModifyTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FindAndModifyTest.php index 69d25df4b3..07aa095083 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FindAndModifyTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FindAndModifyTest.php @@ -24,6 +24,7 @@ public function testFindAndModify(): void ->getQuery(); $result = $q->execute(); + $this->assertInstanceOf(User::class, $result); // Test the username was set and count incremented $this->assertEquals('jwage', $result->getUsername()); $this->assertEquals(5, $result->getCount()); @@ -35,6 +36,7 @@ public function testFindAndModify(): void ->getQuery(); $result = $q->execute(); + $this->assertInstanceOf(User::class, $result); // Test the object was returned $this->assertEquals('jwage', $result->getUsername()); @@ -59,6 +61,7 @@ public function testFindAndModifyAlt(): void ->getQuery(); $result = $q->execute(); + $this->assertInstanceOf(User::class, $result); // Test the username was set $this->assertEquals('Romain Neutron', $result->getUsername()); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php index afb6877be0..97971903a0 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php @@ -6,6 +6,7 @@ use DateTime; use Doctrine\Common\Collections\Collection; +use Doctrine\ODM\MongoDB\Iterator\Iterator; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\MongoDBException; @@ -738,20 +739,24 @@ public function testSameCollectionTest(): void $test = $this->dm->getRepository(SameCollection2::class)->findOneBy(['name' => 'test1']); $this->assertNull($test); - $qb = $this->dm->createQueryBuilder([ + $qb = $this->dm->createQueryBuilder([ SameCollection1::class, SameCollection2::class, ]); - $q = $qb->getQuery(); - $test = $q->execute()->toArray(); + $q = $qb->getQuery(); + $result = $q->execute(); + $this->assertInstanceOf(Iterator::class, $result); + $test = $result->toArray(); $this->assertCount(3, $test); $test = $this->dm->getRepository(SameCollection1::class)->findAll(); $this->assertCount(2, $test); - $qb = $this->dm->createQueryBuilder(SameCollection1::class); - $query = $qb->getQuery(); - $test = $query->execute()->toArray(); + $qb = $this->dm->createQueryBuilder(SameCollection1::class); + $query = $qb->getQuery(); + $result = $query->execute(); + $this->assertInstanceOf(Iterator::class, $result); + $test = $result->toArray(); $this->assertCount(2, $test); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php index 75e7804761..734de163df 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php @@ -105,12 +105,15 @@ public function testIdentityMap(): void ->field('count')->inc(1); $result = $qb->refresh(false)->getQuery()->execute(); + $this->assertInstanceOf(User::class, $result); $this->assertEquals(0, $result->getCount()); $result = $qb->refresh(false)->getQuery()->execute(); + $this->assertInstanceOf(User::class, $result); $this->assertEquals(0, $result->getCount()); $result = $qb->refresh(true)->getQuery()->execute(); + $this->assertInstanceOf(User::class, $result); $this->assertEquals(3, $result->getCount()); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/InheritanceTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/InheritanceTest.php index 8bdd23064f..15761077b8 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/InheritanceTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/InheritanceTest.php @@ -37,12 +37,14 @@ public function testCollectionPerClassInheritance(): void $query = $qb->getQuery(); $user = $query->getSingleResult(); + $this->assertInstanceOf(SpecialUser::class, $user); $user->getProfile()->setLastName('Wage'); $this->dm->flush(); $this->dm->clear(); $query = $qb->getQuery(); $user = $query->getSingleResult(); + $this->assertInstanceOf(SpecialUser::class, $user); $this->assertEquals('Wage', $user->getProfile()->getLastName()); $this->assertInstanceOf(SpecialUser::class, $user); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/OwningAndInverseReferencesTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/OwningAndInverseReferencesTest.php index 12d6183241..3bba63051e 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/OwningAndInverseReferencesTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/OwningAndInverseReferencesTest.php @@ -149,6 +149,7 @@ public function testManyToMany(): void $tag = $this->dm->createQueryBuilder(Tag::class) ->getQuery() ->getSingleResult(); + $this->assertInstanceOf(Tag::class, $tag); $this->assertEquals('baseball', $tag->name); $this->assertEquals(1, $tag->blogPosts->count()); $this->assertEquals('Test', $tag->blogPosts[0]->name); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php index 59d1183c50..11f061b4a9 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryTest.php @@ -188,6 +188,8 @@ public function testFindQuery(): void ->where("function() { return this.username == 'boo' }"); $query = $qb->getQuery(); $user = $query->getSingleResult(); + + $this->assertInstanceOf(User::class, $user); $this->assertEquals('boo', $user->getUsername()); } @@ -254,11 +256,13 @@ public function testMultipleUpdateQuery(): void $q = $qb->getQuery(); $results = $q->execute(); - $qb = $this->dm->createQueryBuilder(User::class) + $qb = $this->dm->createQueryBuilder(User::class) ->find() ->field('username')->equals('foo'); - $q = $qb->getQuery(); - $users = array_values($q->execute()->toArray()); + $q = $qb->getQuery(); + $result = $q->execute(); + $this->assertInstanceOf(Iterator::class, $result); + $users = array_values($result->toArray()); $this->assertCount(4, $users); } @@ -357,8 +361,10 @@ public function testDateRange(): void new UTCDateTime(strtotime('1985-09-01 01:00:00') * 1000), new UTCDateTime(strtotime('1985-09-04') * 1000) ); - $query = $qb->getQuery(); - $articles = array_values($query->execute()->toArray()); + $query = $qb->getQuery(); + $result = $query->execute(); + $this->assertInstanceOf(Iterator::class, $result); + $articles = array_values($result->toArray()); $this->assertCount(2, $articles); $this->assertEquals('1985-09-02', $articles[0]->getCreatedAt()->format('Y-m-d')); $this->assertEquals('1985-09-03', $articles[1]->getCreatedAt()->format('Y-m-d')); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php index 68689a8b2e..4986451d29 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH267Test.php @@ -5,6 +5,7 @@ namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket; use Doctrine\Common\Collections\Collection; +use Doctrine\ODM\MongoDB\Iterator\Iterator; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Tests\BaseTest; @@ -44,6 +45,9 @@ public function testNestedReferences(): void $query = $qb->getQuery(); $result = $query->execute(); + + $this->assertInstanceOf(Iterator::class, $result); + $dbUser = $result->current(); // Assert user name diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php index f4b247c4e7..88282d499c 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php @@ -28,6 +28,7 @@ public function testPrimeWithGetSingleResult(): void $document = $query->getSingleResult(); + $this->assertInstanceOf(GH520Document::class, $document); $this->assertInstanceOf(GhostObjectInterface::class, $document->ref); $this->assertTrue($document->ref->isProxyInitialized()); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php index da7abf3375..7b848ef7e3 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php @@ -8,6 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; +use Doctrine\ODM\MongoDB\Iterator\Iterator; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Tests\BaseTest; use MongoDB\BSON\Binary; @@ -81,6 +82,7 @@ public function testA(Closure $idGenerator): void ->field('name')->equals('parent') ->field('refMany')->prime() ->getQuery()->execute(); + $this->assertInstanceOf(Iterator::class, $docs); $this->assertCount(1, $docs); $this->assertCount(2, $docs->current()->refMany); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php index e17bcf9bd9..b801c79999 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/MODM88Test.php @@ -23,6 +23,7 @@ public function testTest(): void $q = $qb->getQuery(); $document = $q->getSingleResult(); + $this->assertInstanceOf(Article::class, $document); $this->assertEquals('Test Title', $document->getTitle()); $this->assertNull($document->getBody()); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php index cde77568f7..32c7203ced 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/QueryTest.php @@ -408,6 +408,7 @@ public function testReadOnly(): void ->readOnly(true) ->getQuery()->getSingleResult(); + $this->assertInstanceOf(Person::class, $readOnly); $this->assertNotSame($p, $readOnly); $this->assertTrue($this->uow->isInIdentityMap($p)); $this->assertFalse($this->uow->isInIdentityMap($readOnly)); @@ -421,6 +422,8 @@ public function testConstructorShouldThrowExceptionForInvalidType(): void } /** + * @psalm-param Query::TYPE_* $type + * * @dataProvider provideQueryTypesThatDoNotReturnAnIterator */ public function testGetIteratorShouldThrowExceptionWithoutExecutingForTypesThatDoNotReturnAnIterator(int $type, string $method): void