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 937bc46
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 154 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
15 changes: 4 additions & 11 deletions src/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ private function getReferenceRepository(): ReferenceRepository
* @see Doctrine\Common\DataFixtures\ReferenceRepository::setReference
*
* @param object $object - managed object
*
* @return void
*/
public function setReference(string $name, object $object)
public function setReference(string $name, object $object): void
{
$this->getReferenceRepository()->setReference($name, $object);
}
Expand All @@ -64,11 +62,9 @@ public function setReference(string $name, object $object)
*
* @param object $object - managed object
*
* @return void
*
* @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 @@ -81,12 +77,11 @@ public function addReference(string $name, object $object)
*
* @psalm-param class-string<T>|null $class
*
* @return object
* @psalm-return ($class is null ? object : T)
*
* @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 @@ -107,10 +102,8 @@ public function getReference(string $name, ?string $class = null)
* @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference
*
* @psalm-param class-string|null $class
*
* @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;
}
9 changes: 3 additions & 6 deletions src/Executor/MongoDBExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@ 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 */
public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void
{
$this->dm->getEventManager()->removeEventListener(
$this->listener->getSubscribedEvents(),
Expand All @@ -62,7 +59,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
7 changes: 2 additions & 5 deletions src/Executor/ORMExecutorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,13 @@ 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 */
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
24 changes: 7 additions & 17 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 @@ -107,20 +107,16 @@ public function loadFromFile(string $fileName)

/**
* Has fixture?
*
* @return bool
*/
public function hasFixture(FixtureInterface $fixture)
public function hasFixture(FixtureInterface $fixture): bool
{
return isset($this->fixtures[get_class($fixture)]);
}

/**
* Get a specific fixture instance
*
* @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 @@ -197,10 +193,8 @@ public function getFixtures()
* class.
*
* @psalm-param class-string<object> $className
*
* @return bool
*/
public function isTransient(string $className)
public function isTransient(string $className): bool
{
$rc = new ReflectionClass($className);
if ($rc->isAbstract()) {
Expand All @@ -214,10 +208,8 @@ public function isTransient(string $className)

/**
* Creates the fixture object from the class.
*
* @return FixtureInterface
*/
protected function createFixture(string $class)
protected function createFixture(string $class): FixtureInterface
{
return new $class();
}
Expand Down Expand Up @@ -253,10 +245,8 @@ private function orderFixturesByNumber(): void

/**
* Orders fixtures by dependencies
*
* @return void
*/
private function orderFixturesByDependencies()
private function orderFixturesByDependencies(): void
{
/** @psalm-var array<class-string<DependentFixtureInterface>, int> */
$sequenceForClasses = [];
Expand Down
4 changes: 1 addition & 3 deletions src/OrderedFixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface OrderedFixtureInterface
{
/**
* Get the order of this fixture
*
* @return int
*/
public function getOrder();
public function getOrder(): int;
}
16 changes: 4 additions & 12 deletions src/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ class ProxyReferenceRepository extends ReferenceRepository
{
/**
* Serialize reference repository
*
* @return string
*/
public function serialize()
public function serialize(): string
{
$unitOfWork = $this->getManager()->getUnitOfWork();
$simpleReferences = [];
Expand All @@ -46,10 +44,8 @@ public function serialize()
* Unserialize reference repository
*
* @param string $serializedData Serialized data
*
* @return void
*/
public function unserialize(string $serializedData)
public function unserialize(string $serializedData): void
{
$repositoryData = unserialize($serializedData);

Expand Down Expand Up @@ -95,10 +91,8 @@ public function unserialize(string $serializedData)
* Load data fixture reference repository
*
* @param string $baseCacheName Base cache name
*
* @return bool
*/
public function load(string $baseCacheName)
public function load(string $baseCacheName): bool
{
$filename = $baseCacheName . '.ser';

Expand All @@ -121,10 +115,8 @@ public function load(string $baseCacheName)
* Save data fixture reference repository
*
* @param string $baseCacheName Base cache name
*
* @return void
*/
public function save(string $baseCacheName)
public function save(string $baseCacheName): void
{
$serializedData = $this->serialize();

Expand Down
11 changes: 3 additions & 8 deletions src/Purger/MongoDBPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,21 @@ 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 */
public function purge()
public function purge(): void
{
$metadatas = $this->dm->getMetadataFactory()->getAllMetadata();
foreach ($metadatas as $metadata) {
Expand Down
Loading

0 comments on commit 937bc46

Please sign in to comment.