Skip to content

Commit

Permalink
Merge pull request #355 from nicwortel/php8
Browse files Browse the repository at this point in the history
Upgrade the Doctrine Coding Standard
  • Loading branch information
greg0ire authored Dec 10, 2020
2 parents 5240c66 + 2e82157 commit ffb1e86
Show file tree
Hide file tree
Showing 44 changed files with 326 additions and 223 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"require-dev": {
"ext-sqlite3": "*",
"doctrine/coding-standard": "^6.0",
"doctrine/coding-standard": "^8.2",
"doctrine/dbal": "^2.5.4",
"doctrine/mongodb-odm": "^1.3.0 || ^2.0.0",
"doctrine/orm": "^2.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface DependentFixtureInterface
* This method must return an array of fixtures classes
* on which the implementing class depends on
*
* @return class-string[]
* @psalm-return array<class-string<FixtureInterface>>
*/
public function getDependencies();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\Common\DataFixtures\SharedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Exception;

use function get_class;
use function interface_exists;
use function sprintf;
Expand Down Expand Up @@ -106,12 +107,15 @@ public function load(ObjectManager $manager, FixtureInterface $fixture)
if ($fixture instanceof OrderedFixtureInterface) {
$prefix = sprintf('[%d] ', $fixture->getOrder());
}

$this->log('loading ' . $prefix . get_class($fixture));
}

// additionally pass the instance of reference repository to shared fixtures
if ($fixture instanceof SharedFixtureInterface) {
$fixture->setReferenceRepository($this->referenceRepository);
}

$fixture->load($manager);
$manager->clear();
}
Expand All @@ -126,9 +130,11 @@ public function purge()
if ($this->purger === null) {
throw new Exception('Doctrine\Common\DataFixtures\Purger\PurgerInterface instance is required if you want to purge the database before loading your data fixtures.');
}

if ($this->logger) {
$this->log('purging database');
}

$this->purger->purge();
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/Common/DataFixtures/Executor/MongoDBExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(DocumentManager $dm, ?MongoDBPurger $purger = null)
$this->purger = $purger;
$this->purger->setDocumentManager($dm);
}

parent::__construct($dm);
$this->listener = new MongoDBReferenceListener($this->referenceRepository);
$dm->getEventManager()->addEventSubscriber($this->listener);
Expand Down Expand Up @@ -60,6 +61,7 @@ public function execute(array $fixtures, $append = false)
if ($append === false) {
$this->purge();
}

