Skip to content

Commit

Permalink
Add return types
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 15, 2023
1 parent a77f4d3 commit b9fe5cd
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 98 deletions.
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ awareness about deprecated code.
- Use of our low-overhead runtime deprecation API, details:
https://github.com/doctrine/deprecations/

# Upgrade to 2.0

## BC BREAK: Add return types to all the methods

All return types defined in phpdoc `@return` are now defined in the method signature,
they must be added to your code if you extend the classes or implement the interfaces.

# Upgrade to 1.8

Executor and Purger classes are final, they cannot be extended.
Expand Down
8 changes: 4 additions & 4 deletions src/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function getReferenceRepository(): ReferenceRepository
*
* @return void

Check failure on line 50 in src/AbstractFixture.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Common\DataFixtures\AbstractFixture::setReference() has useless @return annotation.
*/
public function setReference(string $name, object $object)
public function setReference(string $name, object $object): void
{
$this->getReferenceRepository()->setReference($name, $object);
}
Expand All @@ -68,7 +68,7 @@ public function setReference(string $name, object $object)
*
* @throws BadMethodCallException - if repository already has a reference by $name.
*/
public function addReference(string $name, object $object)
public function addReference(string $name, object $object): void
{
$this->getReferenceRepository()->addReference($name, $object);
}
Expand All @@ -86,7 +86,7 @@ public function addReference(string $name, object $object)
*
* @template T of object
*/
public function getReference(string $name, ?string $class = null)
public function getReference(string $name, ?string $class = null): object
{
if ($class === null) {
Deprecation::trigger(
Expand All @@ -110,7 +110,7 @@ public function getReference(string $name, ?string $class = null)
*
* @return bool
*/
public function hasReference(string $name, ?string $class = null)
public function hasReference(string $name, ?string $class = null): bool
{
if ($class === null) {
Deprecation::trigger(
Expand Down
32 changes: 9 additions & 23 deletions src/Executor/AbstractExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,60 +48,50 @@ public function __construct(ObjectManager $manager)
$this->referenceRepository = new ReferenceRepository($manager);
}

/** @return ReferenceRepository */
public function getReferenceRepository()
public function getReferenceRepository(): ReferenceRepository
{
return $this->referenceRepository;
}

public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void
{
$this->referenceRepository = $referenceRepository;
}

/**
* Sets the Purger instance to use for this executor instance.
*
* @return void
*/
public function setPurger(PurgerInterface $purger)
public function setPurger(PurgerInterface $purger): void
{
$this->purger = $purger;
}

/** @return PurgerInterface */
public function getPurger()
public function getPurger(): PurgerInterface
{
return $this->purger;
}

/**
* Set the logger callable to execute with the log() method.
*
* @return void
*/
public function setLogger(callable $logger)
public function setLogger(callable $logger): void
{
$this->logger = $logger;
}

/**
* Logs a message using the logger.
*
* @return void
*/
public function log(string $message)
public function log(string $message): void
{
$logger = $this->logger;
$logger($message);
}

/**
* Load a fixture with the given persistence manager.
*
* @return void
*/
public function load(ObjectManager $manager, FixtureInterface $fixture)
public function load(ObjectManager $manager, FixtureInterface $fixture): void
{
if ($this->logger) {
$prefix = '';
Expand All @@ -124,11 +114,9 @@ public function load(ObjectManager $manager, FixtureInterface $fixture)
/**
* Purges the database before loading.
*
* @return void
*
* @throws Exception if the purger is not defined.
*/
public function purge()
public function purge(): void
{
if ($this->purger === null) {
throw new Exception(
Expand All @@ -149,8 +137,6 @@ public function purge()
*
* @param FixtureInterface[] $fixtures Array of fixtures to execute.
* @param bool $append Whether to append the data fixtures or purge the database before loading.
*
* @return void
*/
abstract public function execute(array $fixtures, bool $append = false);
abstract public function execute(array $fixtures, bool $append = false): void;
}
8 changes: 3 additions & 5 deletions src/Executor/MongoDBExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ public function __construct(DocumentManager $dm, ?MongoDBPurger $purger = null)

/**
* Retrieve the DocumentManager instance this executor instance is using.
*
* @return DocumentManager
*/
public function getObjectManager()
public function getObjectManager(): DocumentManager
{
return $this->dm;
}

/** @inheritDoc */

Check failure on line 49 in src/Executor/MongoDBExecutor.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Useless documentation comment with @inheritdoc.
public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void
{
$this->dm->getEventManager()->removeEventListener(
$this->listener->getSubscribedEvents(),
Expand All @@ -62,7 +60,7 @@ public function setReferenceRepository(ReferenceRepository $referenceRepository)
}

/** @inheritDoc */
public function execute(array $fixtures, bool $append = false)
public function execute(array $fixtures, bool $append = false): void
{
if ($append === false) {
$this->purge();
Expand Down
2 changes: 1 addition & 1 deletion src/Executor/ORMExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ORMExecutor extends AbstractExecutor
use ORMExecutorCommon;

/** @inheritDoc */
public function execute(array $fixtures, bool $append = false)
public function execute(array $fixtures, bool $append = false): void
{
$executor = $this;
$this->em->wrapInTransaction(static function (EntityManagerInterface $em) use ($executor, $fixtures, $append) {
Expand Down
6 changes: 2 additions & 4 deletions src/Executor/ORMExecutorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ public function __construct(EntityManagerInterface $em, ?ORMPurgerInterface $pur

/**
* Retrieve the EntityManagerInterface instance this executor instance is using.
*
* @return EntityManagerInterface
*/
public function getObjectManager()
public function getObjectManager(): EntityManagerInterface
{
return $this->originalManager;
}

/** @inheritDoc */

Check failure on line 52 in src/Executor/ORMExecutorCommon.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Useless documentation comment with @inheritdoc.
public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void
{
$this->em->getEventManager()->removeEventListener(
$this->listener->getSubscribedEvents(),
Expand Down
5 changes: 2 additions & 3 deletions src/Executor/PHPCRExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ public function __construct(DocumentManagerInterface $dm, ?PHPCRPurger $purger =
$this->setPurger($purger);
}

/** @return DocumentManagerInterface */
public function getObjectManager()
public function getObjectManager(): DocumentManagerInterface

Check failure on line 38 in src/Executor/PHPCRExecutor.php

View workflow job for this annotation

GitHub Actions / Static Analysis / Psalm (8.2)

UndefinedClass

src/Executor/PHPCRExecutor.php:38:41: UndefinedClass: Class, interface or enum named Doctrine\ODM\PHPCR\DocumentManagerInterface does not exist (see https://psalm.dev/019)
{
return $this->dm;
}

/** @inheritDoc */
public function execute(array $fixtures, bool $append = false)
public function execute(array $fixtures, bool $append = false): void
{
$that = $this;

Expand Down
14 changes: 7 additions & 7 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Loader
*
* @return array $fixtures Array of loaded fixture object instances.
*/
public function loadFromDirectory(string $dir)
public function loadFromDirectory(string $dir): array
{
if (! is_dir($dir)) {
throw new InvalidArgumentException(sprintf('"%s" does not exist', $dir));
Expand All @@ -94,7 +94,7 @@ public function loadFromDirectory(string $dir)
*
* @return array $fixtures Array of loaded fixture object instances.
*/
public function loadFromFile(string $fileName)
public function loadFromFile(string $fileName): array
{
if (! is_readable($fileName)) {
throw new InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName));
Expand All @@ -110,7 +110,7 @@ public function loadFromFile(string $fileName)
*
* @return bool
*/
public function hasFixture(FixtureInterface $fixture)
public function hasFixture(FixtureInterface $fixture): bool
{
return isset($this->fixtures[get_class($fixture)]);
}
Expand All @@ -120,7 +120,7 @@ public function hasFixture(FixtureInterface $fixture)
*
* @return FixtureInterface
*/
public function getFixture(string $className)
public function getFixture(string $className): FixtureInterface
{
if (! isset($this->fixtures[$className])) {
throw new InvalidArgumentException(sprintf(
Expand Down Expand Up @@ -200,7 +200,7 @@ public function getFixtures()
*
* @return bool
*/
public function isTransient(string $className)
public function isTransient(string $className): bool
{
$rc = new ReflectionClass($className);
if ($rc->isAbstract()) {
Expand All @@ -217,7 +217,7 @@ public function isTransient(string $className)
*
* @return FixtureInterface
*/
protected function createFixture(string $class)
protected function createFixture(string $class): FixtureInterface
{
return new $class();
}
Expand Down Expand Up @@ -256,7 +256,7 @@ private function orderFixturesByNumber(): void
*
* @return void
*/
private function orderFixturesByDependencies()
private function orderFixturesByDependencies(): void
{
/** @psalm-var array<class-string<DependentFixtureInterface>, int> */
$sequenceForClasses = [];
Expand Down
2 changes: 1 addition & 1 deletion src/OrderedFixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface OrderedFixtureInterface
*
* @return int

Check failure on line 17 in src/OrderedFixtureInterface.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Common\DataFixtures\OrderedFixtureInterface::getOrder() has useless @return annotation.
*/
public function getOrder();
public function getOrder(): int;
}
8 changes: 4 additions & 4 deletions src/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ProxyReferenceRepository extends ReferenceRepository
*
* @return string

Check failure on line 25 in src/ProxyReferenceRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Common\DataFixtures\ProxyReferenceRepository::serialize() has useless @return annotation.
*/
public function serialize()
public function serialize(): string
{
$unitOfWork = $this->getManager()->getUnitOfWork();
$simpleReferences = [];
Expand All @@ -49,7 +49,7 @@ public function serialize()
*
* @return void

Check failure on line 50 in src/ProxyReferenceRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Common\DataFixtures\ProxyReferenceRepository::unserialize() has useless @return annotation.
*/
public function unserialize(string $serializedData)
public function unserialize(string $serializedData): void
{
$repositoryData = unserialize($serializedData);

Expand Down Expand Up @@ -98,7 +98,7 @@ public function unserialize(string $serializedData)
*
* @return bool

Check failure on line 99 in src/ProxyReferenceRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Common\DataFixtures\ProxyReferenceRepository::load() has useless @return annotation.
*/
public function load(string $baseCacheName)
public function load(string $baseCacheName): bool
{
$filename = $baseCacheName . '.ser';

Expand All @@ -124,7 +124,7 @@ public function load(string $baseCacheName)
*
* @return void

Check failure on line 125 in src/ProxyReferenceRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Common\DataFixtures\ProxyReferenceRepository::save() has useless @return annotation.
*/
public function save(string $baseCacheName)
public function save(string $baseCacheName): void
{
$serializedData = $this->serialize();

Expand Down
10 changes: 3 additions & 7 deletions src/Purger/MongoDBPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,22 @@ public function __construct(?DocumentManager $dm = null)

/**
* Set the DocumentManager instance this purger instance should use.
*
* @return void
*/
public function setDocumentManager(DocumentManager $dm)
public function setDocumentManager(DocumentManager $dm): void
{
$this->dm = $dm;
}

/**
* Retrieve the DocumentManager instance this purger instance is using.
*
* @return DocumentManager
*/
public function getObjectManager()
public function getObjectManager(): DocumentManager
{
return $this->dm;
}

/** @inheritDoc */

Check failure on line 44 in src/Purger/MongoDBPurger.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Useless documentation comment with @inheritdoc.
public function purge()
public function purge(): void
{
$metadatas = $this->dm->getMetadataFactory()->getAllMetadata();
foreach ($metadatas as $metadata) {
Expand Down
18 changes: 5 additions & 13 deletions src/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,36 @@ public function __construct(?EntityManagerInterface $em = null, array $excluded

/**
* Set the purge mode
*
* @return void
*/
public function setPurgeMode(int $mode)
public function setPurgeMode(int $mode): void
{
$this->purgeMode = $mode;
$this->cachedSqlStatements = null;
}

/**
* Get the purge mode
*
* @return int
*/
public function getPurgeMode()
public function getPurgeMode(): int
{
return $this->purgeMode;
}

/** @inheritDoc */
public function setEntityManager(EntityManagerInterface $em)
public function setEntityManager(EntityManagerInterface $em): void
{
$this->em = $em;
$this->cachedSqlStatements = null;
}

/**
* Retrieve the EntityManagerInterface instance this purger instance is using.
*
* @return EntityManagerInterface
*/
public function getObjectManager()
public function getObjectManager(): EntityManagerInterface
{
return $this->em;
}

/** @inheritDoc */
public function purge()
public function purge(): void
{
$connection = $this->em->getConnection();
array_map([$connection, 'executeStatement'], $this->getPurgeStatements());
Expand Down
Loading

0 comments on commit b9fe5cd

Please sign in to comment.