Skip to content

Commit

Permalink
add missing type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Nov 17, 2021
1 parent 9a54beb commit a6fcd96
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 26 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
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
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 @@ -591,7 +591,7 @@
*
* @see discriminatorField
*
* @var mixed
* @psalm-var array<string, class-string>
*/
public $discriminatorMap = [];

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
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
3 changes: 3 additions & 0 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2749,6 +2749,8 @@ public function getOwningDocument(object $document): object
*
* @param FieldMapping $mapping
* @param array<string, mixed>|null $data
*
* @psalm-return class-string
*/
public function getClassNameForAssociation(array $mapping, $data): string
{
Expand Down Expand Up @@ -2806,6 +2808,7 @@ public function getOrCreateDocument(string $className, array $data, array &$hint
}

if ($discriminatorValue !== null) {
/** @psalm-var class-string<T> $className */
$className = $class->discriminatorMap[$discriminatorValue] ?? $discriminatorValue;

$class = $this->dm->getClassMetadata($className);
Expand Down
14 changes: 14 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,17 @@ parameters:
message: "#^Parameter \\#2 \\$mapping of method Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\Driver\\\\XmlDriver\\:\\:addFieldMapping\\(\\) expects array\\(\\?'type' \\=\\> string, \\?'fieldName' \\=\\> string, \\?'name' \\=\\> string, \\?'strategy' \\=\\> string, \\?'association' \\=\\> int, \\?'id' \\=\\> bool, \\?'collectionClass' \\=\\> class\\-string, \\?'cascade' \\=\\> array\\<int, string\\>\\|string, \\.\\.\\.\\), array\\<int\\|string, \\(array\\<int, string\\>&nonEmpty\\)\\|bool\\|string\\> given\\.$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

# This is handled by a try-catch block
-
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
paths:
- lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php
- lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Lookup.php
- lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Out.php

# $this->mapping['targetDocument'] is class-string<T>
-
message: "#^Unable to resolve the template type T in call to method Doctrine\\\\ODM\\\\MongoDB\\\\DocumentManager\\:\\:getClassMetadata\\(\\)$#"
count: 1
path: lib/Doctrine/ODM/MongoDB/PersistentCollection.php
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ parameters:
- tests/Hydrators/
- tests/PersistentCollections/
- tests/Proxies/
# To be removed when reaching phpstan level 6
checkMissingVarTagTypehint: true
checkMissingTypehints: true

# To be removed when reaching phpstan level 6
rules:
- PHPStan\Rules\Constants\MissingClassConstantTypehintRule
- PHPStan\Rules\Functions\MissingFunctionParameterTypehintRule
- PHPStan\Rules\Functions\MissingFunctionReturnTypehintRule
- PHPStan\Rules\Methods\MissingMethodParameterTypehintRule
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule
- PHPStan\Rules\Properties\MissingPropertyTypehintRule
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class MyEventListener
/** @psalm-var array<string, list<class-string>> */
public $called = [];

public function __call(string $method, array $args)
public function __call(string $method, array $args): void
{
$document = $args[0]->getDocument();
$className = get_class($document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function getSubscribedEvents(): array
];
}

public function __call(string $eventName, array $args)
public function __call(string $eventName, array $args): void
{
$document = $args[0]->getDocument();
if (! ($document instanceof User)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ public static function dataProviderTestWriteConcern(): array

/**
* @param int|string $writeConcern
* @psalm-param class-string $class
*
* @dataProvider dataProviderTestWriteConcern
*/
Expand All @@ -668,6 +669,7 @@ public function testExecuteInsertsRespectsWriteConcern(string $class, $writeConc

/**
* @param int|string $writeConcern
* @psalm-param class-string $class
*
* @dataProvider dataProviderTestWriteConcern
*/
Expand All @@ -692,6 +694,7 @@ public function testExecuteUpsertsRespectsWriteConcern(string $class, $writeConc

/**
* @param int|string $writeConcern
* @psalm-param class-string $class
*
* @dataProvider dataProviderTestWriteConcern
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getSubscribedEvents(): array
return $this->events;
}

public function __call(string $eventName, array $args)
public function __call(string $eventName, array $args): void
{
$this->called[] = [$eventName, get_class($args[0]->getDocument())];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MODM83EventListener
/** @var array<string, class-string[]> */
public $called = [];

public function __call(string $method, array $args)
public function __call(string $method, array $args): void
{
$document = $args[0]->getDocument();
$className = get_class($document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class MODM90EventListener
/** @var array<string, class-string[]> */
public $called = [];

public function __call(string $method, array $args)
public function __call(string $method, array $args): void
{
$document = $args[0]->getDocument();
$className = get_class($document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MODM91EventListener
/** @var array<string, class-string[]> */
public $called = [];

public function __call(string $method, array $args)
public function __call(string $method, array $args): void
{
$document = $args[0]->getDocument();
$className = get_class($document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\ODM\MongoDB\Tests\Mapping\Symfony;

use Doctrine\Persistence\Mapping\Driver\FileDriver;
use Doctrine\Persistence\Mapping\MappingException;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
Expand Down Expand Up @@ -89,7 +90,10 @@ protected function tearDown(): void
@rmdir($this->dir);
}

abstract protected function getFileExtension();
abstract protected function getFileExtension(): string;

/**
* @return FileDriver
*/
abstract protected function getDriver(array $paths = []);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function testResolveTargetDocumentListenerCanRetrieveTargetDocumentByAbst

interface ResolveTargetInterface
{
/**
* @return mixed
*/
public function getId();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Documents/Ecommerce/ConfigurableProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function setName(string $name): ConfigurableProduct
return $this;
}

/***
/**
* @return Collection<int, Option>|array<Option>
*/
public function getOptions()
Expand Down

0 comments on commit a6fcd96

Please sign in to comment.