diff --git a/src/Container/ContainerConfigurator.php b/src/Container/ContainerConfigurator.php index 159afc1..64a8d2b 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 = []; @@ -56,7 +59,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 { @@ -68,7 +71,7 @@ public function addFactory(string $id, callable $factory): void /** * @param string $id - * @param callable(ContainerInterface $container):mixed $service + * @param Service $service * * @return void */ @@ -110,7 +113,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 40256bb..4dd862d 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; @@ -39,7 +43,7 @@ class ReadOnlyContainer implements ContainerInterface /** * ReadOnlyContainer constructor. * - * @param array $services + * @param array $services * @param array $factoryIds * @param ServiceExtensions|array $extensions * @param ContainerInterface[] $containers @@ -149,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/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; } 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;