Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
jshannon63 authored and StyleCIBot committed Apr 2, 2018
1 parent 7ddc607 commit 290d8cb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 44 deletions.
32 changes: 18 additions & 14 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
class Container implements ContainerInterface
{
/**
* Global container instance
* Global container instance.
* @var Container
*/
protected static $container;

/**
* Binding ID
* Binding ID.
* @var int
*/
protected $bindingId = 0;
Expand All @@ -61,7 +61,7 @@ class Container implements ContainerInterface
protected $cache = [];

/**
* Array of aliased cached bindings keyed by alias
* Array of aliased cached bindings keyed by alias.
*
* @var array
*/
Expand Down Expand Up @@ -114,7 +114,7 @@ public function bind($abstract, $concrete = null, $singleton = false): void
// If the concrete class is not a closure, then check if concrete is an object.
// If so, set instance and singleton mode, then use reflection on the class,
// cache it in the binding and set the full concrete name.
if (!$binding['concrete'] instanceof Closure) {
if (! $binding['concrete'] instanceof Closure) {
if (is_object($binding['concrete'])) {
$binding['singleton'] = true;
$this->prepareBindingClosure($abstract, $binding['concrete']);
Expand Down Expand Up @@ -143,7 +143,7 @@ public function bind($abstract, $concrete = null, $singleton = false): void
public function resolve($id)
{
// make sure the binding exists
if (!isset($this->bindings[$id])) {
if (! isset($this->bindings[$id])) {
throw new NotFoundException('Binding '.$id.' not found.');
}

Expand All @@ -169,6 +169,7 @@ public function resolve($id)
public function make($id, ...$args)
{
$this->bind($id, ...$args);

return $this->resolve($id);
}

Expand All @@ -185,7 +186,7 @@ private function create($id)
{
// Check if the binding already exists. Forces a bind
// whenever create is called internally.
if (!isset($this->bindings[$id])) {
if (! isset($this->bindings[$id])) {
$this->bind($id);
}

Expand Down Expand Up @@ -234,14 +235,15 @@ private function processDependencies(&$binding): void
{
// Let's use the ReflectionClass object previously generated during binding.
// If it's not instantiable, then we can do nothing... throw exception.
if (!$binding['reflector']->isInstantiable()) {
if (! $binding['reflector']->isInstantiable()) {
throw new ContainerException($binding['concrete'].' can not be instantiated.');
}

// Get the class constructor and see what we have. If there is no constructor,
// then return an empty array of dependencies.
if (!$constructor = $binding['reflector']->getConstructor()) {
if (! $constructor = $binding['reflector']->getConstructor()) {
$binding['dependencies'] = [];

return;
}

Expand Down Expand Up @@ -284,6 +286,7 @@ private function resolveParameters($parameters): array
$dependencies[$key]['value'] = $dependency->name;
}
}

return $dependencies;
}

Expand All @@ -300,6 +303,7 @@ private function prepareBindingClosure($id, $blueprint, $dependencies = null): C
if ($this->bindings[$id]['singleton']) {
return $this->prepareSingletonBindingClosure($id, $blueprint, $dependencies);
}

return $this->preparePrototypeBindingClosure($id, $blueprint, $dependencies);
}

Expand All @@ -320,12 +324,14 @@ private function preparePrototypeBindingClosure($id, $blueprint, $dependencies):
$dependencies[$key] = $dependency();
}
}

return $blueprint->newInstanceArgs($dependencies);
};
}
if ($blueprint instanceof Closure) {
return $this->cache[$id] = $blueprint;
}

return $this->cache[$id] = function () use ($blueprint) {
return new $blueprint;
};
Expand Down Expand Up @@ -385,7 +391,7 @@ public function instance($abstract, $instance)
}

/**
* Create an alias to an existing cached binding
* Create an alias to an existing cached binding.
*
* @param string $alias
* @param string $binding
Expand All @@ -402,7 +408,7 @@ public function alias($alias, $binding)
}

/**
* Create empty binding in the registry and set ID
* Create empty binding in the registry and set ID.
*
* @param string $id
*/
Expand All @@ -412,7 +418,7 @@ private function initializeBinding($id)
}

/**
* Remove all traces of the specified binding
* Remove all traces of the specified binding.
*
* @param string $id
*/
Expand Down Expand Up @@ -465,7 +471,6 @@ public function getBindings(): array
* @return object
* @throws NotFoundException
* @throws ContainerException
*
*/
public function get($id)
{
Expand Down Expand Up @@ -512,7 +517,7 @@ public function offsetExists($offset): bool
public function offsetGet($offset)
{
// if the binding does not exist then throw exception.
if (!$this->has($offset)) {
if (! $this->has($offset)) {
throw new NotFoundException('Binding '.$offset.' not found.');
}

Expand Down Expand Up @@ -543,4 +548,3 @@ public function offsetUnset($offset): void
unset($this->bindings[$offset]);
}
}

6 changes: 2 additions & 4 deletions src/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Jshannon63\Cobalt;

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

/**
* Cobalt Service Container Interface.
Expand Down Expand Up @@ -60,7 +60,7 @@ public function make($id, ...$args);
public function instance($abstract, $instance);

/**
* Create an alias to an existing cached binding
* Create an alias to an existing cached binding.
*
* @param string $alias
* @param string $binding
Expand Down Expand Up @@ -104,7 +104,6 @@ public function getBindings();
* @return object
* @throws NotFoundException
* @throws ContainerException
*
*/
public function get($id);

Expand Down Expand Up @@ -159,4 +158,3 @@ public function offsetSet($offset, $value);
*/
public function offsetUnset($offset);
}

52 changes: 26 additions & 26 deletions tests/unit/containerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Yaz
{
public function sayHello()
{
return "Hello";
return 'Hello';
}
}

Expand Down Expand Up @@ -190,7 +190,8 @@ public function testSimpleBinding()
$this->assertTrue($app->has('Tests\Fiz'));
}

