Skip to content

Commit

Permalink
Merge pull request #8 from northwoods/feature/php7-strict
Browse files Browse the repository at this point in the history
Move to PHP7 syntax and features
  • Loading branch information
shadowhand committed Dec 19, 2017
2 parents 036ba97 + e529b38 commit 28e1720
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 119 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Expand Up @@ -5,17 +5,16 @@ cache:
- $HOME/.composer/cache

php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly

env: MINIMUM_VERSIONS=false

matrix:
fast_finish: true
include:
- php: 5.6
- php: 7.1
env: MINIMUM_VERSIONS=true
allow_failures:
- php: nightly
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.0.0]

### Changed

- Use PHP 7.1+ syntax and features; all classes and interfaces are now strictly typed

## [1.2.1]

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -6,12 +6,12 @@
"psr/container-implementation": "^1.0"
},
"require": {
"php": ">=7.1",
"rdlowrey/auryn": "^1.1",
"psr/container": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"eloquent/phony-phpunit": "^1.0"
"eloquent/phony-phpunit": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 5 additions & 4 deletions src/Config/ContainerConfig.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container\Config;

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

class ContainerConfig implements InjectorConfig
{
public function apply(Injector $injector)
public function apply(Injector $injector): void
{
// Optional: Declare a single container instance.
// Assume that the container will be shared
$injector->share(ContainerInterface::class);

// Use InjectorContainer as the implementation of ContainerInterface.
// Use the injector as the preferred container implementation
$injector->alias(ContainerInterface::class, InjectorContainer::class);

// InjectorContainer will wrap this Injector instance.
// The container will wrap this injector
$injector->define(InjectorContainer::class, [':injector' => $injector]);
}
}
86 changes: 22 additions & 64 deletions src/Config/ServiceConfig.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container\Config;

Expand All @@ -8,31 +9,22 @@

class ServiceConfig implements InjectorConfig
{
/**
* @var array
*/
/** @var array */
private $config;

/**
* @var bool
*/
/** @var bool */
private $sharedByDefault = true;

/**
* @var array
*/
/** @var array */
private $shared = [];

public function __construct(array $config)
{
$this->config = $config;
}

/**
* @param Injector $injector
* @return void
*/
public function apply(Injector $injector)
// InjectorConfig
public function apply(Injector $injector): void
{
// Enable or disable sharing all services by default.
if (isset($this->config['shared_by_default'])) {
Expand Down Expand Up @@ -72,11 +64,7 @@ public function apply(Injector $injector)
}
}

/**
* @param string $name
* @return bool
*/
private function isShared($name)
private function isShared(string $name): bool
{
if (isset($this->shared[$name])) {
return (bool) $this->shared[$name];
Expand All @@ -85,11 +73,7 @@ private function isShared($name)
return $this->sharedByDefault;
}

/**
* @param array $services
* @return void
*/
private function applyAliases(Injector $injector, array $services)
private function applyAliases(Injector $injector, array $services): void
{
foreach ($services as $name => $object) {
$injector->alias($name, $object);
Expand All @@ -99,11 +83,7 @@ private function applyAliases(Injector $injector, array $services)
}
}

/**
* @param array $services
* @return void
*/
private function applyDelegates(Injector $injector, array $services)
private function applyDelegates(Injector $injector, array $services): void
{
foreach ($services as $name => $object) {
$injector->delegate($name, $object);
Expand All @@ -113,11 +93,7 @@ private function applyDelegates(Injector $injector, array $services)
}
}

/**
* @param array $delegators
* @return void
*/
private function applyDelegators(Injector $injector, array $delegators)
private function applyDelegators(Injector $injector, array $delegators): void
{
// https://github.com/rdlowrey/auryn#prepares-and-setter-injection
foreach ($delegators as $service => $prepares) {
Expand All @@ -129,27 +105,20 @@ private function applyDelegators(Injector $injector, array $delegators)
}

/**
* Create a chained prepare()
*
* @param string $service
* @param string[] $delegators
* @return callable
* Create a chained prepare function.
*/
private function createDelegator($service, array $delegators)
private function createDelegator(string $service, array $delegators): callable
{
// Prepare the service by calling each delegator with the result of the previous.
return function ($instance, $injector) use ($service, $delegators) {
return function ($instance, Injector $injector) use ($service, $delegators) {
return array_reduce($delegators, $this->delegatorReducer($injector, $service), $instance);
};
}

/**
* Create a reducer for a chained prepare()
*
* @param string $service
* @return callable
* Create a reducer for a chained prepare function.
*/
private function delegatorReducer(Injector $injector, $service)
private function delegatorReducer(Injector $injector, string $service): callable
{
// https://docs.zendframework.com/zend-expressive/features/container/delegator-factories/
return function ($instance, $delegator) use ($injector, $service) {
Expand All @@ -162,43 +131,32 @@ private function delegatorReducer(Injector $injector, $service)
}

/**
* Curry the delegator to only require a container
*
* @param callable $delegator that will be ultimately called
* @param string $service name of service being prepared
* @param callable $callable that returns the instance
* @return callable
* Curry the delegator to only require a container.
*/
private function curryDelegator(callable $delegator, $service, callable $callable)
private function curryDelegator(callable $delegator, string $service, callable $callable): callable
{
return static function (ContainerInterface $container) use ($delegator, $service, $callable) {
return $delegator($container, $service, $callable);
};
}

/**
* Returns a function that always returns the same value
*
* Also known as a "kestrel" or "k combinator".
*
* @param mixed $x
* @return callable
* Create a k combinator for a value.
*/
private function k($x)
private function k($x): callable
{
return static function () use ($x) {
return $x;
};
}

/**
* @param array $values
* @return callable[]
* Create k combinators for multiple values.
*/
private function kAll(array $values)
private function kAll(array $values): array
{
return array_map(
function ($x) {
function ($x): callable {
return $this->k($x);
},
$values
Expand Down
12 changes: 3 additions & 9 deletions src/ContainerException.php
@@ -1,21 +1,15 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container;

use Auryn\InjectorException;
use Psr\Container\ContainerExceptionInterface;
use RuntimeException;

class ContainerException extends RuntimeException implements
class ContainerException extends \RuntimeException implements
ContainerExceptionInterface
{
/**
* @param string $class
* @param InjectorException $previous
*
* @return static
*/
public static function couldNotMake($class, InjectorException $previous)
public static function couldNotMake(string $class, InjectorException $previous): ContainerException
{
return new static("Could not make $class", 0, $previous);
}
Expand Down
16 changes: 5 additions & 11 deletions src/InjectorBuilder.php
@@ -1,14 +1,13 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container;

use Auryn\Injector;

class InjectorBuilder
{
/**
* @var InjectorConfig[]
*/
/** @var InjectorConfig[] */
private $configs;

/**
Expand All @@ -20,11 +19,9 @@ public function __construct(array $configs = [])
}

/**
* Build the injector using the provided configuration
*
* @return Injector
* Build the injector using the provided configuration.
*/
public function build(Injector $injector = null)
public function build(Injector $injector = null): Injector
{
if (empty($injector)) {
$injector = new Injector();
Expand All @@ -36,10 +33,7 @@ public function build(Injector $injector = null)
return $injector;
}

/**
* @return callable
*/
private function applicator(Injector $injector)
private function applicator(Injector $injector): callable
{
return static function (InjectorConfig $config) use ($injector) {
return $config->apply($injector);
Expand Down
7 changes: 3 additions & 4 deletions src/InjectorConfig.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container;

Expand All @@ -7,9 +8,7 @@
interface InjectorConfig
{
/**
* Apply configuration to the injector
*
* @return void
* Apply configuration to an injector.
*/
public function apply(Injector $injector);
public function apply(Injector $injector): void;
}
14 changes: 3 additions & 11 deletions src/InjectorContainer.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container;

Expand All @@ -10,9 +11,7 @@ class InjectorContainer implements ContainerInterface
{
const I_ALL = 31;

/**
* @var Injector
*/
/** @var Injector */
private $injector;

public function __construct(Injector $injector)
Expand Down Expand Up @@ -40,14 +39,7 @@ public function has($id)
return class_exists($id) || $this->hasReference($id);
}

/**
* Check the injector has a reference
*
* @param string $id
*
* @return bool
*/
private function hasReference($id)
private function hasReference(string $id): bool
{
// https://github.com/rdlowrey/auryn/issues/157
$details = $this->injector->inspect($id, self::I_ALL);
Expand Down
11 changes: 3 additions & 8 deletions src/NotFoundException.php
@@ -1,19 +1,14 @@
<?php
declare(strict_types=1);

namespace Northwoods\Container;

use InvalidArgumentException;
use Psr\Container\NotFoundExceptionInterface;

class NotFoundException extends InvalidArgumentException implements
class NotFoundException extends \InvalidArgumentException implements
NotFoundExceptionInterface
{
/**
* @param string $class
*
* @return static
*/
public static function classDoesNotExist($class)
public static function classDoesNotExist(string $class): NotFoundException
{
return new static("Class $class does not exist");
}
Expand Down

0 comments on commit 28e1720

Please sign in to comment.