Skip to content

Commit

Permalink
Fix for support on different Doctrine ORM versions (#414)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
icanhazstring committed Jan 30, 2022
1 parent fbc273b commit 067fa82
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 198 deletions.
4 changes: 3 additions & 1 deletion 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)

Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions composer.json
Expand Up @@ -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": {
Expand All @@ -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
},
Expand Down
146 changes: 8 additions & 138 deletions phpstan-baseline.neon
Expand Up @@ -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\\.$#"
Expand All @@ -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\\<object\\>\\:\\:createQueryBuilder\\(\\)\\.$#"
count: 1
path: src/Faker/ORM/Doctrine/EntityPopulator.php

Expand All @@ -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
Expand All @@ -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
Expand Down
61 changes: 12 additions & 49 deletions psalm.baseline.xml
Expand Up @@ -19,49 +19,26 @@
<code>self</code>
<code>self</code>
</InvalidReturnType>
</file>
</file>
<file src="src/Faker/ORM/CakePHP/EntityPopulator.php">
<UndefinedClass occurrences="1">
<code>TableRegistry</code>
</UndefinedClass>
</file>
<file src="src/Faker/ORM/Doctrine/ColumnTypeGuesser.php">
<UndefinedClass occurrences="1">
<code>ClassMetadata</code>
</UndefinedClass>
</file>
<file src="src/Faker/ORM/Doctrine/EntityPopulator.php">
<UndefinedClass occurrences="13">
<code>$this-&gt;class</code>
<code>$this-&gt;class</code>
<UndefinedClass occurrences="6">
<code>$this-&gt;class</code>
<code>$this-&gt;class-&gt;associationMappings</code>
<code>$this-&gt;class-&gt;fieldMappings</code>
<code>ClassMetadata</code>
<code>ObjectManager</code>
<code>ObjectManager</code>
<code>\Doctrine\ODM\MongoDB\Mapping\ClassMetadata</code>
<code>\Doctrine\ODM\MongoDB\Mapping\ClassMetadata</code>
<code>\Doctrine\ODM\MongoDB\Mapping\ClassMetadata</code>
<code>\Doctrine\ORM\Mapping\ClassMetadata</code>
<code>\Doctrine\ORM\Mapping\ClassMetadata</code>
</UndefinedClass>
<UndefinedDocblockClass occurrences="7">
<code>$this-&gt;class</code>
<code>$this-&gt;class</code>
<code>$this-&gt;class</code>
<code>$this-&gt;class</code>
<code>$this-&gt;class</code>
<code>$this-&gt;class-&gt;reflFields</code>
<code>ClassMetadata</code>
</UndefinedDocblockClass>
</file>
<file src="src/Faker/ORM/Doctrine/Populator.php">
<UndefinedClass occurrences="1"/>
<UndefinedDocblockClass occurrences="3">
<code>$this-&gt;manager</code>
<code>ObjectManager|null</code>
</UndefinedDocblockClass>
<UndefinedInterfaceMethod occurrences="3">
<code>createQueryBuilder</code>
<code>getAssociationMappings</code>
<code>newInstance</code>
</UndefinedInterfaceMethod>
</file>
<file src="src/Faker/ORM/Mandango/EntityPopulator.php">
<UndefinedClass occurrences="2">
Expand All @@ -76,7 +53,9 @@
</UndefinedClass>
</file>
<file src="src/Faker/ORM/Propel/ColumnTypeGuesser.php">
<UndefinedClass occurrences="1"/>
<UndefinedClass occurrences="1">
<code>\ColumnMap</code>
</UndefinedClass>
</file>
<file src="src/Faker/ORM/Propel/EntityPopulator.php">
<UndefinedClass occurrences="9">
Expand All @@ -88,6 +67,7 @@
<code>$columnMap</code>
<code>$columnMap</code>
<code>$columnMap</code>
<code>\ColumnMap</code>
</UndefinedClass>
</file>
<file src="src/Faker/ORM/Propel/Populator.php">
Expand Down Expand Up @@ -152,6 +132,7 @@
<file src="src/Faker/ORM/Spot/Populator.php">
<UndefinedClass occurrences="2">
<code>$this-&gt;locator</code>
<code>Locator</code>
</UndefinedClass>
<UndefinedDocblockClass occurrences="1">
<code>Locator</code>
Expand Down Expand Up @@ -191,34 +172,16 @@
<code>$checksumArr[$checksum % 11]</code>
</InvalidArrayOffset>
</file>
<file src="src/Faker/Provider/is_IS/Address.php">
<InvalidPropertyAssignmentValue occurrences="1"/>
<UndefinedDocblockClass occurrences="2">
<code>Icelandic</code>
</UndefinedDocblockClass>
</file>
<file src="src/Faker/Provider/is_IS/Person.php">
<InvalidArgument occurrences="1">
<code>static::$middleName</code>
</InvalidArgument>
<InvalidArrayOffset occurrences="1">
<code>$ref[$i]</code>
</InvalidArrayOffset>
<InvalidPropertyAssignmentValue occurrences="3"/>
</file>
<file src="src/Faker/Provider/ja_JP/Text.php">
<UndefinedMethod occurrences="1">
<code>static::split($text)</code>
</UndefinedMethod>
</file>
<file src="src/Faker/Provider/pl_PL/Company.php">
<InvalidReturnStatement occurrences="1">
<code>implode('', $result)</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="1">
<code>14</code>
</InvalidReturnType>
</file>
<file src="src/Faker/Provider/pl_PL/Person.php">
<UndefinedDocblockClass occurrences="1">
<code>DateTime</code>
Expand Down
5 changes: 1 addition & 4 deletions src/Faker/ORM/Doctrine/EntityPopulator.php
Expand Up @@ -226,20 +226,17 @@ 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))
->getQuery()
->execute();
$ids = array_map('current', $result->toArray());

$id = null;

do {
$id = mt_rand();
} while (in_array($id, $ids, false));
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/ORM/Doctrine/Populator.php
Expand Up @@ -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
*/
Expand Down

0 comments on commit 067fa82

Please sign in to comment.