From 067fa82af2cc9165423ecf24cdf1336b4998f0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Fr=C3=B6mer?= Date: Sun, 30 Jan 2022 12:20:27 +0100 Subject: [PATCH] Fix for support on different Doctrine ORM versions (#414) * Add conflict to doctrine/persistence below 1.4 and use correct persistence classes * Resolve more issue with static analysis in doctrine implementations. Add doctrine/orm as dev dependency to avoid baseline entries. * Fix phpstan-baseline for php7.4 * Add backwards compatible class_alias for doctrine/persistence * Update psalm baseline --- CHANGELOG.md | 4 +- composer.json | 12 +- phpstan-baseline.neon | 146 +----------------- psalm.baseline.xml | 61 ++------ src/Faker/ORM/Doctrine/EntityPopulator.php | 5 +- src/Faker/ORM/Doctrine/Populator.php | 2 +- .../ORM/Doctrine/backward-compatibility.php | 11 ++ vendor-bin/phpstan/composer.json | 5 +- vendor-bin/psalm/composer.json | 5 +- 9 files changed, 53 insertions(+), 198 deletions(-) create mode 100644 src/Faker/ORM/Doctrine/backward-compatibility.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 399be4ab3b..b0143728c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG ## [Unreleased](https://github.com/FakerPHP/Faker/compare/v1.18.0..main) +- Fixed usage of `Doctrine\Persistence` dependency +- Added conflict with `doctrine/persistence` below version `1.4` ## [2022-01-23, v1.18.0](https://github.com/FakerPHP/Faker/compare/v1.17.0..v1.18.0) @@ -13,7 +15,7 @@ - Person->name was missing string return type (#424) - Generate a valid BE TAX number (#415) - Added the UUID extension to Core (#427) -- + ## [2021-12-05, v1.17.0](https://github.com/FakerPHP/Faker/compare/v1.16.0..v1.17.0) - Partial PHP 8.1 compatibility (#373) diff --git a/composer.json b/composer.json index 8157935b41..3de6aa55ce 100644 --- a/composer.json +++ b/composer.json @@ -21,12 +21,16 @@ "require-dev": { "ext-intl": "*", "bamarni/composer-bin-plugin": "^1.4.1", + "doctrine/persistence": "^1.3 || ^2.0", "symfony/phpunit-bridge": "^4.4 || ^5.2" }, "autoload": { "psr-4": { "Faker\\": "src/Faker/" - } + }, + "files": [ + "src/Faker/ORM/Doctrine/backward-compatibility.php" + ] }, "autoload-dev": { "psr-4": { @@ -41,11 +45,13 @@ "ext-curl": "Required by Faker\\Provider\\Image to download images.", "ext-dom": "Required by Faker\\Provider\\HtmlLorem for generating random HTML.", "ext-iconv": "Required by Faker\\Provider\\ru_RU\\Text::realText() for generating real Russian text.", - "ext-mbstring": "Required for multibyte Unicode string functionality." + "ext-mbstring": "Required for multibyte Unicode string functionality.", + "doctrine/orm": "Required to use Faker\\ORM\\Doctrine" }, "config": { "allow-plugins": { - "bamarni/composer-bin-plugin": true + "bamarni/composer-bin-plugin": true, + "composer/package-versions-deprecated": true }, "sort-packages": true }, diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 5a50df6733..f445f869e6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -66,19 +66,19 @@ parameters: path: src/Faker/ORM/CakePHP/Populator.php - - message: "#^Access to property \\$fieldMappings on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" + message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$fieldMappings\\.$#" count: 2 path: src/Faker/ORM/Doctrine/ColumnTypeGuesser.php - - message: "#^Call to method getTypeOfField\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" + message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$fieldMappings\\.$#" count: 1 - path: src/Faker/ORM/Doctrine/ColumnTypeGuesser.php + path: src/Faker/ORM/Doctrine/EntityPopulator.php - - message: "#^Parameter \\$class of method Faker\\\\ORM\\\\Doctrine\\\\ColumnTypeGuesser\\:\\:guessFormat\\(\\) has invalid typehint type Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/ColumnTypeGuesser.php + message: "#^Access to an undefined property Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:\\$reflFields\\.$#" + count: 2 + path: src/Faker/ORM/Doctrine/EntityPopulator.php - message: "#^Access to constant ONE on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\.$#" @@ -95,33 +95,18 @@ parameters: count: 1 path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - message: "#^Access to property \\$associationMappings on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - message: "#^Access to property \\$associationMappings on an unknown class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata\\.$#" count: 1 path: src/Faker/ORM/Doctrine/EntityPopulator.php - - message: "#^Access to property \\$fieldMappings on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Access to property \\$reflFields on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 2 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method createQueryBuilder\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectRepository\\.$#" + message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\Mapping\\\\ClassMetadata\\:\\:newInstance\\(\\)\\.$#" count: 1 path: src/Faker/ORM/Doctrine/EntityPopulator.php - - message: "#^Call to method getAssociationMappings\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" + message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectRepository\\\\:\\:createQueryBuilder\\(\\)\\.$#" count: 1 path: src/Faker/ORM/Doctrine/EntityPopulator.php @@ -130,61 +115,6 @@ parameters: count: 1 path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - message: "#^Call to method getAssociationNames\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method getAssociationTargetClass\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method getFieldNames\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method getIdentifier\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method getName\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method getRepository\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method hasField\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method isCollectionValuedAssociation\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method isIdentifier\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method newInstance\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method persist\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - message: "#^Class Doctrine\\\\ODM\\\\MongoDB\\\\Mapping\\\\ClassMetadata not found\\.$#" count: 1 @@ -195,66 +125,6 @@ parameters: count: 1 path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - message: "#^Method Faker\\\\ORM\\\\Doctrine\\\\EntityPopulator\\:\\:generateId\\(\\) never returns null so it can be removed from the return typehint\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^PHPDoc tag @var for variable \\$repository contains unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectRepository\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Parameter \\$class of method Faker\\\\ORM\\\\Doctrine\\\\EntityPopulator\\:\\:__construct\\(\\) has invalid typehint type Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Parameter \\$manager of method Faker\\\\ORM\\\\Doctrine\\\\EntityPopulator\\:\\:execute\\(\\) has invalid typehint type Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Parameter \\$manager of method Faker\\\\ORM\\\\Doctrine\\\\EntityPopulator\\:\\:generateId\\(\\) has invalid typehint type Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Property Faker\\\\ORM\\\\Doctrine\\\\EntityPopulator\\:\\:\\$class has unknown class Doctrine\\\\Common\\\\Persistence\\\\Mapping\\\\ClassMetadata as its type\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/EntityPopulator.php - - - - message: "#^Call to method flush\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 2 - path: src/Faker/ORM/Doctrine/Populator.php - - - - message: "#^Call to method flush\\(\\) on an unknown class Faker\\\\ORM\\\\Doctrine\\\\EntityManager\\.$#" - count: 2 - path: src/Faker/ORM/Doctrine/Populator.php - - - - message: "#^Call to method getClassMetadata\\(\\) on an unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/Populator.php - - - - message: "#^Parameter \\$entityManager of method Faker\\\\ORM\\\\Doctrine\\\\Populator\\:\\:execute\\(\\) has invalid typehint type Faker\\\\ORM\\\\Doctrine\\\\EntityManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/Populator.php - - - - message: "#^Parameter \\$manager of method Faker\\\\ORM\\\\Doctrine\\\\Populator\\:\\:__construct\\(\\) has invalid typehint type Doctrine\\\\Common\\\\Persistence\\\\ObjectManager\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/Populator.php - - - - message: "#^Property Faker\\\\ORM\\\\Doctrine\\\\Populator\\:\\:\\$manager has unknown class Doctrine\\\\Common\\\\Persistence\\\\ObjectManager as its type\\.$#" - count: 1 - path: src/Faker/ORM/Doctrine/Populator.php - - message: "#^Call to method create\\(\\) on an unknown class Mandango\\\\Mandango\\.$#" count: 1 diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 3fd3b88614..7d5b26835d 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -19,49 +19,26 @@ self self - + TableRegistry - - - ClassMetadata - - - - $this->class - $this->class + $this->class - $this->class->associationMappings - $this->class->fieldMappings - ClassMetadata - ObjectManager - ObjectManager \Doctrine\ODM\MongoDB\Mapping\ClassMetadata \Doctrine\ODM\MongoDB\Mapping\ClassMetadata \Doctrine\ODM\MongoDB\Mapping\ClassMetadata \Doctrine\ORM\Mapping\ClassMetadata \Doctrine\ORM\Mapping\ClassMetadata - - $this->class - $this->class - $this->class - $this->class - $this->class - $this->class->reflFields - ClassMetadata - - - - - - $this->manager - ObjectManager|null - + + createQueryBuilder + getAssociationMappings + newInstance + @@ -76,7 +53,9 @@ - + + \ColumnMap + @@ -88,6 +67,7 @@ $columnMap $columnMap $columnMap + \ColumnMap @@ -152,6 +132,7 @@ $this->locator + Locator Locator @@ -191,34 +172,16 @@ $checksumArr[$checksum % 11] - - - - Icelandic - - - - static::$middleName - $ref[$i] - static::split($text) - - - implode('', $result) - - - 14 - - DateTime diff --git a/src/Faker/ORM/Doctrine/EntityPopulator.php b/src/Faker/ORM/Doctrine/EntityPopulator.php index 145f09655c..8bd8bede66 100644 --- a/src/Faker/ORM/Doctrine/EntityPopulator.php +++ b/src/Faker/ORM/Doctrine/EntityPopulator.php @@ -226,11 +226,10 @@ private function callMethods($obj, $insertedEntities) } /** - * @return int|null + * @return int */ private function generateId($obj, $column, ObjectManager $manager) { - /** @var \Doctrine\Common\Persistence\ObjectRepository $repository */ $repository = $manager->getRepository(get_class($obj)); $result = $repository->createQueryBuilder('e') ->select(sprintf('e.%s', $column)) @@ -238,8 +237,6 @@ private function generateId($obj, $column, ObjectManager $manager) ->execute(); $ids = array_map('current', $result->toArray()); - $id = null; - do { $id = mt_rand(); } while (in_array($id, $ids, false)); diff --git a/src/Faker/ORM/Doctrine/Populator.php b/src/Faker/ORM/Doctrine/Populator.php index d75400feb1..a1284edac3 100644 --- a/src/Faker/ORM/Doctrine/Populator.php +++ b/src/Faker/ORM/Doctrine/Populator.php @@ -86,7 +86,7 @@ public function addEntity($entity, $number, $customColumnFormatters = [], $custo * Please note that large amounts of data will result in more memory usage since the the Populator will return * all newly created primary keys after executing. * - * @param EntityManager|null $entityManager A Doctrine connection object + * @param ObjectManager|null $entityManager A Doctrine connection object * * @return array A list of the inserted PKs */ diff --git a/src/Faker/ORM/Doctrine/backward-compatibility.php b/src/Faker/ORM/Doctrine/backward-compatibility.php new file mode 100644 index 0000000000..6f545f87b8 --- /dev/null +++ b/src/Faker/ORM/Doctrine/backward-compatibility.php @@ -0,0 +1,11 @@ +