Skip to content

Commit

Permalink
Add return types
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Mar 12, 2024
1 parent 36d2078 commit 5a655dc
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 156 deletions.
7 changes: 6 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ awareness about deprecated code.

# Upgrade to 2.0

You need PHP 8.1 or newer to use this library.
You need PHP 8.1 or newer to use this library.

## 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.

## Loggers have to implement the PSR-3 contracts

Expand Down
10 changes: 3 additions & 7 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<files psalm-version="5.17.0@c620f6e80d0abfca532b00bda366062aaedf6e5d">
<file src="src/Executor/PHPCRExecutor.php">
<UndefinedClass>
<code>DocumentManagerInterface</code>
</UndefinedClass>
<UndefinedDocblockClass>
<code>DocumentManagerInterface</code>
</UndefinedDocblockClass>
</UndefinedClass>
</file>
<file src="src/Purger/PHPCRPurger.php">
<UndefinedClass>
<code><![CDATA[$this->dm]]></code>
<code>DocumentManager</code>
<code>DocumentManagerInterface|null</code>
<code>NodeHelper</code>
<code>private</code>
</UndefinedClass>
<UndefinedDocblockClass>
<code>DocumentManagerInterface|null</code>
</UndefinedDocblockClass>
</file>
<file src="src/ReferenceRepository.php">
<UndefinedClass>
Expand Down
15 changes: 4 additions & 11 deletions src/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ private function getReferenceRepository(): ReferenceRepository
* @see 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 @@ -63,11 +61,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 @@ -80,12 +76,11 @@ public function addReference(string $name, object $object)
*
* @psalm-param class-string<T> $class
*
* @return object
* @psalm-return T
*
* @template T of object
*/
public function getReference(string $name, string $class)
public function getReference(string $name, string $class): object
{
return $this->getReferenceRepository()->getReference($name, $class);
}
Expand All @@ -97,10 +92,8 @@ public function getReference(string $name, string $class)
* @see ReferenceRepository::hasReference()
*
* @psalm-param class-string $class
*
* @return bool
*/
public function hasReference(string $name, string $class)
public function hasReference(string $name, string $class): bool
{
return $this->getReferenceRepository()->hasReference($name, $class);
}
Expand Down
24 changes: 7 additions & 17 deletions src/Executor/AbstractExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,33 @@ 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;
}

/**
* 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 @@ -100,11 +94,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 @@ -123,8 +115,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 @@ -38,16 +38,13 @@ public function __construct(private DocumentManager $dm, MongoDBPurger|null $pur

/**
* 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 @@ -60,7 +57,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|null

/**
* 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 @@ -32,14 +32,13 @@ public function __construct(private DocumentManagerInterface $dm, PHPCRPurger|nu
$this->setPurger($purger);
}

/** @return DocumentManagerInterface */
public function getObjectManager()
public function getObjectManager(): DocumentManagerInterface
{
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 @@ -72,7 +72,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 @@ -93,7 +93,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 @@ -106,20 +106,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[$fixture::class]);
}

/**
* 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 @@ -196,10 +192,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 @@ -213,10 +207,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 @@ -252,10 +244,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 @@ -20,10 +20,8 @@ class ProxyReferenceRepository extends ReferenceRepository
{
/**
* Serialize reference repository
*
* @return string
*/
public function serialize()
public function serialize(): string
{
return serialize([
'identitiesByClass' => $this->getIdentitiesByClass(),
Expand All @@ -34,10 +32,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 All @@ -60,10 +56,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 @@ -86,10 +80,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 @@ -24,26 +24,21 @@ public function __construct(private DocumentManager|null $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 5a655dc

Please sign in to comment.