From 290d8cb770f18cda2e4a70e01ac2f20b3e327cd1 Mon Sep 17 00:00:00 2001 From: Jim Shannon Date: Mon, 2 Apr 2018 16:16:01 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Container.php | 32 ++++++++++++---------- src/ContainerInterface.php | 6 ++--- tests/unit/containerTest.php | 52 ++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/src/Container.php b/src/Container.php index 473fd53..626c83c 100644 --- a/src/Container.php +++ b/src/Container.php @@ -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; @@ -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 */ @@ -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']); @@ -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.'); } @@ -169,6 +169,7 @@ public function resolve($id) public function make($id, ...$args) { $this->bind($id, ...$args); + return $this->resolve($id); } @@ -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); } @@ -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; } @@ -284,6 +286,7 @@ private function resolveParameters($parameters): array $dependencies[$key]['value'] = $dependency->name; } } + return $dependencies; } @@ -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); } @@ -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; }; @@ -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 @@ -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 */ @@ -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 */ @@ -465,7 +471,6 @@ public function getBindings(): array * @return object * @throws NotFoundException * @throws ContainerException - * */ public function get($id) { @@ -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.'); } @@ -543,4 +548,3 @@ public function offsetUnset($offset): void unset($this->bindings[$offset]); } } - diff --git a/src/ContainerInterface.php b/src/ContainerInterface.php index e873ba7..aaf9b77 100644 --- a/src/ContainerInterface.php +++ b/src/ContainerInterface.php @@ -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. @@ -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 @@ -104,7 +104,6 @@ public function getBindings(); * @return object * @throws NotFoundException * @throws ContainerException - * */ public function get($id); @@ -159,4 +158,3 @@ public function offsetSet($offset, $value); */ public function offsetUnset($offset); } - diff --git a/tests/unit/containerTest.php b/tests/unit/containerTest.php index 8445d98..e8fe583 100644 --- a/tests/unit/containerTest.php +++ b/tests/unit/containerTest.php @@ -98,7 +98,7 @@ class Yaz { public function sayHello() { - return "Hello"; + return 'Hello'; } } @@ -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'); @@ -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 @@ -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() @@ -439,29 +439,29 @@ 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']); @@ -469,14 +469,14 @@ public function testTimeToCreate() $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()); @@ -484,7 +484,8 @@ public function testTimeToCreate() } } - public function testDeprecatedMakeCommandStillWorks(){ + public function testDeprecatedMakeCommandStillWorks() + { $app = new Container(); $test = $app->make(Baz::class); @@ -492,15 +493,14 @@ public function testDeprecatedMakeCommandStillWorks(){ $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()); - } } -