From a0692f45f200d1b397230785565a8a3da843d2ed Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Sat, 9 Mar 2024 13:39:10 +0100 Subject: [PATCH 1/3] Add generic PHPStan type hint for `ServicesTrait::getService` (#414) --- src/Queue/ServicesTrait.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Queue/ServicesTrait.php b/src/Queue/ServicesTrait.php index 2e7ca99d..b1904381 100644 --- a/src/Queue/ServicesTrait.php +++ b/src/Queue/ServicesTrait.php @@ -13,14 +13,17 @@ trait ServicesTrait { protected ContainerInterface $container; /** - * @param string $id Classname or identifier of the service you want to retrieve + * @template OBJECT + * + * @param class-string $id Classname or identifier of the service you want to retrieve * * @throws \Psr\Container\NotFoundExceptionInterface * @throws \Psr\Container\ContainerExceptionInterface * - * @return mixed + * @return OBJECT */ - protected function getService(string $id): mixed { + protected function getService(string $id) { + return $this->container->get($id); } From 77ffc030a138e391474edd0b81073731c89ad705 Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Sat, 9 Mar 2024 13:39:44 +0100 Subject: [PATCH 2/3] Throw an exception if the container is not set yet (#414) --- src/Queue/ServicesTrait.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Queue/ServicesTrait.php b/src/Queue/ServicesTrait.php index b1904381..8d24d861 100644 --- a/src/Queue/ServicesTrait.php +++ b/src/Queue/ServicesTrait.php @@ -4,25 +4,31 @@ namespace Queue\Queue; use Cake\Core\ContainerInterface; +use LogicException; trait ServicesTrait { /** - * @var \Cake\Core\ContainerInterface + * @var \Cake\Core\ContainerInterface|null */ - protected ContainerInterface $container; + protected ?ContainerInterface $container = null; /** * @template OBJECT * * @param class-string $id Classname or identifier of the service you want to retrieve * - * @throws \Psr\Container\NotFoundExceptionInterface * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface * * @return OBJECT */ protected function getService(string $id) { + if ($this->container === null) { + throw new LogicException( + "The Container has not been set. Hint: getService() must not be called in the Task's constructor.", + ); + } return $this->container->get($id); } From 3dc186783e0f2d12f9b18bffbbb51d343e63c6cc Mon Sep 17 00:00:00 2001 From: passchn <77938819+passchn@users.noreply.github.com> Date: Sat, 9 Mar 2024 14:11:36 +0100 Subject: [PATCH 3/3] rename type variable (#414) --- src/Queue/ServicesTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Queue/ServicesTrait.php b/src/Queue/ServicesTrait.php index 8d24d861..d005d003 100644 --- a/src/Queue/ServicesTrait.php +++ b/src/Queue/ServicesTrait.php @@ -14,14 +14,14 @@ trait ServicesTrait { protected ?ContainerInterface $container = null; /** - * @template OBJECT + * @template T * - * @param class-string $id Classname or identifier of the service you want to retrieve + * @param class-string $id Classname or identifier of the service you want to retrieve * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface * - * @return OBJECT + * @return T */ protected function getService(string $id) { if ($this->container === null) {