Skip to content

Commit

Permalink
Update code style and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratadox committed Dec 2, 2018
1 parent ebf4224 commit cef38ce
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 81 deletions.
12 changes: 5 additions & 7 deletions src/ArrayAdapter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -9,7 +7,7 @@

final class ArrayAdapter implements ArrayAccess
{
protected $container;
private $container;

/**
* @param ContainerInterface $container
Expand All @@ -23,7 +21,7 @@ public function __construct(ContainerInterface $container)
* @param string $offset
* @return bool
*/
public function offsetExists($offset) : bool
public function offsetExists($offset): bool
{
return $this->container->has($offset);
}
Expand All @@ -43,15 +41,15 @@ public function offsetGet($offset)
* @param string $offset
* @param Closure $value
*/
public function offsetSet($offset, $value) : void
public function offsetSet($offset, $value): void
{
$this->container->set($offset, $value);
}

/**
* @param string $offset
*/
public function offsetUnset($offset) : void
public function offsetUnset($offset): void
{
$this->container->forget($offset);
}
Expand Down
27 changes: 12 additions & 15 deletions src/AutoWiring.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

use Closure;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use ReflectionClass as Reflected;
use ReflectionException;
use ReflectionType;

final class AutoWiring implements ContainerInterface, PsrContainerInterface
final class AutoWiring implements ContainerInterface
{
private $container;
private $links;
Expand All @@ -21,12 +18,12 @@ private function __construct(ContainerInterface $container, array $links)
$this->links = $links;
}

public static function the(ContainerInterface $container) : self
public static function the(ContainerInterface $container): ContainerInterface
{
return new self($container, []);
}

public function link(string $interface, string $class) : self
public function link(string $interface, string $class) : ContainerInterface
{
if (!is_a($class, $interface, true)) {
throw InvalidServiceType::serviceIsNotOfType($class, $interface);
Expand All @@ -42,24 +39,24 @@ public function get($theService, string $type = '')
return $this->container->get($theService);
}

public function has($theService) : bool
public function has($theService): bool
{
return class_exists($theService)
|| isset($this->links[$theService])
|| $this->container->has($theService);
}

public function set(string $service, Closure $factory, bool $cache = true)
public function set(string $service, Closure $factory, bool $cache = true): void
{
$this->container->set($service, $factory, $cache);
}

public function forget(string $service)
public function forget(string $service): void
{
$this->container->forget($service);
}

private function resolve(string $service)
private function resolve(string $service): void
{
try {
$this->resolveThe(new Reflected($service));
Expand All @@ -68,7 +65,7 @@ private function resolve(string $service)
}
}

private function resolveThe(Reflected $service)
private function resolveThe(Reflected $service): void
{
if ($service->isAbstract() || $service->isInterface()) {
$this->resolveAbstract($service);
Expand All @@ -77,7 +74,7 @@ private function resolveThe(Reflected $service)
}
}

private function resolveAbstract(Reflected $service) : void
private function resolveAbstract(Reflected $service): void
{
$name = $service->getName();
if (!isset($this->links[$name])) {
Expand All @@ -90,7 +87,7 @@ private function resolveAbstract(Reflected $service) : void
});
}

private function resolveClass(Reflected $service) : void
private function resolveClass(Reflected $service): void
{
$name = $service->getName();
$constructor = $service->getConstructor();
Expand All @@ -110,7 +107,7 @@ private function resolveClass(Reflected $service) : void
});
}

