Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add type and return type declarations #93

Merged
merged 1 commit into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 3 additions & 48 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 All @@ -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\\<int, string\\>, array\\<int, \\(int\\|string\\)\\> 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

Expand Down
78 changes: 15 additions & 63 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,34 @@
<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="1">
<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>
Expand All @@ -55,82 +50,41 @@
</MixedOperand>
</file>
<file src="src/FixtureFactory.php">
<MissingParamType occurrences="8">
<code>$entity</code>
<code>$fieldName</code>
<MissingParamType occurrences="3">
<code>$fieldValue</code>
<code>$array</code>
<code>$entity</code>
<code>$classMetadata</code>
<code>$fieldName</code>
<code>$fieldValue</code>
</MissingParamType>
<MissingReturnType occurrences="4">
<code>get</code>
<code>getList</code>
<MissingReturnType occurrences="1">
<code>getAsSingleton</code>
<code>createCollectionFrom</code>
</MissingReturnType>
<MixedArgument occurrences="21">
<MixedArgument occurrences="7">
<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>$fieldValue</code>
<code>$fieldValue</code>
<code>$inversedBy</code>
</MixedArgument>
<MixedArgumentTypeCoercion occurrences="1">
<code>$extraFieldNames</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess occurrences="1">
<code>$association['inversedBy']</code>
</MixedArrayAccess>
<MixedArrayOffset occurrences="7">
<MixedArrayOffset occurrences="1">
<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="5">
<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">
<MixedPropertyTypeCoercion occurrences="1">
<code>$this-&gt;entityDefinitions</code>
<code>$this-&gt;singletons</code>
<code>$this-&gt;singletons</code>
</MixedPropertyTypeCoercion>
</file>
<file src="test/Fixture/FixtureFactory/Entity/Organization.php">
Expand Down Expand Up @@ -160,10 +114,8 @@
</InternalMethod>
</file>
<file src="test/Unit/FixtureFactoryTest.php">
<MixedArgument occurrences="5">
<MixedArgument occurrences="3">
<code>$name</code>
<code>$fixtureFactory-&gt;getList(Fixture\FixtureFactory\Entity\Organization::class)</code>
<code>$fixtureFactory-&gt;getList(Fixture\FixtureFactory\Entity\Organization::class, [], 5)</code>
<code>$fieldValues['name']</code>
<code>$fieldValues['name']</code>
</MixedArgument>
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
16 changes: 8 additions & 8 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,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,
[],
Expand Down
Loading