Skip to content

Commit

Permalink
Small language features update
Browse files Browse the repository at this point in the history
  • Loading branch information
philipobenito committed Mar 14, 2024
1 parent 43cf67d commit 96de03d
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 97 deletions.
40 changes: 9 additions & 31 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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)) {

Check failure on line 158 in src/Container.php

View workflow job for this annotation

GitHub Actions / PHPStan

Result of && is always true.
throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
}
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait ContainerAwareTrait
/**
* @var ?DefinitionContainerInterface
*/
protected $container;
protected ?DefinitionContainerInterface $container = null;

public function setContainer(DefinitionContainerInterface $container): ContainerAwareInterface
{
Expand Down
3 changes: 1 addition & 2 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
};
use League\Container\ContainerAwareTrait;
use League\Container\Exception\ContainerException;
use League\Container\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use ReflectionClass;

Expand Down Expand Up @@ -78,7 +77,7 @@ public function isShared(): bool
return $this->shared;
}

public function getConcrete()
public function getConcrete(): mixed
{
return $this->concrete;
}
Expand Down
27 changes: 11 additions & 16 deletions src/Definition/DefinitionAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,15 +30,15 @@ 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);
}

public function has(string $id): bool
{
foreach ($this->getIterator() as $definition) {
foreach ($this as $definition) {
if ($id === $definition->getAlias()) {
return true;
}
Expand All @@ -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;
}
Expand All @@ -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());
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Definition/DefinitionAggregateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions src/Definition/DefinitionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 6 additions & 18 deletions src/Inflector/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Inflector/InflectorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 5 additions & 11 deletions src/ReflectionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider/BootableServiceProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions src/ServiceProvider/ServiceProviderAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Inflector/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 96de03d

Please sign in to comment.