Skip to content

Commit

Permalink
Fix: Resolve inversedBy from array or Doctrine\ORM\Mapping\Associatio…
Browse files Browse the repository at this point in the history
…nMapping
  • Loading branch information
localheinz committed Feb 7, 2024
1 parent e6f0d4d commit 1293591
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 11 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ parameters:
path: src/FieldDefinition/References.php

-
message: "#^Call to function array_key_exists\\(\\) with 'inversedBy' and array\\{cache\\?\\: array, cascade\\: array\\<string\\>, declared\\?\\: class\\-string, fetch\\: mixed, fieldName\\: string, id\\?\\: bool, inherited\\?\\: class\\-string, indexBy\\?\\: string, \\.\\.\\.\\} will always evaluate to true\\.$#"
message: "#^Call to function is_array\\(\\) with array\\{cache\\?\\: array, cascade\\: array\\<string\\>, declared\\?\\: class\\-string, fetch\\: mixed, fieldName\\: string, id\\?\\: bool, inherited\\?\\: class\\-string, indexBy\\?\\: string, \\.\\.\\.\\} will always evaluate to true\\.$#"
count: 1
path: src/FixtureFactory.php

Expand All @@ -173,6 +173,11 @@ parameters:
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:resolveInversedBy\\(\\) has a nullable return type declaration\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Parameter \\#1 \\$className of method Doctrine\\\\ORM\\\\EntityManagerInterface\\:\\:getClassMetadata\\(\\) expects class\\-string\\<object\\>, class\\-string\\<T\\> given\\.$#"
count: 1
Expand All @@ -193,6 +198,11 @@ parameters:
count: 1
path: src/FixtureFactory.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\Test\\\\DataProvider\\\\IntProvider\\:\\:faker\\(\\) is protected, but since the containing class is final, it can be private\\.$#"
count: 1
Expand Down
9 changes: 9 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
<MixedAssignment>
<code>$fieldValue</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>?string</code>
</MixedInferredReturnType>
<RedundantConditionGivenDocblockType>
<code>\is_array($association)</code>
</RedundantConditionGivenDocblockType>
<TooManyArguments>
<code><![CDATA[function () use ($className, $fieldDefinitionOverrides) {
return $this->createOne(
Expand All @@ -95,6 +101,9 @@
);
}]]></code>
</TooManyArguments>
<UndefinedClass>
<code>ORM\Mapping\AssociationMapping</code>
</UndefinedClass>
<UnnecessaryVarAnnotation>
<code>EntityDefinition</code>
<code>EntityDefinitionProvider</code>
Expand Down
32 changes: 21 additions & 11 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,22 +434,15 @@ private function updateCollectionSideOfAssociation(
string $fieldName,
object $fieldValue,
): void {
$association = $classMetadata->getAssociationMapping($fieldName);

if (!\array_key_exists('inversedBy', $association)) {
return;
}

$inversedBy = $association['inversedBy'];
$inversedBy = $this->resolveInversedBy(
$classMetadata,
$fieldName,
);

if (!\is_string($inversedBy)) {
return;
}

if ('' === $inversedBy) {
return;
}

$classMetadataOfFieldValue = $this->entityManager->getClassMetadata($fieldValue::class);

$collection = $classMetadataOfFieldValue->getFieldValue(
Expand All @@ -463,4 +456,21 @@ private function updateCollectionSideOfAssociation(

$collection->add($entity);
}

private function resolveInversedBy(
ORM\Mapping\ClassMetadata $classMetadata,
string $fieldName,
): ?string {
$association = $classMetadata->getAssociationMapping($fieldName);

if (\is_array($association)) {
return $association['inversedBy'];
}

if ($association instanceof ORM\Mapping\AssociationMapping) {
return $association->inversedBy;
}

throw new \RuntimeException('This should not happen');
}
}

0 comments on commit 1293591

Please sign in to comment.