From 90f532675d4d482b19d1edc7dafaa4cdc4de55a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 28 Mar 2020 12:20:56 +0100 Subject: [PATCH] Fix: Add type and return type declarations --- phpstan-baseline.neon | 51 ++------------------------ psalm-baseline.xml | 78 ++++++++-------------------------------- src/EntityDefinition.php | 17 ++++++--- src/FieldDefinition.php | 16 ++++----- src/FixtureFactory.php | 48 ++++++++++++++----------- 5 files changed, 65 insertions(+), 145 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index edae79b9..beb421a5 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6,22 +6,12 @@ parameters: path: src/EntityDefinition.php - - message: "#^Method Ergebnis\\\\FactoryBot\\\\EntityDefinition\\:\\:fieldDefinitions\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/EntityDefinition.php - - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\EntityDefinition\\:\\:normalizeFieldDefinition\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/EntityDefinition.php - - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\EntityDefinition\\:\\:normalizeFieldDefinition\\(\\) has parameter \\$fieldDefinition with no typehint specified\\.$#" + message: "#^Parameter \\#1 \\$object of function method_exists expects object\\|string, callable given\\.$#" count: 1 path: src/EntityDefinition.php - - message: "#^Parameter \\#1 \\$object of function method_exists expects object\\|string, callable given\\.$#" + message: "#^Method Ergebnis\\\\FactoryBot\\\\EntityDefinition\\:\\:normalizeFieldDefinition\\(\\) should return Closure but returns object\\.$#" count: 1 path: src/EntityDefinition.php @@ -40,63 +30,28 @@ parameters: count: 1 path: src/FieldDefinition.php - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:get\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - message: "#^Parameter \\#2 \\.\\.\\.\\$fieldNames of static method Ergebnis\\\\FactoryBot\\\\Exception\\\\InvalidFieldNames\\:\\:notFoundIn\\(\\) expects array\\, array\\ given\\.$#" count: 1 path: src/FixtureFactory.php - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:getList\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:getAsSingleton\\(\\) has no return typehint specified\\.$#" count: 1 path: src/FixtureFactory.php - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:setField\\(\\) has parameter \\$entity with no typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:setField\\(\\) has parameter \\$fieldName with no typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:setField\\(\\) has parameter \\$fieldValue with no typehint specified\\.$#" count: 1 path: src/FixtureFactory.php - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createCollectionFrom\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createCollectionFrom\\(\\) has parameter \\$array with no typehint specified\\.$#" count: 1 path: src/FixtureFactory.php - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$classMetadata with no typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$entity with no typehint specified\\.$#" - count: 1 - path: src/FixtureFactory.php - - - - message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:updateCollectionSideOfAssocation\\(\\) has parameter \\$fieldName with no typehint specified\\.$#" + message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createCollectionFrom\\(\\) return type with generic class Doctrine\\\\Common\\\\Collections\\\\ArrayCollection does not specify its types\\: TKey, T$#" count: 1 path: src/FixtureFactory.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 2db4708d..12ce25d4 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -10,17 +10,10 @@ static function () use ($fieldDefinition) { static function () use ($fieldDefinition) { - - $fieldDefinition - $fieldDefinitions $configuration - - fieldDefinitions - normalizeFieldDefinition - $extraFieldNames @@ -28,21 +21,23 @@ $fieldDefinition $defaultFieldValue - + + array<string, \Closure> array + \Closure - + + $this->fieldDefinitions $this->configuration + $fieldDefinition \method_exists($fieldDefinition, '__invoke') - + static function () use (&$n, $funcOrString) { - static function (FixtureFactory $factory) use ($name) { - static function (FixtureFactory $factory) use ($name, $numberOfInstances) { $n @@ -55,41 +50,19 @@ - - $entity - $fieldName + $fieldValue $array - $entity - $classMetadata - $fieldName $fieldValue - - get - getList + getAsSingleton - createCollectionFrom - + $name $name $type $type - $name - $name - $name - $entityDefinition->fieldDefinitions() - $fieldName - $name - $name - $name - $fieldName - $entity - $fieldName - $entity - $fieldName - $fieldName $fieldValue $fieldValue $inversedBy @@ -97,40 +70,21 @@ $extraFieldNames - - $association['inversedBy'] - - + $this->entityDefinitions[$name] - $fieldOverrides[$fieldName] - $fieldValues[$fieldName] - $this->singletons[$name] - $this->singletons[$name] - $this->singletons[$name] - $this->singletons[$name] - + $type - $fieldDefinition $fieldValues[$fieldName] $fieldValue - $instances[] - $this->persist - $association $inversedBy $collection - - $fieldDefinition($this) + $configuration['afterCreate']($entity, $fieldValues) - - getAssociationMapping - - + $this->entityDefinitions - $this->singletons - $this->singletons @@ -160,10 +114,8 @@ - + $name - $fixtureFactory->getList(Fixture\FixtureFactory\Entity\Organization::class) - $fixtureFactory->getList(Fixture\FixtureFactory\Entity\Organization::class, [], 5) $fieldValues['name'] $fieldValues['name'] diff --git a/src/EntityDefinition.php b/src/EntityDefinition.php index ec419b8b..88b2fa2d 100644 --- a/src/EntityDefinition.php +++ b/src/EntityDefinition.php @@ -98,15 +98,17 @@ public function __construct(ORM\Mapping\ClassMetadata $classMetadata, array $fie * * @return string */ - public function className() + public function className(): string { return $this->classMetadata->getName(); } /** * Returns the fielde definition callbacks. + * + * @return array */ - public function fieldDefinitions() + public function fieldDefinitions(): array { return $this->fieldDefinitions; } @@ -116,7 +118,7 @@ public function fieldDefinitions() * * @return ORM\Mapping\ClassMetadata */ - public function classMetadata() + public function classMetadata(): ORM\Mapping\ClassMetadata { return $this->classMetadata; } @@ -126,12 +128,17 @@ public function classMetadata() * * @return array */ - public function configuration() + public function configuration(): array { return $this->configuration; } - private function normalizeFieldDefinition($fieldDefinition) + /** + * @param callable|\Closure|mixed $fieldDefinition + * + * @return \Closure + */ + private function normalizeFieldDefinition($fieldDefinition): \Closure { if (\is_callable($fieldDefinition)) { if (\method_exists($fieldDefinition, '__invoke')) { diff --git a/src/FieldDefinition.php b/src/FieldDefinition.php index 49a5fc4a..c33d9f74 100644 --- a/src/FieldDefinition.php +++ b/src/FieldDefinition.php @@ -33,9 +33,9 @@ final class FieldDefinition * @param callable|string $funcOrString the function or pattern to generate a value from * @param int $firstNum the first number to use * - * @return callable + * @return \Closure */ - public static function sequence($funcOrString, $firstNum = 1) + public static function sequence($funcOrString, int $firstNum = 1): \Closure { $n = $firstNum - 1; @@ -71,11 +71,11 @@ public static function sequence($funcOrString, $firstNum = 1) * * @param string $name the name of the entity to get * - * @return callable + * @return \Closure */ - public static function reference($name) + public static function reference(string $name): \Closure { - return static function (FixtureFactory $factory) use ($name) { + return static function (FixtureFactory $factory) use ($name): object { return $factory->get($name); }; } @@ -93,15 +93,15 @@ public static function reference($name) * * @throws \InvalidArgumentException * - * @return callable + * @return \Closure */ - public static function references($name, $numberOfInstances = 1) + public static function references(string $name, int $numberOfInstances = 1): \Closure { if (1 > $numberOfInstances) { throw new \InvalidArgumentException('Can only get >= 1 instances'); } - return static function (FixtureFactory $factory) use ($name, $numberOfInstances) { + return static function (FixtureFactory $factory) use ($name, $numberOfInstances): array { return $factory->getList( $name, [], diff --git a/src/FixtureFactory.php b/src/FixtureFactory.php index cbfe1f42..3d4d752a 100644 --- a/src/FixtureFactory.php +++ b/src/FixtureFactory.php @@ -101,13 +101,13 @@ public function defineEntity($name, array $fieldDefinitions = [], array $configu * * If you've called `persistOnGet()` then the entity is also persisted. * - * @param mixed $name - * @param array $fieldOverrides + * @param string $name + * @param array $fieldOverrides * * @throws Exception\EntityDefinitionUnavailable * @throws Exception\InvalidFieldNames */ - public function get($name, array $fieldOverrides = []) + public function get(string $name, array $fieldOverrides = []): object { if (\array_key_exists($name, $this->singletons)) { return $this->singletons[$name]; @@ -170,11 +170,13 @@ public function get($name, array $fieldOverrides = []) * * If you've called `persistOnGet()` then the entities are also persisted. * - * @param mixed $name - * @param array $fieldOverrides - * @param mixed $numberOfInstances + * @param string $name + * @param array $fieldOverrides + * @param int $numberOfInstances + * + * @return object[] */ - public function getList($name, array $fieldOverrides = [], $numberOfInstances = 1) + public function getList(string $name, array $fieldOverrides = [], int $numberOfInstances = 1): array { if (1 > $numberOfInstances) { throw new \InvalidArgumentException('Can only get >= 1 instances'); @@ -198,9 +200,9 @@ public function getList($name, array $fieldOverrides = [], $numberOfInstances = * By default it does not. In any case, you still need to call * flush() yourself. * - * @param mixed $enabled + * @param bool $enabled */ - public function persistOnGet($enabled = true): void + public function persistOnGet(bool $enabled = true): void { $this->persist = $enabled; } @@ -210,10 +212,10 @@ public function persistOnGet($enabled = true): void * * It's illegal to call this if `$name` already has a singleton. * - * @param mixed $name - * @param array $fieldOverrides + * @param string $name + * @param array $fieldOverrides */ - public function getAsSingleton($name, array $fieldOverrides = []) + public function getAsSingleton(string $name, array $fieldOverrides = []) { if (\array_key_exists($name, $this->singletons)) { throw new \Exception(\sprintf( @@ -232,10 +234,10 @@ public function getAsSingleton($name, array $fieldOverrides = []) * * This causes `get($name)` to return `$entity`. * - * @param mixed $name - * @param mixed $entity + * @param string $name + * @param object $entity */ - public function setSingleton($name, $entity): void + public function setSingleton(string $name, object $entity): void { $this->singletons[$name] = $entity; } @@ -245,9 +247,9 @@ public function setSingleton($name, $entity): void * * This causes `get($name)` to return new entities again. * - * @param mixed $name + * @param string $name */ - public function unsetSingleton($name): void + public function unsetSingleton(string $name): void { unset($this->singletons[$name]); } @@ -260,7 +262,7 @@ public function definitions(): array return $this->entityDefinitions; } - private function setField($entity, EntityDefinition $entityDefinition, $fieldName, $fieldValue): void + private function setField(object $entity, EntityDefinition $entityDefinition, string $fieldName, $fieldValue): void { $classMetadata = $entityDefinition->classMetadata(); @@ -275,7 +277,7 @@ private function setField($entity, EntityDefinition $entityDefinition, $fieldNam } } - private function createCollectionFrom($array = []) + private function createCollectionFrom($array = []): Common\Collections\ArrayCollection { if (\is_array($array)) { return new Common\Collections\ArrayCollection($array); @@ -284,8 +286,12 @@ private function createCollectionFrom($array = []) return new Common\Collections\ArrayCollection(); } - private function updateCollectionSideOfAssocation($entity, $classMetadata, $fieldName, $fieldValue): void - { + private function updateCollectionSideOfAssocation( + object $entity, + ORM\Mapping\ClassMetadata $classMetadata, + string $fieldName, + $fieldValue + ): void { $association = $classMetadata->getAssociationMapping($fieldName); $inversedBy = $association['inversedBy'];