Skip to content

Commit

Permalink
introduce psalm-types for complex service/extension/factory callables…
Browse files Browse the repository at this point in the history
… type hints.
  • Loading branch information
Chrico committed May 3, 2024
1 parent 7538809 commit eab8168
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/Container/ContainerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, callable(ContainerInterface $container):mixed>
* @var array<string, Service>
*/
private $services = [];

Expand All @@ -19,7 +22,7 @@ class ContainerConfigurator
private $factoryIds = [];

/**
* @var array<string, array<callable(mixed $service, ContainerInterface $container):mixed>>
* @var array<string, array<ExtendingService>>
*/
private $extensions = [];

Expand Down Expand Up @@ -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
{
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
12 changes: 8 additions & 4 deletions src/Container/ReadOnlyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, callable(\Psr\Container\ContainerInterface $container):mixed>
* @var array<string, Service>
*/
private $services;

Expand All @@ -20,7 +24,7 @@ class ReadOnlyContainer implements ContainerInterface
private $factoryIds;

/**
* @var array<string, array<callable(mixed, ContainerInterface $container):mixed>>
* @var array<string, array<ExtendingService>>
*/
private $extensions;

Expand All @@ -39,9 +43,9 @@ class ReadOnlyContainer implements ContainerInterface
/**
* ReadOnlyContainer constructor.
*
* @param array<string, callable(ContainerInterface $container):mixed> $services
* @param array<string, Service> $services
* @param array<string, bool> $factoryIds
* @param array<string, array<callable(mixed, ContainerInterface $container):mixed>> $extensions
* @param array<string, array<ExtendingService>> $extensions
* @param ContainerInterface[] $containers
*/
public function __construct(
Expand Down
7 changes: 6 additions & 1 deletion src/Module/ExtendingModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -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<string, callable(mixed $service, \Psr\Container\ContainerInterface $container):mixed>
* @return array<string, ExtendingService>
*/
public function extensions(): array;
}
5 changes: 4 additions & 1 deletion src/Module/FactoryModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Inpsyde\Modularity\Module;

/**
* @psalm-import-type Service from ServiceModule
*/
interface FactoryModule extends Module
{
/**
Expand All @@ -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<string, callable(\Psr\Container\ContainerInterface $container):mixed>
* @return array<string, Service>
*/
public function factories(): array;
}
7 changes: 6 additions & 1 deletion src/Module/ServiceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Inpsyde\Modularity\Module;

use Psr\Container\ContainerInterface;

/**
* @psalm-type Service = callable(ContainerInterface $container):mixed
*/
interface ServiceModule extends Module
{

Expand All @@ -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<string, callable(\Psr\Container\ContainerInterface $container):mixed>
* @return array<string, Service>
*/
public function services(): array;
}

0 comments on commit eab8168

Please sign in to comment.