From eab8168cc5bcd0b7ab46c3b709e189cf6b4b4a7e Mon Sep 17 00:00:00 2001 From: Christian Leucht Date: Fri, 3 May 2024 10:50:54 +0200 Subject: [PATCH 1/2] introduce psalm-types for complex service/extension/factory callables type hints. --- src/Container/ContainerConfigurator.php | 15 +++++++++------ src/Container/ReadOnlyContainer.php | 12 ++++++++---- src/Module/ExtendingModule.php | 7 ++++++- src/Module/FactoryModule.php | 5 ++++- src/Module/ServiceModule.php | 7 ++++++- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Container/ContainerConfigurator.php b/src/Container/ContainerConfigurator.php index a6d849d..c8d91df 100644 --- a/src/Container/ContainerConfigurator.php +++ b/src/Container/ContainerConfigurator.php @@ -3,13 +3,16 @@ namespace Inpsyde\Modularity\Container; -use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +/** + * @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule + * @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule + */ class ContainerConfigurator { /** - * @var array + * @var array */ private $services = []; @@ -19,7 +22,7 @@ class ContainerConfigurator private $factoryIds = []; /** - * @var array> + * @var array> */ private $extensions = []; @@ -55,7 +58,7 @@ public function addContainer(ContainerInterface $container): void /** * @param string $id - * @param callable(ContainerInterface $container):mixed $factory + * @param Service $factory */ public function addFactory(string $id, callable $factory): void { @@ -67,7 +70,7 @@ public function addFactory(string $id, callable $factory): void /** * @param string $id - * @param callable(ContainerInterface $container):mixed $service + * @param Service $service * * @return void */ @@ -109,7 +112,7 @@ public function hasService(string $id): bool /** * @param string $id - * @param callable(mixed $service, ContainerInterface $container):mixed $extender + * @param ExtendingService $extender * * @return void */ diff --git a/src/Container/ReadOnlyContainer.php b/src/Container/ReadOnlyContainer.php index 99dd12a..b1a9590 100644 --- a/src/Container/ReadOnlyContainer.php +++ b/src/Container/ReadOnlyContainer.php @@ -7,10 +7,14 @@ use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +/** + * @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule + * @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule + */ class ReadOnlyContainer implements ContainerInterface { /** - * @var array + * @var array */ private $services; @@ -20,7 +24,7 @@ class ReadOnlyContainer implements ContainerInterface private $factoryIds; /** - * @var array> + * @var array> */ private $extensions; @@ -39,9 +43,9 @@ class ReadOnlyContainer implements ContainerInterface /** * ReadOnlyContainer constructor. * - * @param array $services + * @param array $services * @param array $factoryIds - * @param array> $extensions + * @param array> $extensions * @param ContainerInterface[] $containers */ public function __construct( diff --git a/src/Module/ExtendingModule.php b/src/Module/ExtendingModule.php index cbcb2d3..adc1c21 100644 --- a/src/Module/ExtendingModule.php +++ b/src/Module/ExtendingModule.php @@ -4,6 +4,11 @@ namespace Inpsyde\Modularity\Module; +use Psr\Container\ContainerInterface; + +/** + * @psalm-type ExtendingService = callable(mixed $service, ContainerInterface $container):mixed + */ interface ExtendingModule extends Module { @@ -18,7 +23,7 @@ interface ExtendingModule extends Module * That is done by using as ID (array key in the `extensions` method) the target module ID * and the service ID. * - * @return array + * @return array */ public function extensions(): array; } diff --git a/src/Module/FactoryModule.php b/src/Module/FactoryModule.php index 9f0bed9..10517f2 100644 --- a/src/Module/FactoryModule.php +++ b/src/Module/FactoryModule.php @@ -4,6 +4,9 @@ namespace Inpsyde\Modularity\Module; +/** + * @psalm-import-type Service from ServiceModule + */ interface FactoryModule extends Module { /** @@ -12,7 +15,7 @@ interface FactoryModule extends Module * Similar to `services`, but object created by given factories are not "cached", but a *new* * instance is returned everytime `get()` is called in the container. * - * @return array + * @return array */ public function factories(): array; } diff --git a/src/Module/ServiceModule.php b/src/Module/ServiceModule.php index 464db07..6bf2d77 100644 --- a/src/Module/ServiceModule.php +++ b/src/Module/ServiceModule.php @@ -4,6 +4,11 @@ namespace Inpsyde\Modularity\Module; +use Psr\Container\ContainerInterface; + +/** + * @psalm-type Service = callable(ContainerInterface $container):mixed + */ interface ServiceModule extends Module { @@ -15,7 +20,7 @@ interface ServiceModule extends Module * Services are "cached", so the given factory is called once the first time `get()` is called * in the container, and on subsequent `get()` the same instance is returned again and again. * - * @return array + * @return array */ public function services(): array; } From 2c50cb62d09c06d125d0a3ea1a4bedddf977b6da Mon Sep 17 00:00:00 2001 From: Christian Leucht Date: Fri, 10 May 2024 09:53:08 +0200 Subject: [PATCH 2/2] update type hints for new ServiceExtensions feature. --- src/Container/ReadOnlyContainer.php | 2 +- src/Container/ServiceExtensions.php | 11 +++++++---- src/Package.php | 6 +++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Container/ReadOnlyContainer.php b/src/Container/ReadOnlyContainer.php index 2da3bd7..4dd862d 100644 --- a/src/Container/ReadOnlyContainer.php +++ b/src/Container/ReadOnlyContainer.php @@ -153,7 +153,7 @@ private function configureServiceExtensions($extensions): ServiceExtensions foreach ($extensions as $id => $callback) { /** * @var string $id - * @var callable(mixed,ContainerInterface):mixed $callback + * @var ExtendingService $callback */ $servicesExtensions->add($id, $callback); } diff --git a/src/Container/ServiceExtensions.php b/src/Container/ServiceExtensions.php index 94c8909..63fb1f6 100644 --- a/src/Container/ServiceExtensions.php +++ b/src/Container/ServiceExtensions.php @@ -6,6 +6,9 @@ use Psr\Container\ContainerInterface as Container; +/** + * @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule + */ class ServiceExtensions { private const SERVICE_TYPE_NOT_CHANGED = 1; @@ -13,7 +16,7 @@ class ServiceExtensions private const SERVICE_TYPE_NOT_OBJECT = 0; /** - * @var array> + * @var array> */ protected $extensions = []; @@ -28,7 +31,7 @@ final public static function typeId(string $type): string /** * @param string $extensionId - * @param callable $extender + * @param ExtendingService $extender * @return static */ public function add(string $extensionId, callable $extender): ServiceExtensions @@ -94,7 +97,7 @@ protected function resolveByType( $extendedClasses[] = $className; - /** @var array> $allCallbacks */ + /** @var array> $allCallbacks */ $allCallbacks = []; // 1st group of extensions: targeting exact class @@ -147,7 +150,7 @@ protected function resolveByType( * @param class-string $type * @param object $service * @param Container $container - * @param array $extenders + * @param list $extenders * @return array{mixed, int} */ private function extendByType( diff --git a/src/Package.php b/src/Package.php index 06177f2..f75ccd9 100644 --- a/src/Package.php +++ b/src/Package.php @@ -14,6 +14,10 @@ use Inpsyde\Modularity\Properties\Properties; use Psr\Container\ContainerInterface; +/** + * @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule + * @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule + */ class Package { /** @@ -507,7 +511,7 @@ private function addModuleServices(Module $module, string $status): bool array_walk( $services, static function (callable $service, string $id) use ($addCallback, &$ids) { - /** @var callable(string, callable) $addCallback */ + /** @var callable(string, Service|ExtendingService) $addCallback */ $addCallback($id, $service); /** @var list $ids */ $ids[] = $id;