Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return types on public classes and interfaces #459

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 0 additions & 5 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@
<exclude-pattern>src/*</exclude-pattern>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response to #459 (comment): This rule is applied by default. This config was to exclude src dir.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I missed that you've changed the config already. 🙈

<!-- Adding return types is a BC break in most cases. -->
<exclude-pattern>src/*</exclude-pattern>
</rule>

<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
Expand Down
22 changes: 9 additions & 13 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<file src="src/Executor/PHPCRExecutor.php">
<UndefinedClass>
<code>DocumentManagerInterface</code>
<code><![CDATA[DocumentManagerInterface]]></code>
<code><![CDATA[DocumentManagerInterface]]></code>
</UndefinedClass>
<UndefinedDocblockClass>
<code>DocumentManagerInterface</code>
</UndefinedDocblockClass>
</file>
<file src="src/Purger/PHPCRPurger.php">
<UndefinedClass>
<code><![CDATA[$this->dm]]></code>
<code>DocumentManager</code>
<code>NodeHelper</code>
<code>private</code>
<code><![CDATA[DocumentManager]]></code>
<code><![CDATA[DocumentManagerInterface|null]]></code>
<code><![CDATA[NodeHelper]]></code>
<code><![CDATA[private]]></code>
</UndefinedClass>
<UndefinedDocblockClass>
<code>DocumentManagerInterface|null</code>
</UndefinedDocblockClass>
</file>
<file src="src/ReferenceRepository.php">
<UndefinedClass>
<code>PhpcrDocumentManager</code>
<code>PhpcrDocumentManager</code>
<code><![CDATA[PhpcrDocumentManager]]></code>
<code><![CDATA[PhpcrDocumentManager]]></code>
</UndefinedClass>
</file>
</files>
20 changes: 5 additions & 15 deletions src/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
*/
protected $referenceRepository;

/**
* {@inheritDoc}
*/
public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void
{
$this->referenceRepository = $referenceRepository;
}
Expand All @@ -45,10 +42,8 @@
* @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

Check warning on line 46 in src/AbstractFixture.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractFixture.php#L46

Added line #L46 was not covered by tests
{
$this->getReferenceRepository()->setReference($name, $object);
}
Expand All @@ -63,11 +58,9 @@
*
* @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 +73,11 @@
*
* @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 +89,8 @@
* @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

Check warning on line 93 in src/AbstractFixture.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractFixture.php#L93

Added line #L93 was not covered by tests
{
return $this->getReferenceRepository()->hasReference($name, $class);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependentFixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface DependentFixtureInterface
*
* @psalm-return array<class-string<FixtureInterface>>
*/
public function getDependencies();
public function getDependencies(): array;
}
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 @@
$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

Check warning on line 53 in src/Executor/AbstractExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/AbstractExecutor.php#L53

Added line #L53 was not covered by tests
{
$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

Check warning on line 66 in src/Executor/AbstractExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/AbstractExecutor.php#L66

Added line #L66 was not covered by tests
{
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 @@
/**
* 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 @@
*
* @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 @@

/**
* Retrieve the DocumentManager instance this executor instance is using.
*
* @return DocumentManager
*/
public function getObjectManager()
public function getObjectManager(): DocumentManager

Check warning on line 42 in src/Executor/MongoDBExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/MongoDBExecutor.php#L42

Added line #L42 was not covered by tests
{
return $this->dm;
}

/** @inheritDoc */
public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void

Check warning on line 47 in src/Executor/MongoDBExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/MongoDBExecutor.php#L47

Added line #L47 was not covered by tests
{
$this->dm->getEventManager()->removeEventListener(
$this->listener->getSubscribedEvents(),
Expand All @@ -60,7 +57,7 @@
}

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

Check warning on line 60 in src/Executor/MongoDBExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/MongoDBExecutor.php#L60

Added line #L60 was not covered by tests
{
if ($append === false) {
$this->purge();
Expand Down
4 changes: 2 additions & 2 deletions src/Executor/ORMExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ 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) {
$this->em->wrapInTransaction(static function (EntityManagerInterface $em) use ($executor, $fixtures, $append): void {
if ($append === false) {
$executor->purge();
}
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 @@

/**
* Retrieve the EntityManagerInterface instance this executor instance is using.
*
* @return EntityManagerInterface
*/
public function getObjectManager()
public function getObjectManager(): EntityManagerInterface

Check warning on line 47 in src/Executor/ORMExecutorCommon.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/ORMExecutorCommon.php#L47

Added line #L47 was not covered by tests
{
return $this->originalManager;
}

/** @inheritDoc */
public function setReferenceRepository(ReferenceRepository $referenceRepository)
public function setReferenceRepository(ReferenceRepository $referenceRepository): void

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

View check run for this annotation

Codecov / codecov/patch

src/Executor/ORMExecutorCommon.php#L52

Added line #L52 was not covered by tests
{
$this->em->getEventManager()->removeEventListener(
$this->listener->getSubscribedEvents(),
Expand Down
7 changes: 3 additions & 4 deletions src/Executor/PHPCRExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@
$this->setPurger($purger);
}

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

Check warning on line 35 in src/Executor/PHPCRExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/PHPCRExecutor.php#L35

Added line #L35 was not covered by tests
{
return $this->dm;
}

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

Check warning on line 41 in src/Executor/PHPCRExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/PHPCRExecutor.php#L41

Added line #L41 was not covered by tests
{
$that = $this;

$function = static function ($dm) use ($append, $that, $fixtures) {
$function = static function ($dm) use ($append, $that, $fixtures): void {

Check warning on line 45 in src/Executor/PHPCRExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Executor/PHPCRExecutor.php#L45

Added line #L45 was not covered by tests
if ($append === false) {
$that->purge();
}
Expand Down
2 changes: 1 addition & 1 deletion src/FixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface FixtureInterface
/**
* Load data fixtures with the passed EntityManager
*/
public function load(ObjectManager $manager);
public function load(ObjectManager $manager): void;
Copy link
Member Author

@GromNaN GromNaN Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change can impact every project using data-fixtures. Adding the @return type annotation in the previous version will ease migration: #472

}
26 changes: 8 additions & 18 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
*
* @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 @@
*
* @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 @@

/**
* Has fixture?
*
* @return bool
*/
public function hasFixture(FixtureInterface $fixture)
public function hasFixture(FixtureInterface $fixture): bool

Check warning on line 110 in src/Loader.php

View check run for this annotation

Codecov / codecov/patch

src/Loader.php#L110

Added line #L110 was not covered by tests
{
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 All @@ -134,7 +130,7 @@
/**
* Add a fixture object instance to the loader.
*/
public function addFixture(FixtureInterface $fixture)
public function addFixture(FixtureInterface $fixture): void
{
$fixtureClass = $fixture::class;

Expand Down Expand Up @@ -196,10 +192,8 @@
* 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 @@

/**
* 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 @@

/**
* 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;
}
Loading
Loading