Skip to content

Commit c6855a6

Browse files
authored
psalm-types for complex service/extension/factory type hints. (#46)
* introduce psalm-types for complex service/extension/factory callables type hints.
1 parent feaa4d0 commit c6855a6

7 files changed

Lines changed: 43 additions & 16 deletions

File tree

src/Container/ContainerConfigurator.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
namespace Inpsyde\Modularity\Container;
55

6-
use Psr\Container\ContainerExceptionInterface;
76
use Psr\Container\ContainerInterface;
87

8+
/**
9+
* @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
10+
* @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
11+
*/
912
class ContainerConfigurator
1013
{
1114
/**
12-
* @var array<string, callable(ContainerInterface $container):mixed>
15+
* @var array<string, Service>
1316
*/
1417
private $services = [];
1518

@@ -56,7 +59,7 @@ public function addContainer(ContainerInterface $container): void
5659

5760
/**
5861
* @param string $id
59-
* @param callable(ContainerInterface $container):mixed $factory
62+
* @param Service $factory
6063
*/
6164
public function addFactory(string $id, callable $factory): void
6265
{
@@ -68,7 +71,7 @@ public function addFactory(string $id, callable $factory): void
6871

6972
/**
7073
* @param string $id
71-
* @param callable(ContainerInterface $container):mixed $service
74+
* @param Service $service
7275
*
7376
* @return void
7477
*/
@@ -110,7 +113,7 @@ public function hasService(string $id): bool
110113

111114
/**
112115
* @param string $id
113-
* @param callable(mixed $service, ContainerInterface $container):mixed $extender
116+
* @param ExtendingService $extender
114117
*
115118
* @return void
116119
*/

src/Container/ReadOnlyContainer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
use Psr\Container\ContainerInterface;
88
use Psr\Container\NotFoundExceptionInterface;
99

10+
/**
11+
* @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
12+
* @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
13+
*/
1014
class ReadOnlyContainer implements ContainerInterface
1115
{
1216
/**
13-
* @var array<string, callable(\Psr\Container\ContainerInterface $container):mixed>
17+
* @var array<string, Service>
1418
*/
1519
private $services;
1620

@@ -39,7 +43,7 @@ class ReadOnlyContainer implements ContainerInterface
3943
/**
4044
* ReadOnlyContainer constructor.
4145
*
42-
* @param array<string, callable(ContainerInterface $container):mixed> $services
46+
* @param array<string, Service> $services
4347
* @param array<string, bool> $factoryIds
4448
* @param ServiceExtensions|array $extensions
4549
* @param ContainerInterface[] $containers
@@ -149,7 +153,7 @@ private function configureServiceExtensions($extensions): ServiceExtensions
149153
foreach ($extensions as $id => $callback) {
150154
/**
151155
* @var string $id
152-
* @var callable(mixed,ContainerInterface):mixed $callback
156+
* @var ExtendingService $callback
153157
*/
154158
$servicesExtensions->add($id, $callback);
155159
}

src/Container/ServiceExtensions.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
use Psr\Container\ContainerInterface as Container;
88

9+
/**
10+
* @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
11+
*/
912
class ServiceExtensions
1013
{
1114
private const SERVICE_TYPE_NOT_CHANGED = 1;
1215
private const SERVICE_TYPE_CHANGED = 2;
1316
private const SERVICE_TYPE_NOT_OBJECT = 0;
1417

1518
/**
16-
* @var array<string, list<callable>>
19+
* @var array<string, list<ExtendingService>>
1720
*/
1821
protected $extensions = [];
1922

@@ -28,7 +31,7 @@ final public static function typeId(string $type): string
2831

2932
/**
3033
* @param string $extensionId
31-
* @param callable $extender
34+
* @param ExtendingService $extender
3235
* @return static
3336
*/
3437
public function add(string $extensionId, callable $extender): ServiceExtensions
@@ -94,7 +97,7 @@ protected function resolveByType(
9497

9598
$extendedClasses[] = $className;
9699

97-
/** @var array<class-string, list<callable>> $allCallbacks */
100+
/** @var array<class-string, list<ExtendingService>> $allCallbacks */
98101
$allCallbacks = [];
99102

100103
// 1st group of extensions: targeting exact class
@@ -147,7 +150,7 @@ protected function resolveByType(
147150
* @param class-string $type
148151
* @param object $service
149152
* @param Container $container
150-
* @param array $extenders
153+
* @param list<ExtendingService> $extenders
151154
* @return array{mixed, int}
152155
*/
153156
private function extendByType(

src/Module/ExtendingModule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
namespace Inpsyde\Modularity\Module;
66

7+
use Psr\Container\ContainerInterface;
8+
9+
/**
10+
* @psalm-type ExtendingService = callable(mixed $service, ContainerInterface $container):mixed
11+
*/
712
interface ExtendingModule extends Module
813
{
914

@@ -18,7 +23,7 @@ interface ExtendingModule extends Module
1823
* That is done by using as ID (array key in the `extensions` method) the target module ID
1924
* and the service ID.
2025
*
21-
* @return array<string, callable(mixed $service, \Psr\Container\ContainerInterface $container):mixed>
26+
* @return array<string, ExtendingService>
2227
*/
2328
public function extensions(): array;
2429
}

src/Module/FactoryModule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Inpsyde\Modularity\Module;
66

7+
/**
8+
* @psalm-import-type Service from ServiceModule
9+
*/
710
interface FactoryModule extends Module
811
{
912
/**
@@ -12,7 +15,7 @@ interface FactoryModule extends Module
1215
* Similar to `services`, but object created by given factories are not "cached", but a *new*
1316
* instance is returned everytime `get()` is called in the container.
1417
*
15-
* @return array<string, callable(\Psr\Container\ContainerInterface $container):mixed>
18+
* @return array<string, Service>
1619
*/
1720
public function factories(): array;
1821
}

src/Module/ServiceModule.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
namespace Inpsyde\Modularity\Module;
66

7+
use Psr\Container\ContainerInterface;
8+
9+
/**
10+
* @psalm-type Service = callable(ContainerInterface $container):mixed
11+
*/
712
interface ServiceModule extends Module
813
{
914

@@ -15,7 +20,7 @@ interface ServiceModule extends Module
1520
* Services are "cached", so the given factory is called once the first time `get()` is called
1621
* in the container, and on subsequent `get()` the same instance is returned again and again.
1722
*
18-
* @return array<string, callable(\Psr\Container\ContainerInterface $container):mixed>
23+
* @return array<string, Service>
1924
*/
2025
public function services(): array;
2126
}

src/Package.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use Inpsyde\Modularity\Properties\Properties;
1515
use Psr\Container\ContainerInterface;
1616

17+
/**
18+
* @psalm-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
19+
* @psalm-import-type ExtendingService from \Inpsyde\Modularity\Module\ExtendingModule
20+
*/
1721
class Package
1822
{
1923
/**
@@ -507,7 +511,7 @@ private function addModuleServices(Module $module, string $status): bool
507511
array_walk(
508512
$services,
509513
static function (callable $service, string $id) use ($addCallback, &$ids) {
510-
/** @var callable(string, callable) $addCallback */
514+
/** @var callable(string, Service|ExtendingService) $addCallback */
511515
$addCallback($id, $service);
512516
/** @var list<string> $ids */
513517
$ids[] = $id;

0 commit comments

Comments
 (0)