From fab5afc21788ae09028cebcfe12c50f1dfa174ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 7 Feb 2024 17:04:06 +0100 Subject: [PATCH 1/3] Enhancement: Allow installation of doctrine/orm:^3.0.0 --- CHANGELOG.md | 2 ++ composer.json | 2 +- composer.lock | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ebebbce..7cca9a8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ For a full diff see [`1.6.0...main`][1.6.0...main]. ### Changed +- Allowed installation of `doctrine/orm:^3.0.0` ([#1293]), by [@localheinz] - Required `doctrine/orm:^2.14.0` ([#1298]), by [@localheinz] ## [`1.6.0`][1.6.0] @@ -359,6 +360,7 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0]. [#1213]: https://github.com/ergebnis/factory-bot/pull/1213 [#1233]: https://github.com/ergebnis/factory-bot/pull/1233 [#1234]: https://github.com/ergebnis/factory-bot/pull/1234 +[#1293]: https://github.com/ergebnis/factory-bot/pull/1293 [#1298]: https://github.com/ergebnis/factory-bot/pull/1298 [@abenerd]: https://github.com/abenerd diff --git a/composer.json b/composer.json index ab2b6212..d13a4adb 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "php": "~8.1.0 || ~8.2.0 || ~8.3.0", "doctrine/collections": "^1.6.5 || ^2.0.0", "doctrine/dbal": "^2.12.0 || ^3.0.0", - "doctrine/orm": "^2.14.0", + "doctrine/orm": "^2.14.0 || ^3.0.0", "doctrine/persistence": "^2.1.0 || ^3.0.0", "ergebnis/classy": "^1.6.0", "fakerphp/faker": "^1.20.0" diff --git a/composer.lock b/composer.lock index 4ed605b2..afe2d244 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1d005d9482abe8ef9293f9686bd2cb49", + "content-hash": "0bb1f4a6429a3779eb985af0fcb9efa7", "packages": [ { "name": "doctrine/cache", From 011d3c9e6b6610d0decd21e2397bfd8ce915cf78 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 2/3] 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'); + } } From 2f52314b2c50d2d0fe2fe626596727e968692ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 7 Feb 2024 18:00:20 +0100 Subject: [PATCH 3/3] Fix: Add Doctrine\ORM\Mapping\AssociationMapping to whitelist --- composer-require-checker.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer-require-checker.json b/composer-require-checker.json index b8b091ad..e9c6cb8b 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -1,3 +1,5 @@ { - "symbol-whitelist": [] + "symbol-whitelist": [ + "Doctrine\\ORM\\Mapping\\AssociationMapping" + ] }