Skip to content

Commit

Permalink
Fix: Add type and return type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Mar 28, 2020
1 parent 0d02aa9 commit c7e6ddc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 92 deletions.
36 changes: 3 additions & 33 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -60,11 +50,6 @@ parameters:
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
Expand All @@ -75,28 +60,13 @@ parameters:
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

Expand Down
60 changes: 20 additions & 40 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,77 @@
<code>static function () use ($fieldDefinition) {</code>
<code>static function () use ($fieldDefinition) {</code>
</MissingClosureReturnType>
<MissingParamType occurrences="1">
<code>$fieldDefinition</code>
</MissingParamType>
<MissingPropertyType occurrences="2">
<code>$fieldDefinitions</code>
<code>$configuration</code>
</MissingPropertyType>
<MissingReturnType occurrences="2">
<code>fieldDefinitions</code>
<code>normalizeFieldDefinition</code>
</MissingReturnType>
<MixedArgumentTypeCoercion occurrences="1">
<code>$extraFieldNames</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="2">
<code>$fieldDefinition</code>
<code>$defaultFieldValue</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<MixedInferredReturnType occurrences="3">
<code>array&lt;string, \Closure&gt;</code>
<code>array</code>
<code>\Closure</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<MixedReturnStatement occurrences="3">
<code>$this-&gt;fieldDefinitions</code>
<code>$this-&gt;configuration</code>
<code>$fieldDefinition</code>
</MixedReturnStatement>
<TypeDoesNotContainType occurrences="1">
<code>\method_exists($fieldDefinition, '__invoke')</code>
</TypeDoesNotContainType>
</file>
<file src="src/FieldDefinition.php">
<MissingClosureReturnType occurrences="3">
<MissingClosureReturnType occurrences="2">
<code>static function () use (&amp;$n, $funcOrString) {</code>
<code>static function (FixtureFactory $factory) use ($name) {</code>
<code>static function (FixtureFactory $factory) use ($name, $numberOfInstances) {</code>
</MissingClosureReturnType>
<MixedArgument occurrences="1">
<code>$n</code>
</MixedArgument>
<MixedInferredReturnType occurrences="1">
<code>object</code>
</MixedInferredReturnType>
<MixedOperand occurrences="4">
<code>$n</code>
<code>$n</code>
<code>$n</code>
<code>$n</code>
</MixedOperand>
<MixedReturnStatement occurrences="1">
<code>$factory-&gt;get($name)</code>
</MixedReturnStatement>
</file>
<file src="src/FixtureFactory.php">
<MissingParamType occurrences="8">
<code>$entity</code>
<MissingParamType occurrences="4">
<code>$fieldName</code>
<code>$fieldValue</code>
<code>$array</code>
<code>$entity</code>
<code>$classMetadata</code>
<code>$fieldName</code>
<code>$fieldValue</code>
</MissingParamType>
<MissingReturnType occurrences="4">
<MissingReturnType occurrences="3">
<code>get</code>
<code>getList</code>
<code>getAsSingleton</code>
<code>createCollectionFrom</code>
</MissingReturnType>
<MixedArgument occurrences="21">
<MixedArgument occurrences="17">
<code>$name</code>
<code>$name</code>
<code>$type</code>
<code>$type</code>
<code>$name</code>
<code>$name</code>
<code>$name</code>
<code>$entityDefinition-&gt;fieldDefinitions()</code>
<code>$fieldName</code>
<code>$name</code>
<code>$name</code>
<code>$name</code>
<code>$fieldName</code>
<code>$entity</code>
<code>$fieldName</code>
<code>$entity</code>
<code>$fieldName</code>
<code>$fieldName</code>
<code>$fieldName</code>
<code>$fieldValue</code>
Expand All @@ -97,36 +90,23 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>$extraFieldNames</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess occurrences="1">
<code>$association['inversedBy']</code>
</MixedArrayAccess>
<MixedArrayOffset occurrences="7">
<MixedArrayOffset occurrences="3">
<code>$this-&gt;entityDefinitions[$name]</code>
<code>$fieldOverrides[$fieldName]</code>
<code>$fieldValues[$fieldName]</code>
<code>$this-&gt;singletons[$name]</code>
<code>$this-&gt;singletons[$name]</code>
<code>$this-&gt;singletons[$name]</code>
<code>$this-&gt;singletons[$name]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="9">
<MixedAssignment occurrences="7">
<code>$type</code>
<code>$fieldDefinition</code>
<code>$fieldValues[$fieldName]</code>
<code>$fieldValue</code>
<code>$instances[]</code>
<code>$this-&gt;persist</code>
<code>$association</code>
<code>$inversedBy</code>
<code>$collection</code>
</MixedAssignment>
<MixedFunctionCall occurrences="2">
<code>$fieldDefinition($this)</code>
<MixedFunctionCall occurrences="1">
<code>$configuration['afterCreate']($entity, $fieldValues)</code>
</MixedFunctionCall>
<MixedMethodCall occurrences="1">
<code>getAssociationMapping</code>
</MixedMethodCall>
<MixedPropertyTypeCoercion occurrences="3">
<code>$this-&gt;entityDefinitions</code>
<code>$this-&gt;singletons</code>
Expand Down
17 changes: 12 additions & 5 deletions src/EntityDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, \Closure>
*/
public function fieldDefinitions()
public function fieldDefinitions(): array
{
return $this->fieldDefinitions;
}
Expand All @@ -116,7 +118,7 @@ public function fieldDefinitions()
*
* @return ORM\Mapping\ClassMetadata
*/
public function classMetadata()
public function classMetadata(): ORM\Mapping\ClassMetadata
{
return $this->classMetadata;
}
Expand All @@ -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')) {
Expand Down
14 changes: 7 additions & 7 deletions src/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
};
}
Expand All @@ -93,9 +93,9 @@ 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');
Expand Down
18 changes: 11 additions & 7 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
{
if (\array_key_exists($name, $this->singletons)) {
return $this->singletons[$name];
Expand Down Expand Up @@ -260,7 +260,7 @@ public function definitions(): array
return $this->entityDefinitions;
}

private function setField($entity, EntityDefinition $entityDefinition, $fieldName, $fieldValue): void
private function setField(object $entity, EntityDefinition $entityDefinition, $fieldName, $fieldValue): void
{
$classMetadata = $entityDefinition->classMetadata();

Expand All @@ -275,7 +275,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);
Expand All @@ -284,8 +284,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'];
Expand Down

0 comments on commit c7e6ddc

Please sign in to comment.