diff --git a/src/Container.php b/src/Container.php index 852e756..66c70a0 100644 --- a/src/Container.php +++ b/src/Container.php @@ -14,40 +14,17 @@ class Container implements DefinitionContainerInterface { - /** - * @var boolean - */ - protected $defaultToShared = false; - - /** - * @var DefinitionAggregateInterface - */ - protected $definitions; - - /** - * @var ServiceProviderAggregateInterface - */ - protected $providers; - - /** - * @var InflectorAggregateInterface - */ - protected $inflectors; - /** * @var ContainerInterface[] */ - protected $delegates = []; + protected array $delegates = []; public function __construct( - DefinitionAggregateInterface $definitions = null, - ServiceProviderAggregateInterface $providers = null, - InflectorAggregateInterface $inflectors = null + protected DefinitionAggregateInterface $definitions = new DefinitionAggregate(), + protected ServiceProviderAggregateInterface $providers = new ServiceProviderAggregate(), + protected InflectorAggregateInterface $inflectors = new InflectorAggregate(), + protected bool $defaultToShared = false ) { - $this->definitions = $definitions ?? new DefinitionAggregate(); - $this->providers = $providers ?? new ServiceProviderAggregate(); - $this->inflectors = $inflectors ?? new InflectorAggregate(); - if ($this->definitions instanceof ContainerAwareInterface) { $this->definitions->setContainer($this); } @@ -61,7 +38,7 @@ public function __construct( } } - public function add(string $id, $concrete = null): DefinitionInterface + public function add(string $id, mixed $concrete = null): DefinitionInterface { $concrete ??= $id; @@ -72,7 +49,7 @@ public function add(string $id, $concrete = null): DefinitionInterface return $this->definitions->add($id, $concrete); } - public function addShared(string $id, $concrete = null): DefinitionInterface + public function addShared(string $id, mixed $concrete = null): DefinitionInterface { $concrete ??= $id; return $this->definitions->addShared($id, $concrete); @@ -155,7 +132,7 @@ public function delegate(ContainerInterface $container): self return $this; } - protected function resolve(string $id, bool $new = false) + protected function resolve(string $id, bool $new = false): mixed { if ($this->definitions->has($id)) { $resolved = (true === $new) ? $this->definitions->resolveNew($id) : $this->definitions->resolve($id); @@ -177,6 +154,7 @@ protected function resolve(string $id, bool $new = false) if ($this->providers->provides($id)) { $this->providers->register($id); + /** @noinspection PhpUnreachableStatementInspection */ if (false === $this->definitions->has($id) && false === $this->definitions->hasTag($id)) { throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id)); } diff --git a/src/ContainerAwareTrait.php b/src/ContainerAwareTrait.php index 1519f34..d4e7711 100644 --- a/src/ContainerAwareTrait.php +++ b/src/ContainerAwareTrait.php @@ -12,7 +12,7 @@ trait ContainerAwareTrait /** * @var ?DefinitionContainerInterface */ - protected $container; + protected ?DefinitionContainerInterface $container = null; public function setContainer(DefinitionContainerInterface $container): ContainerAwareInterface { diff --git a/src/Definition/Definition.php b/src/Definition/Definition.php index a34191d..8b774f6 100644 --- a/src/Definition/Definition.php +++ b/src/Definition/Definition.php @@ -12,7 +12,6 @@ }; use League\Container\ContainerAwareTrait; use League\Container\Exception\ContainerException; -use League\Container\Exception\NotFoundException; use Psr\Container\ContainerInterface; use ReflectionClass; @@ -78,7 +77,7 @@ public function isShared(): bool return $this->shared; } - public function getConcrete() + public function getConcrete(): mixed { return $this->concrete; } diff --git a/src/Definition/DefinitionAggregate.php b/src/Definition/DefinitionAggregate.php index ec008f5..11f315a 100644 --- a/src/Definition/DefinitionAggregate.php +++ b/src/Definition/DefinitionAggregate.php @@ -12,19 +12,14 @@ class DefinitionAggregate implements DefinitionAggregateInterface { use ContainerAwareTrait; - /** - * @var DefinitionInterface[] - */ - protected array $definitions = []; - - public function __construct(array $definitions = []) + public function __construct(protected array $definitions = []) { - $this->definitions = array_filter($definitions, static function ($definition) { + $this->definitions = array_filter($this->definitions, static function ($definition) { return ($definition instanceof DefinitionInterface); }); } - public function add(string $id, $definition): DefinitionInterface + public function add(string $id, mixed $definition): DefinitionInterface { if (false === ($definition instanceof DefinitionInterface)) { $definition = new Definition($id, $definition); @@ -35,7 +30,7 @@ public function add(string $id, $definition): DefinitionInterface return $definition; } - public function addShared(string $id, $definition): DefinitionInterface + public function addShared(string $id, mixed $definition): DefinitionInterface { $definition = $this->add($id, $definition); return $definition->setShared(true); @@ -43,7 +38,7 @@ public function addShared(string $id, $definition): DefinitionInterface public function has(string $id): bool { - foreach ($this->getIterator() as $definition) { + foreach ($this as $definition) { if ($id === $definition->getAlias()) { return true; } @@ -54,7 +49,7 @@ public function has(string $id): bool public function hasTag(string $tag): bool { - foreach ($this->getIterator() as $definition) { + foreach ($this as $definition) { if ($definition->hasTag($tag)) { return true; } @@ -65,7 +60,7 @@ public function hasTag(string $tag): bool public function getDefinition(string $id): DefinitionInterface { - foreach ($this->getIterator() as $definition) { + foreach ($this as $definition) { if ($id === $definition->getAlias()) { return $definition->setContainer($this->getContainer()); } @@ -74,12 +69,12 @@ public function getDefinition(string $id): DefinitionInterface throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id)); } - public function resolve(string $id) + public function resolve(string $id): mixed { return $this->getDefinition($id)->resolve(); } - public function resolveNew(string $id) + public function resolveNew(string $id): mixed { return $this->getDefinition($id)->resolveNew(); } @@ -88,7 +83,7 @@ public function resolveTagged(string $tag): array { $arrayOf = []; - foreach ($this->getIterator() as $definition) { + foreach ($this as $definition) { if ($definition->hasTag($tag)) { $arrayOf[] = $definition->setContainer($this->getContainer())->resolve(); } @@ -101,7 +96,7 @@ public function resolveTaggedNew(string $tag): array { $arrayOf = []; - foreach ($this->getIterator() as $definition) { + foreach ($this as $definition) { if ($definition->hasTag($tag)) { $arrayOf[] = $definition->setContainer($this->getContainer())->resolveNew(); } diff --git a/src/Definition/DefinitionAggregateInterface.php b/src/Definition/DefinitionAggregateInterface.php index 16a4060..918a0f9 100644 --- a/src/Definition/DefinitionAggregateInterface.php +++ b/src/Definition/DefinitionAggregateInterface.php @@ -9,13 +9,13 @@ interface DefinitionAggregateInterface extends ContainerAwareInterface, IteratorAggregate { - public function add(string $id, $definition): DefinitionInterface; - public function addShared(string $id, $definition): DefinitionInterface; + public function add(string $id, mixed $definition): DefinitionInterface; + public function addShared(string $id, mixed $definition): DefinitionInterface; public function getDefinition(string $id): DefinitionInterface; public function has(string $id): bool; public function hasTag(string $tag): bool; - public function resolve(string $id); - public function resolveNew(string $id); + public function resolve(string $id): mixed; + public function resolveNew(string $id): mixed; public function resolveTagged(string $tag): array; public function resolveTaggedNew(string $tag): array; } diff --git a/src/Definition/DefinitionInterface.php b/src/Definition/DefinitionInterface.php index ce6c267..bc32f04 100644 --- a/src/Definition/DefinitionInterface.php +++ b/src/Definition/DefinitionInterface.php @@ -14,11 +14,11 @@ public function addMethodCall(string $method, array $args = []): DefinitionInter public function addMethodCalls(array $methods = []): DefinitionInterface; public function addTag(string $tag): DefinitionInterface; public function getAlias(): string; - public function getConcrete(); + public function getConcrete(): mixed; public function hasTag(string $tag): bool; public function isShared(): bool; - public function resolve(); - public function resolveNew(); + public function resolve(): mixed; + public function resolveNew(): mixed; public function setAlias(string $id): DefinitionInterface; public function setConcrete($concrete): DefinitionInterface; public function setShared(bool $shared): DefinitionInterface; diff --git a/src/Inflector/Inflector.php b/src/Inflector/Inflector.php index b8f0675..29bb49a 100644 --- a/src/Inflector/Inflector.php +++ b/src/Inflector/Inflector.php @@ -13,29 +13,17 @@ class Inflector implements ArgumentResolverInterface, InflectorInterface use ArgumentResolverTrait; use ContainerAwareTrait; - /** - * @var string - */ - protected $type; - /** * @var callable|null */ protected $callback; - /** - * @var array - */ - protected $methods = []; - - /** - * @var array - */ - protected $properties = []; - - public function __construct(string $type, callable $callback = null) - { - $this->type = $type; + public function __construct( + protected string $type, + protected $methods = [], + protected $properties = [], + ?callable $callback = null + ) { $this->callback = $callback; } diff --git a/src/Inflector/InflectorAggregate.php b/src/Inflector/InflectorAggregate.php index c719332..1ff79e8 100644 --- a/src/Inflector/InflectorAggregate.php +++ b/src/Inflector/InflectorAggregate.php @@ -14,18 +14,18 @@ class InflectorAggregate implements InflectorAggregateInterface /** * @var Inflector[] */ - protected $inflectors = []; + protected array $inflectors = []; public function add(string $type, callable $callback = null): Inflector { - $inflector = new Inflector($type, $callback); + $inflector = new Inflector($type, callback: $callback); $this->inflectors[] = $inflector; return $inflector; } public function inflect($object) { - foreach ($this->getIterator() as $inflector) { + foreach ($this as $inflector) { $type = $inflector->getType(); if ($object instanceof $type) { diff --git a/src/ReflectionContainer.php b/src/ReflectionContainer.php index 6971ee1..3864ab1 100644 --- a/src/ReflectionContainer.php +++ b/src/ReflectionContainer.php @@ -17,24 +17,18 @@ class ReflectionContainer implements ArgumentResolverInterface, ContainerInterfa use ArgumentResolverTrait; use ContainerAwareTrait; - /** - * @var boolean - */ - protected $cacheResolutions; - /** * @var array */ - protected $cache = []; + protected array $cache = []; - public function __construct(bool $cacheResolutions = false) + public function __construct(protected bool $cacheResolutions = false) { - $this->cacheResolutions = $cacheResolutions; } - public function get($id, array $args = []) + public function get(string $id, array $args = []) { - if ($this->cacheResolutions === true && array_key_exists($id, $this->cache)) { + if (true === $this->cacheResolutions && array_key_exists($id, $this->cache)) { return $this->cache[$id]; } @@ -65,7 +59,7 @@ public function get($id, array $args = []) return $resolution; } - public function has($id): bool + public function has(string $id): bool { return class_exists($id); } diff --git a/src/ServiceProvider/BootableServiceProviderInterface.php b/src/ServiceProvider/BootableServiceProviderInterface.php index 1b4be06..befcc48 100644 --- a/src/ServiceProvider/BootableServiceProviderInterface.php +++ b/src/ServiceProvider/BootableServiceProviderInterface.php @@ -10,7 +10,7 @@ interface BootableServiceProviderInterface extends ServiceProviderInterface * Method will be invoked on registration of a service provider implementing * this interface. Provides ability for eager loading of Service Providers. * - * @return void + * @return never */ public function boot(): void; } diff --git a/src/ServiceProvider/ServiceProviderAggregate.php b/src/ServiceProvider/ServiceProviderAggregate.php index a790b3f..f4de9ba 100644 --- a/src/ServiceProvider/ServiceProviderAggregate.php +++ b/src/ServiceProvider/ServiceProviderAggregate.php @@ -15,12 +15,12 @@ class ServiceProviderAggregate implements ServiceProviderAggregateInterface /** * @var ServiceProviderInterface[] */ - protected $providers = []; + protected array $providers = []; /** * @var array */ - protected $registered = []; + protected array $registered = []; public function add(ServiceProviderInterface $provider): ServiceProviderAggregateInterface { @@ -38,10 +38,10 @@ public function add(ServiceProviderInterface $provider): ServiceProviderAggregat return $this; } - public function provides(string $service): bool + public function provides(string $id): bool { - foreach ($this->getIterator() as $provider) { - if ($provider->provides($service)) { + foreach ($this as $provider) { + if ($provider->provides($id)) { return true; } } @@ -62,7 +62,7 @@ public function register(string $service): void ); } - foreach ($this->getIterator() as $provider) { + foreach ($this as $provider) { if (in_array($provider->getIdentifier(), $this->registered, true)) { continue; } diff --git a/tests/Inflector/InflectorTest.php b/tests/Inflector/InflectorTest.php index 6c19310..ed2a9a7 100644 --- a/tests/Inflector/InflectorTest.php +++ b/tests/Inflector/InflectorTest.php @@ -142,7 +142,7 @@ public function setBar($bar): void $bar = new class { }; - $inflector = new Inflector('Type', function ($object) use ($bar) { + $inflector = new Inflector('Type', callback: function ($object) use ($bar) { $object->setBar($bar); });