foreach ($fixtures as $fixture) {
$this->load($this->dm, $fixture);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function __construct(EntityManagerInterface $em, ?ORMPurger $purger = nul
$this->purger = $purger;
$this->purger->setEntityManager($em);
}

parent::__construct($em);
$this->listener = new ORMReferenceListener($this->referenceRepository);
$em->getEventManager()->addEventSubscriber($this->listener);
Expand Down Expand Up @@ -65,6 +66,7 @@ public function execute(array $fixtures, $append = false)
if ($append === false) {
$executor->purge();
}

foreach ($fixtures as $fixture) {
$executor->load($em, $fixture);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/Common/DataFixtures/Executor/PHPCRExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\DataFixtures\Purger\PHPCRPurger;
use Doctrine\ODM\PHPCR\DocumentManagerInterface;

use function method_exists;

/**
Expand Down Expand Up @@ -33,6 +34,9 @@ public function __construct(DocumentManagerInterface $dm, ?PHPCRPurger $purger =
$this->setPurger($purger);
}

/**
* @return DocumentManagerInterface
*/
public function getObjectManager()
{
return $this->dm;
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Common/DataFixtures/FixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Common\DataFixtures;

use Doctrine\Persistence\ObjectManager;

use function interface_exists;

/**
Expand Down
34 changes: 22 additions & 12 deletions lib/Doctrine/Common/DataFixtures/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ReflectionClass;
use RuntimeException;
use SplFileInfo;

use function array_keys;
use function array_merge;
use function asort;
Expand All @@ -39,14 +40,14 @@ class Loader
/**
* Array of fixture object instances to execute.
*
* @var array
* @psalm-var array<class-string<FixtureInterface>, FixtureInterface>
*/
private $fixtures = [];

/**
* Array of ordered fixture object instances.
*
* @var array
* @psalm-var array<class-string<OrderedFixtureInterface>, OrderedFixtureInterface>
*/
private $orderedFixtures = [];

Expand Down Expand Up @@ -206,6 +207,8 @@ public function getFixtures()
* class.
*
* @return bool
*
* @psalm-param class-string<object> $className
*/
public function isTransient($className)
{
Expand Down Expand Up @@ -234,14 +237,12 @@ protected function createFixture($class)
/**
* Orders fixtures by number
*
* @return void
*
* @todo maybe there is a better way to handle reordering
*/
private function orderFixturesByNumber()
private function orderFixturesByNumber(): void
{
$this->orderedFixtures = $this->fixtures;
usort($this->orderedFixtures, static function ($a, $b) {
usort($this->orderedFixtures, static function (FixtureInterface $a, FixtureInterface $b): int {
if ($a instanceof OrderedFixtureInterface && $b instanceof OrderedFixtureInterface) {
if ($a->getOrder() === $b->getOrder()) {
return 0;
Expand Down Expand Up @@ -269,6 +270,7 @@ private function orderFixturesByNumber()
*/
private function orderFixturesByDependencies()
{
/** @psalm-var array<class-string<DependentFixtureInterface>, int> */
$sequenceForClasses = [];

// If fixtures were already ordered by number then we need
Expand Down Expand Up @@ -362,7 +364,10 @@ private function orderFixturesByDependencies()
$this->orderedFixtures = array_merge($this->orderedFixtures, $orderedFixtures);
}

private function validateDependencies($dependenciesClasses)
/**
* @psalm-param iterable<class-string> $dependenciesClasses
*/
private function validateDependencies(iterable $dependenciesClasses): bool
{
$loadedFixtureClasses = array_keys($this->fixtures);

Expand All @@ -375,7 +380,12 @@ private function validateDependencies($dependenciesClasses)
return true;
}

private function getUnsequencedClasses($sequences, $classes = null)
/**
* @psalm-param array<class-string<DependentFixtureInterface>, int> $sequences
* @psalm-param iterable<class-string<FixtureInterface>>|null $classes
* @psalm-return array<class-string<FixtureInterface>>
*/
private function getUnsequencedClasses(array $sequences, ?iterable $classes = null): array
{
$unsequencedClasses = [];

Expand All @@ -397,11 +407,11 @@ private function getUnsequencedClasses($sequences, $classes = null)
/**
* Load fixtures from files contained in iterator.
*
* @param Iterator $iterator Iterator over files from which fixtures should be loaded.
*
* @return array $fixtures Array of loaded fixture object instances.
* @psalm-param Iterator<SplFileInfo> $iterator Iterator over files from
* which fixtures should be loaded.
* @psalm-return list<FixtureInterface> $fixtures Array of loaded fixture object instances.
*/
private function loadFromIterator(Iterator $iterator)
private function loadFromIterator(Iterator $iterator): array
{
$includedFiles = [];
foreach ($iterator as $file) {
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Common/DataFixtures/Purger/MongoDBPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function purge()

$this->dm->getDocumentCollection($metadata->name)->drop();
}

$this->dm->getSchemaManager()->ensureIndexes();
}
}
26 changes: 14 additions & 12 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;

use function array_reverse;
use function array_search;
use function assert;
use function count;
use function is_callable;
use function method_exists;
Expand Down Expand Up @@ -118,7 +120,8 @@ public function purge()
for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
$class = $commitOrder[$i];

if ((isset($class->isEmbeddedClass) && $class->isEmbeddedClass) ||
if (
(isset($class->isEmbeddedClass) && $class->isEmbeddedClass) ||
$class->isMappedSuperclass ||
($class->isInheritanceTypeSingleTable() && $class->name !== $class->rootEntityName)
) {
Expand Down Expand Up @@ -189,8 +192,8 @@ private function getCommitOrder(EntityManagerInterface $em, array $classes)
continue;
}

/** @var ClassMetadata $targetClass */
$targetClass = $em->getClassMetadata($assoc['targetEntity']);
$targetClass = $em->getClassMetadata($assoc['targetEntity']);
assert($targetClass instanceof ClassMetadata);
$targetClassName = $targetClass->getName();

if (! $sorter->hasNode($targetClassName)) {
Expand Down Expand Up @@ -239,7 +242,7 @@ private function getAssociationTables(array $classes, AbstractPlatform $platform
return $associationTables;
}

private function getTableName(ClassMetadata $class, AbstractPlatform $platform) : string
private function getTableName(ClassMetadata $class, AbstractPlatform $platform): string
{
if (isset($class->table['schema']) && ! method_exists($class, 'getSchemaName')) {
return $class->table['schema'] . '.' . $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
Expand All @@ -249,22 +252,21 @@ private function getTableName(ClassMetadata $class, AbstractPlatform $platform)
}

/**
* @param array $association
* @param ClassMetadata $class
* @param AbstractPlatform $platform
*
* @return string
* @param mixed[] $assoc
*/
private function getJoinTableName($assoc, $class, $platform)
{
private function getJoinTableName(
array $assoc,
ClassMetadata $class,
AbstractPlatform $platform
): string {
if (isset($assoc['joinTable']['schema']) && ! method_exists($class, 'getSchemaName')) {
return $assoc['joinTable']['schema'] . '.' . $this->em->getConfiguration()->getQuoteStrategy()->getJoinTableName($assoc, $class, $platform);
}

return $this->em->getConfiguration()->getQuoteStrategy()->getJoinTableName($assoc, $class, $platform);
}

private function getDeleteFromTableSQL(string $tableName, AbstractPlatform $platform) : string
private function getDeleteFromTableSQL(string $tableName, AbstractPlatform $platform): string
{
$tableIdentifier = new Identifier($tableName);

Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/Common/DataFixtures/Purger/PHPCRPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class PHPCRPurger implements PurgerInterface
{
/** @var DocumentManagerInterface */
/** @var DocumentManagerInterface|null */
private $dm;

public function __construct(?DocumentManagerInterface $dm = null)
Expand All @@ -26,6 +26,9 @@ public function setDocumentManager(DocumentManager $dm)
$this->dm = $dm;
}

/**
* @return DocumentManagerInterface|null
*/
public function getObjectManager()
{
return $this->dm;
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/Common/DataFixtures/ReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\ODM\PHPCR\DocumentManager as PhpcrDocumentManager;
use Doctrine\Persistence\ObjectManager;
use OutOfBoundsException;

use function array_key_exists;
use function array_keys;
use function get_class;
Expand Down Expand Up @@ -198,6 +199,8 @@ public function getReferenceNames($reference)
* Checks if reference has identity stored
*
* @param string $name
*
* @return bool
*/
public function hasIdentity($name)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/Common/DataFixtures/Sorter/TopologicalSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\DataFixtures\Exception\CircularReferenceException;
use Doctrine\ORM\Mapping\ClassMetadata;
use RuntimeException;

use function get_class;
use function sprintf;

Expand Down Expand Up @@ -166,6 +167,7 @@ private function visit(Vertex $definition)
)
);
}

break;
case Vertex::NOT_VISITED:
$this->visit($childDefinition);
Expand Down
17 changes: 12 additions & 5 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@

<rule ref="Doctrine">
<!-- Traversable type hints often end up as mixed[], so we skip them for now -->
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>

<!-- Will cause BC breaks to method signatures - disabled for now -->
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
</rule>

<!-- Property type hints are only available in PHP 7.4+ -->
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<properties>
<property name="enableNativeTypeHint" value="false"/>
</properties>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
Expand Down
Loading

0 comments on commit ffb1e86

Please sign in to comment.