From 1293591f89d7476e954c52d460ef19bb1531dc6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 7 Feb 2024 17:52:19 +0100 Subject: [PATCH] Fix: Resolve inversedBy from array or Doctrine\ORM\Mapping\AssociationMapping --- phpstan-baseline.neon | 12 +++++++++++- psalm-baseline.xml | 9 +++++++++ src/FixtureFactory.php | 32 +++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 313e3f6a..1b281158 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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\\, 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\\, 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 @@ -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\\, class\\-string\\ given\\.$#" count: 1 @@ -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 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 69680892..06af0e4a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -87,6 +87,12 @@ $fieldValue + + ?string + + + \is_array($association) + createOne( @@ -95,6 +101,9 @@ ); }]]> + + ORM\Mapping\AssociationMapping + EntityDefinition EntityDefinitionProvider diff --git a/src/FixtureFactory.php b/src/FixtureFactory.php index cd8f0410..a5c69b57 100644 --- a/src/FixtureFactory.php +++ b/src/FixtureFactory.php @@ -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( @@ -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'); + } }