From ecd9e98a1603817c50e73a2e7e97a2e6c7734b7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 14 Dec 2023 16:14:30 +0100 Subject: [PATCH 1/2] Add argument type-hint --- src/AbstractFixture.php | 12 +++---- src/Executor/AbstractExecutor.php | 8 ++--- src/Executor/MongoDBExecutor.php | 2 +- .../MultipleTransactionORMExecutor.php | 2 +- src/Executor/ORMExecutor.php | 2 +- src/Executor/PHPCRExecutor.php | 2 +- src/Loader.php | 18 ++++------- src/ProxyReferenceRepository.php | 6 ++-- src/Purger/ORMPurger.php | 4 +-- src/ReferenceRepository.php | 32 ++++++------------- src/Sorter/TopologicalSorter.php | 16 +++------- 11 files changed, 34 insertions(+), 70 deletions(-) diff --git a/src/AbstractFixture.php b/src/AbstractFixture.php index 8de1e971..3564a0a4 100644 --- a/src/AbstractFixture.php +++ b/src/AbstractFixture.php @@ -45,12 +45,11 @@ private function getReferenceRepository(): ReferenceRepository * * @see Doctrine\Common\DataFixtures\ReferenceRepository::setReference * - * @param string $name * @param object $object - managed object * * @return void */ - public function setReference($name, $object) + public function setReference(string $name, $object) { $this->getReferenceRepository()->setReference($name, $object); } @@ -63,14 +62,13 @@ public function setReference($name, $object) * * @see Doctrine\Common\DataFixtures\ReferenceRepository::addReference * - * @param string $name * @param object $object - managed object * * @return void * * @throws BadMethodCallException - if repository already has a reference by $name. */ - public function addReference($name, $object) + public function addReference(string $name, object $object) { $this->getReferenceRepository()->addReference($name, $object); } @@ -81,7 +79,6 @@ public function addReference($name, $object) * * @see Doctrine\Common\DataFixtures\ReferenceRepository::getReference * - * @param string $name * @psalm-param class-string|null $class * * @return object @@ -89,7 +86,7 @@ public function addReference($name, $object) * * @template T of object */ - public function getReference($name, ?string $class = null) + public function getReference(string $name, ?string $class = null) { if ($class === null) { Deprecation::trigger( @@ -109,12 +106,11 @@ public function getReference($name, ?string $class = null) * * @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference * - * @param string $name * @psalm-param class-string $class * * @return bool */ - public function hasReference($name, ?string $class = null) + public function hasReference(string $name, ?string $class = null) { if ($class === null) { Deprecation::trigger( diff --git a/src/Executor/AbstractExecutor.php b/src/Executor/AbstractExecutor.php index 0c3c78d5..d0c31b3b 100644 --- a/src/Executor/AbstractExecutor.php +++ b/src/Executor/AbstractExecutor.php @@ -76,11 +76,9 @@ public function getPurger() /** * Set the logger callable to execute with the log() method. * - * @param callable $logger - * * @return void */ - public function setLogger($logger) + public function setLogger(callable $logger) { $this->logger = $logger; } @@ -88,11 +86,9 @@ public function setLogger($logger) /** * Logs a message using the logger. * - * @param string $message - * * @return void */ - public function log($message) + public function log(string $message) { $logger = $this->logger; $logger($message); diff --git a/src/Executor/MongoDBExecutor.php b/src/Executor/MongoDBExecutor.php index f48a55cd..ff2a72d1 100644 --- a/src/Executor/MongoDBExecutor.php +++ b/src/Executor/MongoDBExecutor.php @@ -60,7 +60,7 @@ public function setReferenceRepository(ReferenceRepository $referenceRepository) } /** @inheritDoc */ - public function execute(array $fixtures, $append = false) + public function execute(array $fixtures, bool $append = false) { if ($append === false) { $this->purge(); diff --git a/src/Executor/MultipleTransactionORMExecutor.php b/src/Executor/MultipleTransactionORMExecutor.php index 7bd2e08a..7cc954dd 100644 --- a/src/Executor/MultipleTransactionORMExecutor.php +++ b/src/Executor/MultipleTransactionORMExecutor.php @@ -11,7 +11,7 @@ final class MultipleTransactionORMExecutor extends AbstractExecutor use ORMExecutorCommon; /** @inheritDoc */ - public function execute(array $fixtures, $append = false): void + public function execute(array $fixtures, bool $append = false): void { $executor = $this; if ($append === false) { diff --git a/src/Executor/ORMExecutor.php b/src/Executor/ORMExecutor.php index 0e279723..a82a0a2a 100644 --- a/src/Executor/ORMExecutor.php +++ b/src/Executor/ORMExecutor.php @@ -14,7 +14,7 @@ class ORMExecutor extends AbstractExecutor use ORMExecutorCommon; /** @inheritDoc */ - public function execute(array $fixtures, $append = false) + public function execute(array $fixtures, bool $append = false) { $executor = $this; $this->em->wrapInTransaction(static function (EntityManagerInterface $em) use ($executor, $fixtures, $append) { diff --git a/src/Executor/PHPCRExecutor.php b/src/Executor/PHPCRExecutor.php index 58a83278..bd1fca70 100644 --- a/src/Executor/PHPCRExecutor.php +++ b/src/Executor/PHPCRExecutor.php @@ -40,7 +40,7 @@ public function getObjectManager() } /** @inheritDoc */ - public function execute(array $fixtures, $append = false) + public function execute(array $fixtures, bool $append = false) { $that = $this; diff --git a/src/Loader.php b/src/Loader.php index 5006a9bb..ea406d2f 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -73,7 +73,7 @@ class Loader * * @return array $fixtures Array of loaded fixture object instances. */ - public function loadFromDirectory($dir) + public function loadFromDirectory(string $dir) { if (! is_dir($dir)) { throw new InvalidArgumentException(sprintf('"%s" does not exist', $dir)); @@ -94,7 +94,7 @@ public function loadFromDirectory($dir) * * @return array $fixtures Array of loaded fixture object instances. */ - public function loadFromFile($fileName) + public function loadFromFile(string $fileName) { if (! is_readable($fileName)) { throw new InvalidArgumentException(sprintf('"%s" does not exist or is not readable', $fileName)); @@ -108,11 +108,9 @@ public function loadFromFile($fileName) /** * Has fixture? * - * @param FixtureInterface $fixture - * * @return bool */ - public function hasFixture($fixture) + public function hasFixture(FixtureInterface $fixture) { return isset($this->fixtures[get_class($fixture)]); } @@ -120,11 +118,9 @@ public function hasFixture($fixture) /** * Get a specific fixture instance * - * @param string $className - * * @return FixtureInterface */ - public function getFixture($className) + public function getFixture(string $className) { if (! isset($this->fixtures[$className])) { throw new InvalidArgumentException(sprintf( @@ -204,7 +200,7 @@ public function getFixtures() * * @return bool */ - public function isTransient($className) + public function isTransient(string $className) { $rc = new ReflectionClass($className); if ($rc->isAbstract()) { @@ -219,11 +215,9 @@ public function isTransient($className) /** * Creates the fixture object from the class. * - * @param string $class - * * @return FixtureInterface */ - protected function createFixture($class) + protected function createFixture(string $class) { return new $class(); } diff --git a/src/ProxyReferenceRepository.php b/src/ProxyReferenceRepository.php index d5d28f9a..605ef481 100644 --- a/src/ProxyReferenceRepository.php +++ b/src/ProxyReferenceRepository.php @@ -49,7 +49,7 @@ public function serialize() * * @return void */ - public function unserialize($serializedData) + public function unserialize(string $serializedData) { $repositoryData = unserialize($serializedData); @@ -98,7 +98,7 @@ public function unserialize($serializedData) * * @return bool */ - public function load($baseCacheName) + public function load(string $baseCacheName) { $filename = $baseCacheName . '.ser'; @@ -124,7 +124,7 @@ public function load($baseCacheName) * * @return void */ - public function save($baseCacheName) + public function save(string $baseCacheName) { $serializedData = $this->serialize(); diff --git a/src/Purger/ORMPurger.php b/src/Purger/ORMPurger.php index b153a0e6..2fbf4095 100644 --- a/src/Purger/ORMPurger.php +++ b/src/Purger/ORMPurger.php @@ -59,11 +59,9 @@ public function __construct(?EntityManagerInterface $em = null, array $excluded /** * Set the purge mode * - * @param int $mode - * * @return void */ - public function setPurgeMode($mode) + public function setPurgeMode(int $mode) { $this->purgeMode = $mode; $this->cachedSqlStatements = null; diff --git a/src/ReferenceRepository.php b/src/ReferenceRepository.php index 23adec11..60ede99f 100644 --- a/src/ReferenceRepository.php +++ b/src/ReferenceRepository.php @@ -75,7 +75,7 @@ public function __construct(ObjectManager $manager) * * @return array */ - protected function getIdentifier($reference, $uow) + protected function getIdentifier(object $reference, object $uow) { // In case Reference is not yet managed in UnitOfWork if (! $this->hasIdentifier($reference)) { @@ -103,12 +103,9 @@ protected function getIdentifier($reference, $uow) * and referenced to $reference. If $name * already is set, it overrides it * - * @param string $name - * @param object $reference - * * @return void */ - public function setReference($name, $reference) + public function setReference(string $name, object $reference) { $class = $this->getRealClass(get_class($reference)); @@ -134,13 +131,12 @@ public function setReference($name, $reference) /** * Store the identifier of a reference * - * @param string $name * @param mixed $identity * @param class-string|null $class * * @return void */ - public function setReferenceIdentity($name, $identity, ?string $class = null) + public function setReferenceIdentity(string $name, $identity, ?string $class = null) { if ($class === null) { Deprecation::trigger( @@ -166,14 +162,13 @@ public function setReferenceIdentity($name, $identity, ?string $class = null) * the record is inserted, be sure tu use this method * after $object is flushed * - * @param string $name * @param object $object - managed object * * @return void * * @throws BadMethodCallException - if repository already has a reference by $name. */ - public function addReference($name, $object) + public function addReference(string $name, object $object) { // For BC, to be removed in next major. if (isset($this->references[$name])) { @@ -199,7 +194,6 @@ public function addReference($name, $object) * Loads an object using stored reference * named by $name * - * @param string $name * @psalm-param class-string|null $class * * @return object @@ -209,7 +203,7 @@ public function addReference($name, $object) * * @template T of object */ - public function getReference($name, ?string $class = null) + public function getReference(string $name, ?string $class = null) { if ($class === null) { Deprecation::trigger( @@ -256,12 +250,11 @@ public function getReference($name, ?string $class = null) * Check if an object is stored using reference * named by $name * - * @param string $name * @psalm-param class-string $class * * @return bool */ - public function hasReference($name, ?string $class = null) + public function hasReference(string $name, ?string $class = null) { if ($class === null) { Deprecation::trigger( @@ -281,11 +274,9 @@ public function hasReference($name, ?string $class = null) * Searches for reference names in the * list of stored references * - * @param object $reference - * * @return array */ - public function getReferenceNames($reference) + public function getReferenceNames(object $reference) { $class = $this->getRealClass(get_class($reference)); if (! isset($this->referencesByClass[$class])) { @@ -298,12 +289,11 @@ public function getReferenceNames($reference) /** * Checks if reference has identity stored * - * @param string $name * @param class-string|null $class * * @return bool */ - public function hasIdentity($name, ?string $class = null) + public function hasIdentity(string $name, ?string $class = null) { if ($class === null) { Deprecation::trigger( @@ -380,7 +370,7 @@ public function getManager() * * @return string */ - protected function getRealClass($className) + protected function getRealClass(string $className) { return $this->manager->getClassMetadata($className)->getName(); } @@ -388,11 +378,9 @@ protected function getRealClass($className) /** * Checks if object has identifier already in unit of work. * - * @param object $reference - * * @return bool */ - private function hasIdentifier($reference) + private function hasIdentifier(object $reference) { // in case if reference is set after flush, store its identity $uow = $this->manager->getUnitOfWork(); diff --git a/src/Sorter/TopologicalSorter.php b/src/Sorter/TopologicalSorter.php index a8c1ef81..a1334f39 100644 --- a/src/Sorter/TopologicalSorter.php +++ b/src/Sorter/TopologicalSorter.php @@ -43,8 +43,7 @@ class TopologicalSorter */ private bool $allowCyclicDependencies; - /** @param bool $allowCyclicDependencies */ - public function __construct($allowCyclicDependencies = true) + public function __construct(bool $allowCyclicDependencies = true) { $this->allowCyclicDependencies = (bool) $allowCyclicDependencies; } @@ -52,11 +51,9 @@ public function __construct($allowCyclicDependencies = true) /** * Adds a new node (vertex) to the graph, assigning its hash and value. * - * @param string $hash - * * @return void */ - public function addNode($hash, ClassMetadata $node) + public function addNode(string $hash, ClassMetadata $node) { $this->nodeList[$hash] = new Vertex($node); } @@ -64,11 +61,9 @@ public function addNode($hash, ClassMetadata $node) /** * Checks the existence of a node in the graph. * - * @param string $hash - * * @return bool */ - public function hasNode($hash) + public function hasNode(string $hash) { return isset($this->nodeList[$hash]); } @@ -76,12 +71,9 @@ public function hasNode($hash) /** * Adds a new dependency (edge) to the graph using their hashes. * - * @param string $fromHash - * @param string $toHash - * * @return void */ - public function addDependency($fromHash, $toHash) + public function addDependency(string $fromHash, string $toHash) { $definition = $this->nodeList[$fromHash]; From 19763cdf55d6b2d0a2e6c4ec7625e9a252908c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 14 Dec 2023 17:06:35 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Alexander M. Turek --- src/AbstractFixture.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AbstractFixture.php b/src/AbstractFixture.php index 3564a0a4..41b757b5 100644 --- a/src/AbstractFixture.php +++ b/src/AbstractFixture.php @@ -49,7 +49,7 @@ private function getReferenceRepository(): ReferenceRepository * * @return void */ - public function setReference(string $name, $object) + public function setReference(string $name, object $object) { $this->getReferenceRepository()->setReference($name, $object); } @@ -106,7 +106,7 @@ public function getReference(string $name, ?string $class = null) * * @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference * - * @psalm-param class-string $class + * @psalm-param class-string|null $class * * @return bool */