public function testBindingOfClassWithoutConstructor(){
public function testBindingOfClassWithoutConstructor()
{
$app = new Container();

$app->bind('Tests\Yaz');
Expand All @@ -199,7 +200,7 @@ public function testBindingOfClassWithoutConstructor(){

$yaz = $app['Tests\Yaz'];

$this->assertInstanceOf(Yaz::class,$yaz);
$this->assertInstanceOf(Yaz::class, $yaz);
}

// check dependency injection through closure
Expand Down Expand Up @@ -407,19 +408,18 @@ public function testVariadicConstructor()
{
$app = new Container('cached');

$app->bind('Yib', function(){
return new Yib('I','am','variadic',new Yaz,function(){
return "Closure";
$app->bind('Yib', function () {
return new Yib('I', 'am', 'variadic', new Yaz, function () {
return 'Closure';
});
});

$value = $app['Yib']();
$this->assertEquals(['I','am','variadic',new Yaz,function(){
return "closure";
$this->assertEquals(['I', 'am', 'variadic', new Yaz, function () {
return 'closure';
}], $value);
$this->assertEquals('Hello', $value[3]->sayHello());
$this->assertEquals('Closure', $value[4]());

}

public function testDirectBindingOfObject()
Expand All @@ -439,68 +439,68 @@ public function testTimeToCreate()
$timer['start'] = microtime(true);

$app = new Container($mode);
$timer['create'] = microtime(true)-$timer['start'];
$timer['create'] = microtime(true) - $timer['start'];

$app->bind(Foo::class);
$timer['bind'] = ((microtime(true)-$timer['start'])-$timer['create']);
$timer['bind'] = ((microtime(true) - $timer['start']) - $timer['create']);

$foo = $app->resolve(Foo::class);
$timer['resolve'] = ((microtime(true)-$timer['start'])-$timer['bind']);
$timer['resolve'] = ((microtime(true) - $timer['start']) - $timer['bind']);

$foo2 = $app->resolve(Foo::class);
$timer['resolve2'] = ((microtime(true)-$timer['start'])-$timer['resolve']);
$timer['resolve2'] = ((microtime(true) - $timer['start']) - $timer['resolve']);

$foo3 = $app->resolve(Foo::class);
$timer['resolve3'] = ((microtime(true)-$timer['start'])-$timer['resolve2']);
$timer['resolve3'] = ((microtime(true) - $timer['start']) - $timer['resolve2']);

$foo4 = $app->resolve(Foo::class);
$timer['resolve4'] = ((microtime(true)-$timer['start'])-$timer['resolve3']);
$timer['resolve4'] = ((microtime(true) - $timer['start']) - $timer['resolve3']);

for ($cnt = 0;$cnt < 100000;$cnt++) {
for ($cnt = 0; $cnt < 100000; $cnt++) {
$fooX = $app->resolve(Foo::class);
}
$timer['resolveX'] = ((microtime(true)-$timer['start'])-$timer['resolve4']);
$timer['resolveX'] = ((microtime(true) - $timer['start']) - $timer['resolve4']);

$timer['total'] = (microtime(true)-$timer['start']);
$timer['total'] = (microtime(true) - $timer['start']);

unset($timer['start']);

foreach ($timer as $key => $entry) {
$timer[$key] = number_format(1e6 * $entry, 2);
}

$timer['memory'] = ((memory_get_peak_usage()/1000)."Kbytes");
$timer['memory'] = ((memory_get_peak_usage() / 1000).'Kbytes');

if($mode == 'shared'){
if ($mode == 'shared') {
$this->assertSame($foo->bar()->baz(), $foo2->bar()->baz());
$this->assertSame($foo2->bar()->baz(), $foo3->bar()->baz());
$this->assertSame($foo3->bar()->baz(), $foo4->bar()->baz());
$this->assertSame($foo4->bar()->baz(), $fooX->bar()->baz());
} else{
} else {
$this->assertNotSame($foo->bar()->baz(), $foo2->bar()->baz());
$this->assertNotSame($foo2->bar()->baz(), $foo3->bar()->baz());
$this->assertNotSame($foo3->bar()->baz(), $foo4->bar()->baz());
$this->assertNotSame($foo4->bar()->baz(), $fooX->bar()->baz());
}
}

public function testDeprecatedMakeCommandStillWorks(){
public function testDeprecatedMakeCommandStillWorks()
{
$app = new Container();

$test = $app->make(Baz::class);

$this->assertEquals('default words', $test->sayWords());
}

public function testAliasCreation(){
public function testAliasCreation()
{
$app = new Container();

$test = $app->make(Baz::class);

$app->alias('aliased_test',Baz::class);
$app->alias('aliased_test', Baz::class);

$this->assertEquals('default words', $app['aliased_test']->sayWords());

}
}

0 comments on commit 290d8cb

Please sign in to comment.