private function handleDependency(ReflectionType $theType) : string
private function handleDependency(ReflectionType $theType): string
{
if ($theType->isBuiltin()) {
throw CannotAutoWireBuiltInTypes::cannotResolve($theType);
Expand Down
6 changes: 2 additions & 4 deletions src/CannotAutoWireBuiltInTypes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -9,7 +7,7 @@

final class CannotAutoWireBuiltInTypes extends InvalidArgument implements InvalidServiceDefinition
{
public static function cannotResolve(ReflectionType $type)
public static function cannotResolve(ReflectionType $type): InvalidServiceDefinition
{
return new self(sprintf(
'Cannot autowire the %s argument.',
Expand Down
9 changes: 4 additions & 5 deletions src/CannotResolveAbstractType.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -10,8 +8,9 @@

final class CannotResolveAbstractType extends RuntimeException implements InvalidServiceDefinition
{
public static function noLinkDefinedFor(Reflected $theAbstractType)
{
public static function noLinkDefinedFor(
Reflected $theAbstractType
): InvalidServiceDefinition {
return new self(sprintf(
'Cannot resolve the %s `%s`. Consider adding an AutoWire link.',
$theAbstractType->isInterface() ? 'interface' : 'abstract class',
Expand Down
46 changes: 22 additions & 24 deletions src/Container.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

use Closure;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Throwable;
use Psr\Container\NotFoundExceptionInterface as NotFound;

final class Container implements ContainerInterface, PsrContainerInterface
final class Container implements ContainerInterface
{
protected $remember = [];
protected $factoryFor = [];
protected $mustReload = [];
protected $isCurrentlyResolving = [];
private $remember = [];
private $factoryFor = [];
private $mustReload = [];
private $isCurrentlyResolving = [];

public function get($theService)
{
$this->mustKnowAbout($theService);

if ($this->hasNotYetLoaded($theService) or $this->mustReload[$theService]) {
if ($this->mustReload[$theService] || $this->hasNotYetLoaded($theService)) {
$this->remember[$theService] = $this->load($theService);
}

return $this->remember[$theService];
}

public function has($theService) : bool
public function has($theService): bool
{
return isset($this->factoryFor[$theService]);
}
Expand All @@ -35,18 +31,19 @@ public function set(
string $theService,
Closure $producingTheService,
bool $cache = true
) : void
{
): void {
$this->remember[$theService] = null;
$this->factoryFor[$theService] = $producingTheService;
$this->mustReload[$theService] = !$cache;
}

public function forget(string $theService) : void
public function forget(string $theService): void
{
unset($this->remember[$theService]);
unset($this->factoryFor[$theService]);
unset($this->mustReload[$theService]);
unset(
$this->remember[$theService],
$this->factoryFor[$theService],
$this->mustReload[$theService]
);
}

/** @throws InvalidServiceDefinition */
Expand All @@ -56,7 +53,6 @@ private function load(string $theService)
throw DependenciesCannotBeCircular::loopDetectedIn($theService);
}
$this->isCurrentlyResolving[$theService] = true;

$makeTheService = $this->factoryFor[$theService];
try {
return $makeTheService();
Expand All @@ -67,15 +63,17 @@ private function load(string $theService)
}
}

private function hasNotYetLoaded(string $theService) : bool
private function hasNotYetLoaded(string $theService): bool
{
return !isset($this->remember[$theService]);
}

/** @throws ServiceNotFound */
private function mustKnowAbout(string $theService) : void
/** @throws NotFound */
private function mustKnowAbout(string $theService): void
{
if ($this->has($theService)) return;
if ($this->has($theService)) {
return;
}
throw ServiceNotFound::noServiceNamed($theService);
}
}
11 changes: 5 additions & 6 deletions src/ContainerInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

declare(strict_types=1);

namespace Stratadox\Di;

use Closure;
use Psr\Container\ContainerInterface as PsrContainerInterface;

interface ContainerInterface
interface ContainerInterface extends PsrContainerInterface
{
/**
* Register a service to the container.
Expand All @@ -17,7 +16,7 @@ interface ContainerInterface
*
* @return void
*/
public function set(string $service, Closure $factory, bool $cache = true);
public function set(string $service, Closure $factory, bool $cache = true): void;

/**
* Retrieve a service from the container.
Expand All @@ -38,7 +37,7 @@ public function get($service);
*
* @return boolean Whether or not the service exists
*/
public function has($service) : bool;
public function has($service): bool;

/**
* Remove a service from the container.
Expand All @@ -47,5 +46,5 @@ public function has($service) : bool;
*
* @return void
*/
public function forget(string $service);
public function forget(string $service): void;
}
6 changes: 2 additions & 4 deletions src/DependenciesCannotBeCircular.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -9,7 +7,7 @@

final class DependenciesCannotBeCircular extends RuntimeException implements InvalidServiceDefinition
{
public static function loopDetectedIn($serviceName) : DependenciesCannotBeCircular
public static function loopDetectedIn($serviceName): DependenciesCannotBeCircular
{
return new static(sprintf(
'Circular dependency loop detected in factory `%s`.',
Expand Down
7 changes: 2 additions & 5 deletions src/InvalidFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -13,8 +11,7 @@ final class InvalidFactory extends RuntimeException implements InvalidServiceDef
public static function threwException(
string $serviceName,
Throwable $exception
) : Throwable
{
): InvalidServiceDefinition {
return new static(sprintf(
'Service `%s` was configured incorrectly and could not be created: %s',
$serviceName,
Expand Down
2 changes: 0 additions & 2 deletions src/InvalidServiceDefinition.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Stratadox\Di;

use Psr\Container\ContainerExceptionInterface;
Expand Down
7 changes: 2 additions & 5 deletions src/InvalidServiceType.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -12,8 +10,7 @@ final class InvalidServiceType extends RuntimeException implements InvalidServic
public static function serviceIsNotOfType(
string $serviceName,
string $expectedType
) : InvalidServiceDefinition
{
): InvalidServiceDefinition {
return new static(sprintf(
'Service %s is not of type %s',
$serviceName,
Expand Down
6 changes: 2 additions & 4 deletions src/ServiceNotFound.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

declare(strict_types=1);
<?php declare(strict_types=1);

namespace Stratadox\Di;

Expand All @@ -10,7 +8,7 @@

final class ServiceNotFound extends RuntimeException implements NotFound
{
public static function noServiceNamed(string $serviceName) : NotFound
public static function noServiceNamed(string $serviceName): NotFound
{
return new static(sprintf(
'No service registered for %s',
Expand Down

0 comments on commit cef38ce

Please sign in to comment.