Skip to content

Commit

Permalink
Merge pull request doctrine#2385 from franmomu/use_phpstan_1
Browse files Browse the repository at this point in the history
Use PHPStan 1
  • Loading branch information
malarzm authored Nov 18, 2021
2 parents 9a54beb + 5f4a863 commit 240fc93
Show file tree
Hide file tree
Showing 34 changed files with 192 additions and 88 deletions.
12 changes: 6 additions & 6 deletions benchmark/Document/HydrateDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class HydrateDocumentBench extends BaseBench
/** @var HydratorInterface */
private static $hydrator;

public function init()
public function init(): void
{
self::$data = [
'_id' => new ObjectId(),
Expand Down Expand Up @@ -83,39 +83,39 @@ public function init()
/**
* @Warmup(2)
*/
public function benchHydrateDocument()
public function benchHydrateDocument(): void
{
self::$hydrator->hydrate(new User(), self::$data);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithEmbedOne()
public function benchHydrateDocumentWithEmbedOne(): void
{
self::$hydrator->hydrate(new User(), self::$data + self::$embedOneData);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithEmbedMany()
public function benchHydrateDocumentWithEmbedMany(): void
{
self::$hydrator->hydrate(new User(), self::$data + self::$embedManyData);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithReferenceOne()
public function benchHydrateDocumentWithReferenceOne(): void
{
self::$hydrator->hydrate(new User(), self::$data + self::$referenceOneData);
}

/**
* @Warmup(2)
*/
public function benchHydrateDocumentWithReferenceMany()
public function benchHydrateDocumentWithReferenceMany(): void
{
self::$hydrator->hydrate(new User(), self::$data + self::$referenceManyData);
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/Document/LoadDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public function benchLoadEmbedMany(): void
/**
* @Warmup(2)
*/
public function benchLoadReferenceOne()
public function benchLoadReferenceOne(): void
{
$this->loadDocument()->getAccount()->getName();
}

/**
* @Warmup(2)
*/
public function benchLoadReferenceMany()
public function benchLoadReferenceMany(): void
{
$this->loadDocument()->getGroups()->forAll(static function (int $key, Group $group) {
return $group->getName() !== null;
Expand Down
10 changes: 5 additions & 5 deletions benchmark/Document/StoreDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class StoreDocumentBench extends BaseBench
/**
* @Warmup(2)
*/
public function benchStoreDocument()
public function benchStoreDocument(): void
{
$user = new User();
$user->setUsername('alcaeus');
Expand All @@ -32,7 +32,7 @@ public function benchStoreDocument()
/**
* @Warmup(2)
*/
public function benchStoreDocumentWithEmbedOne()
public function benchStoreDocumentWithEmbedOne(): void
{
$address = new Address();
$address->setAddress('Redacted');
Expand All @@ -51,7 +51,7 @@ public function benchStoreDocumentWithEmbedOne()
/**
* @Warmup(2)
*/
public function benchStoreDocumentWithEmbedMany()
public function benchStoreDocumentWithEmbedMany(): void
{
$user = new User();
$user->setUsername('alcaeus');
Expand All @@ -67,7 +67,7 @@ public function benchStoreDocumentWithEmbedMany()
/**
* @Warmup(2)
*/
public function benchStoreDocumentWithReferenceOne()
public function benchStoreDocumentWithReferenceOne(): void
{
$account = new Account();
$account->setName('alcaeus');
Expand All @@ -85,7 +85,7 @@ public function benchStoreDocumentWithReferenceOne()
/**
* @Warmup(2)
*/
public function benchStoreDocumentWithReferenceMany()
public function benchStoreDocumentWithReferenceMany(): void
{
$group1 = new Group('One');
$group2 = new Group('Two');
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"doctrine/coding-standard": "^9.0",
"jmikola/geojson": "^1.0",
"phpbench/phpbench": "^1.0.0",
"phpstan/phpstan": "^0.12.89",
"phpstan/phpstan-phpunit": "^0.12.19",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^8.5 || ^9",
"squizlabs/php_codesniffer": "^3.5",
"symfony/cache": "^4.4 || ^5.0 || ^6.0",
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class Builder
*/
private $class;

/** @var string */
/**
* @var string
* @psalm-var class-string
*/
private $hydrationClass;

/**
Expand All @@ -61,6 +64,8 @@ class Builder

/**
* Create a new aggregation builder.
*
* @psalm-param class-string $documentName
*/
public function __construct(DocumentManager $dm, string $documentName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Aggregation\Stage\Bucket;

use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Aggregation\Expr;
use Doctrine\ODM\MongoDB\Aggregation\Stage;

use function assert;
Expand All @@ -22,6 +23,8 @@ public function __construct(Builder $builder, Stage\BucketAuto $bucket)
/**
* An expression to group documents by. To specify a field path, prefix the
* field name with a dollar sign $ and enclose it in quotes.
*
* @param array|Expr|string $expression
*/
public function groupBy($expression): Stage\BucketAuto
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public function depthField(string $depthField): self
*
* The from collection cannot be sharded and must be in the same database as
* any other collections used in the operation.
*
* @psalm-param class-string|string $from
*/
public function from(string $from): self
{
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/ReplaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class ReplaceRoot extends Operator
/** @var string|array|Expr|null */
private $expression;

/**
* @param string|array|Expr|null $expression
*/
public function __construct(Builder $builder, DocumentManager $documentManager, ClassMetadata $class, $expression = null)
{
parent::__construct($builder);
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/MongoDB/DocumentNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/
final class DocumentNotFoundException extends MongoDBException
{
/**
* @param mixed $identifier
*/
public static function documentNotFound(string $className, $identifier): self
{
return new self(sprintf(
Expand Down
17 changes: 12 additions & 5 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* strategy?: string,
* association?: int,
* id?: bool,
* isOwningSide?: bool,
* collectionClass?: class-string,
* cascade?: list<string>|string,
* embedded?: bool,
Expand All @@ -90,7 +91,11 @@
* sparse?: bool,
* unique?: bool,
* index?: bool,
* index-name?: string,
* criteria?: array<string, string>,
* alsoLoadFields?: list<string>,
* order?: int|string,
* background?: bool
* }
* @psalm-type FieldMapping = array{
* type: string,
Expand All @@ -103,7 +108,7 @@
* isCascadeDetach: bool,
* isOwningSide: bool,
* isInverseSide: bool,
* strategy: string,
* strategy?: string,
* association?: int,
* id?: bool,
* collectionClass?: class-string,
Expand Down Expand Up @@ -134,9 +139,10 @@
* unique?: bool,
* index?: bool,
* criteria?: array<string, string>,
* alsoLoadFields?: list<string>,
* }
* @psalm-type AssociationFieldMapping = array{
* type: string,
* type?: string,
* fieldName: string,
* name: string,
* isCascadeRemove: bool,
Expand All @@ -148,7 +154,7 @@
* isInverseSide: bool,
* targetDocument: class-string|null,
* association: int,
* strategy: string,
* strategy?: string,
* id?: bool,
* collectionClass?: class-string,
* cascade?: list<string>|string,
Expand Down Expand Up @@ -177,6 +183,7 @@
* unique?: bool,
* index?: bool,
* criteria?: array<string, string>,
* alsoLoadFields?: list<string>,
* }
* @psalm-type IndexKeys = array<string, mixed>
* @psalm-type IndexOptions = array<string, mixed>
Expand Down Expand Up @@ -591,7 +598,7 @@
*
* @see discriminatorField
*
* @var mixed
* @psalm-var array<string, class-string>
*/
public $discriminatorMap = [];

Expand Down Expand Up @@ -1456,7 +1463,7 @@ public function isMappedToCollection(): bool
/**
* Validates the storage strategy of a mapping for consistency
*
* @psalm-param FieldMapping $mapping
* @psalm-param FieldMappingConfig $mapping
*
* @throws MappingException
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ private function getWriteOptions(array $options = []): array
return $writeOptions;
}

private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj): array
private function prepareReference(string $fieldName, object $value, array $mapping, bool $inNewObj): array
{
$reference = $this->dm->createReference($value, $mapping);
if ($inNewObj || $mapping['storeAs'] === ClassMetadata::REFERENCE_STORE_AS_ID) {
Expand Down
14 changes: 7 additions & 7 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public function prepareInsertData($document)
$insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new);

// @ReferenceOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE) {
$insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);

// @EmbedOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
} elseif ($mapping['association'] === ClassMetadata::EMBED_ONE) {
$insertData[$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new);

// @ReferenceMany, @EmbedMany
Expand Down Expand Up @@ -165,7 +165,7 @@ public function prepareUpdateData($document)
$updateData[$operator][$mapping['name']] = $value;

// @EmbedOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
} elseif ($mapping['association'] === ClassMetadata::EMBED_ONE) {
// If we have a new embedded document then lets set the whole thing
if ($this->uow->isScheduledForInsert($new)) {
$updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new);
Expand All @@ -181,7 +181,7 @@ public function prepareUpdateData($document)
}

// @ReferenceMany, @EmbedMany
} elseif (isset($mapping['association']) && $mapping['type'] === ClassMetadata::MANY) {
} elseif ($mapping['type'] === ClassMetadata::MANY) {
if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) {
$updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true);
} elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($new)) {
Expand All @@ -206,7 +206,7 @@ public function prepareUpdateData($document)
}

// @ReferenceOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE) {
$updateData['$set'][$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ public function prepareUpsertData($document)
$updateData[$operator][$mapping['name']] = $value;

// @EmbedOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) {
} elseif ($mapping['association'] === ClassMetadata::EMBED_ONE) {
// If we don't have a new value then do nothing on upsert
// If we have a new embedded document then lets set the whole thing
if ($this->uow->isScheduledForInsert($new)) {
Expand All @@ -285,7 +285,7 @@ public function prepareUpsertData($document)
}

// @ReferenceOne
} elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) {
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE) {
$updateData['$set'][$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new);

// @ReferenceMany, @EmbedMany
Expand Down
5 changes: 3 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1591,11 +1591,11 @@ public function where($javascript): self
/**
* Get Discriminator Values
*
* @param string[] $classNames
* @psalm-param class-string[] $classNames
*
* @throws InvalidArgumentException If the number of found collections > 1.
*/
private function getDiscriminatorValues($classNames): array
private function getDiscriminatorValues(array $classNames): array
{
$discriminatorValues = [];
$collections = [];
Expand All @@ -1615,6 +1615,7 @@ private function getDiscriminatorValues($classNames): array

/**
* @param string[]|string|null $documentName an array of document names or just one.
* @psalm-param class-string[]|class-string|null $documentName
*/
private function setDocumentName($documentName): void
{
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function primeReferences(ClassMetadata $class, $documents, string $fieldN
}
}

/** @psalm-var class-string $className */
foreach ($groupedIds as $className => $ids) {
$refClass = $this->dm->getClassMetadata($className);
call_user_func($primer, $this->dm, $refClass, array_values($ids), $hints);
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Repository/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function matching(Criteria $criteria): ArrayCollection
}

// @TODO: wrap around a specialized Collection for efficient count on large collections
/** @var Iterator<T> $iterator */
$iterator = $queryBuilder->getQuery()->execute();
assert($iterator instanceof Iterator);

Expand Down
Loading

0 comments on commit 240fc93

Please sign in to comment.