Skip to content

Commit

Permalink
Fix: Move operations from EntityDefinition to FixtureFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Mar 28, 2020
1 parent a0a815f commit f0619a7
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 122 deletions.
20 changes: 10 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$object of function method_exists expects object\\|string, callable given\\.$#"
count: 1
path: src/EntityDefinition.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\EntityDefinition\\:\\:normalizeFieldDefinition\\(\\) should return Closure but returns object\\.$#"
count: 1
path: src/EntityDefinition.php

-
message: "#^Class \"Ergebnis\\\\FactoryBot\\\\Exception\\\\EntityDefinitionUnavailable\" is not allowed to extend \"OutOfRangeException\"\\.$#"
count: 1
Expand Down Expand Up @@ -45,6 +35,16 @@ parameters:
count: 1
path: src/FixtureFactory.php

-
message: "#^Parameter \\#1 \\$object of function method_exists expects object\\|string, callable given\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:normalizeFieldDefinition\\(\\) should return callable\\(\\)\\: mixed but returns object\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\Test\\\\Fixture\\\\FixtureFactory\\\\Entity\\\\Organization\\:\\:id\\(\\) has a nullable return type declaration\\.$#"
count: 1
Expand Down
50 changes: 25 additions & 25 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.10.1@eeed5ecccc10131397f0eb7ee6da810c0be3a7fc">
<file src="src/EntityDefinition.php">
<InvalidArgument occurrences="1">
<code>$fieldDefinition</code>
</InvalidArgument>
<MissingClosureReturnType occurrences="4">
<code>static function () {</code>
<code>static function () use ($defaultFieldValue) {</code>
<code>static function () use ($fieldDefinition) {</code>
<code>static function () use ($fieldDefinition) {</code>
</MissingClosureReturnType>
<MixedAssignment occurrences="2">
<code>$fieldDefinition</code>
<code>$defaultFieldValue</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>\Closure</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>$fieldDefinition</code>
</MixedReturnStatement>
<TypeDoesNotContainType occurrences="1">
<code>\method_exists($fieldDefinition, '__invoke')</code>
</TypeDoesNotContainType>
</file>
<file src="src/FieldDefinition.php">
<MissingClosureReturnType occurrences="1">
<code>static function () use (&amp;$n, $funcOrString) {</code>
Expand All @@ -39,6 +15,15 @@
</MixedOperand>
</file>
<file src="src/FixtureFactory.php">
<InvalidArgument occurrences="1">
<code>$fieldDefinition</code>
</InvalidArgument>
<MissingClosureReturnType occurrences="4">
<code>static function () {</code>
<code>static function () use ($defaultFieldValue) {</code>
<code>static function () use ($fieldDefinition) {</code>
<code>static function () use ($fieldDefinition) {</code>
</MissingClosureReturnType>
<MissingParamType occurrences="3">
<code>$fieldValue</code>
<code>$array</code>
Expand All @@ -54,12 +39,22 @@
<code>$configuration</code>
<code>$fieldOverrides</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="4">
<MixedAssignment occurrences="5">
<code>$defaultFieldValue</code>
<code>$fieldValues[$fieldName]</code>
<code>$fieldValue</code>
<code>$inversedBy</code>
<code>$collection</code>
</MixedAssignment>
<MixedInferredReturnType occurrences="1">
<code>callable</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>$fieldDefinition</code>
</MixedReturnStatement>
<TypeDoesNotContainType occurrences="1">
<code>\method_exists($fieldDefinition, '__invoke')</code>
</TypeDoesNotContainType>
</file>
<file src="test/Fixture/FixtureFactory/Entity/Organization.php">
<PropertyNotSetInConstructor occurrences="1">
Expand Down Expand Up @@ -87,6 +82,11 @@
<code>addToAssertionCount</code>
</InternalMethod>
</file>
<file src="test/Unit/EntityDefinitionTest.php">
<MissingClosureParamType occurrences="1">
<code>$entity</code>
</MissingClosureParamType>
</file>
<file src="test/Unit/FixtureFactoryTest.php">
<MixedArgument occurrences="3">
<code>$name</code>
Expand Down
89 changes: 3 additions & 86 deletions src/EntityDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,83 +20,22 @@
*/
final class EntityDefinition
{
/**
* @var ORM\Mapping\ClassMetadata
*/
private $classMetadata;

/**
* @var array<string, \Closure>
*/
private $fieldDefinitions;

/**
* @var array<string, callable>
*/
private $configuration;

/**
* @param ORM\Mapping\ClassMetadata $classMetadata
* @param array<string, mixed> $fieldDefinitions
* @param array<string, callable> $fieldDefinitions
* @param array<string, callable> $configuration
*
* @throws Exception\InvalidFieldNames
* @throws \Exception
*/
public function __construct(ORM\Mapping\ClassMetadata $classMetadata, array $fieldDefinitions, array $configuration)
{
$this->classMetadata = $classMetadata;
$this->fieldDefinitions = [];
$this->fieldDefinitions = $fieldDefinitions;
$this->configuration = $configuration;

/** @var string[] $allFieldNames */
$allFieldNames = \array_merge(
\array_keys($this->classMetadata->fieldMappings),
\array_keys($this->classMetadata->associationMappings),
\array_keys($this->classMetadata->embeddedClasses)
);

$fieldNames = \array_filter($allFieldNames, static function (string $fieldName): bool {
return false === \strpos($fieldName, '.');
});

$extraFieldNames = \array_diff(
\array_keys($fieldDefinitions),
$fieldNames
);

if ([] !== $extraFieldNames) {
throw Exception\InvalidFieldNames::notFoundIn(
$classMetadata->getName(),
...$extraFieldNames
);
}

foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
$this->fieldDefinitions[$fieldName] = self::normalizeFieldDefinition($fieldDefinition);
}

$defaultEntity = $this->classMetadata->newInstance();

foreach ($fieldNames as $fieldName) {
if (\array_key_exists($fieldName, $this->fieldDefinitions)) {
continue;
}

$defaultFieldValue = $this->classMetadata->getFieldValue($defaultEntity, $fieldName);

if (null === $defaultFieldValue) {
$this->fieldDefinitions[$fieldName] = static function () {
return null;
};

continue;
}

$this->fieldDefinitions[$fieldName] = static function () use ($defaultFieldValue) {
return $defaultFieldValue;
};
}
}

/**
Expand All @@ -112,7 +51,7 @@ public function className(): string
/**
* Returns the fielde definition callbacks.
*
* @return array<string, \Closure>
* @return array<string, callable>
*/
public function fieldDefinitions(): array
{
Expand All @@ -138,26 +77,4 @@ public function configuration(): array
{
return $this->configuration;
}

/**
* @param callable|\Closure|mixed $fieldDefinition
*
* @return \Closure
*/
private static function normalizeFieldDefinition($fieldDefinition): \Closure
{
if (\is_callable($fieldDefinition)) {
if (\method_exists($fieldDefinition, '__invoke')) {
return $fieldDefinition;
}

return static function () use ($fieldDefinition) {
return \call_user_func_array($fieldDefinition, \func_get_args());
};
}

return static function () use ($fieldDefinition) {
return $fieldDefinition;
};
}
}
75 changes: 75 additions & 0 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function __construct(ORM\EntityManagerInterface $entityManager)
* @param array $fieldDefinitions
* @param array $configuration
*
* @throws \Exception
* @throws Exception\InvalidFieldNames
*
* @return FixtureFactory
*/
public function defineEntity(string $name, array $fieldDefinitions = [], array $configuration = [])
Expand Down Expand Up @@ -84,6 +87,56 @@ public function defineEntity(string $name, array $fieldDefinitions = [], array $
));
}

/** @var string[] $allFieldNames */
$allFieldNames = \array_merge(
\array_keys($classMetadata->fieldMappings),
\array_keys($classMetadata->associationMappings),
\array_keys($classMetadata->embeddedClasses)
);

$fieldNames = \array_filter($allFieldNames, static function (string $fieldName): bool {
return false === \strpos($fieldName, '.');
});

/** @var array<int, string> $extraFieldNames */
$extraFieldNames = \array_diff(
\array_keys($fieldDefinitions),
$fieldNames
);

if ([] !== $extraFieldNames) {
throw Exception\InvalidFieldNames::notFoundIn(
$classMetadata->getName(),
...$extraFieldNames
);
}

$fieldDefinitions = \array_map(static function ($fieldDefinition): callable {
return self::normalizeFieldDefinition($fieldDefinition);
}, $fieldDefinitions);

$defaultEntity = $classMetadata->newInstance();

foreach ($fieldNames as $fieldName) {
if (\array_key_exists($fieldName, $fieldDefinitions)) {
continue;
}

$defaultFieldValue = $classMetadata->getFieldValue($defaultEntity, $fieldName);

if (null === $defaultFieldValue) {
$fieldDefinitions[$fieldName] = static function () {
return null;
};

continue;
}

$fieldDefinitions[$fieldName] = static function () use ($defaultFieldValue) {
return $defaultFieldValue;
};
}

$this->entityDefinitions[$name] = new EntityDefinition(
$classMetadata,
$fieldDefinitions,
Expand Down Expand Up @@ -311,4 +364,26 @@ private function updateCollectionSideOfAssocation(
}
}
}

/**
* @param callable|\Closure|mixed|object $fieldDefinition
*
* @return callable
*/
private static function normalizeFieldDefinition($fieldDefinition): callable
{
if (\is_callable($fieldDefinition)) {
if (\method_exists($fieldDefinition, '__invoke')) {
return $fieldDefinition;
}

return static function () use ($fieldDefinition) {
return \call_user_func_array($fieldDefinition, \func_get_args());
};
}

return static function () use ($fieldDefinition) {
return $fieldDefinition;
};
}
}
1 change: 0 additions & 1 deletion test/AutoReview/SrcCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function testSrcClassesHaveUnitTests(): void
'Ergebnis\\FactoryBot\\',
'Ergebnis\\FactoryBot\\Test\\Unit',
[
FactoryBot\EntityDefinition::class,
FactoryBot\FieldDefinition::class,
]
);
Expand Down
Loading

0 comments on commit f0619a7

Please sign in to comment.