Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jakewhiteley committed May 16, 2022
2 parents 58140b8 + 432a723 commit 3be42e0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
26 changes: 16 additions & 10 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,35 @@ private function resolveFromContainer(string $className): bool
private function resolveParams(array $params, array $args): void
{
foreach ($params as $param) {
$class = $param->getType();
$reflectionType = $param->getType();

// if the param is not a class, check $args for the value
if (\is_null($class)) {
// if the param is not type-hinted, check $args for the value
if (\is_null($reflectionType)) {
$this->resolveParam($args, $param);
continue;
}

$className = $class->getName();
$typeName = $reflectionType->getName();

// if the class exists in the container, inject it
if ($this->resolveFromContainer($className)) {
if ($this->resolveFromContainer($typeName)) {
continue;
}

$reflectionClass = new ReflectionClass($className);
if (class_exists($typeName) || interface_exists($typeName)) {
$reflectionClass = new ReflectionClass($typeName);

if ($reflectionClass->isInterface()) {
throw new ConcreteClassNotFoundException("$className is an interface with no bound implementation.");
if ($reflectionClass->isInterface()) {
throw new ConcreteClassNotFoundException("$typeName is an interface with no bound implementation.");
}

// else the param is a class, so run $this->resolve on it
$this->addToStack($this->resolve($typeName, $args));
continue;
}

// else the param is a class, so run $this->resolve on it
$this->addToStack($this->resolve($className, $args));
// It was type-hinted but not a defined class, so assume it's a primitive and try to resolve
$this->resolveParam($args, $param);
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/ContainerArrayAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
namespace Hodl;

use ArrayAccess;
use Closure;

/**
* ArrayAccess implementaion for Hodl\Container
* ArrayAccess implementation for Hodl\Container
*/
class ContainerArrayAccess implements ArrayAccess
{
/**
* Sets the value at specified offset.
*
* @param string $offset The key to set
* @param \closure $value The value to set
* @param Closure $value The value to set
*/
public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$this->add($offset, $value);
}
Expand All @@ -26,7 +27,7 @@ public function offsetSet($offset, $value)
* @param string $offset The key to set
* @return bool
*/
public function offsetExists($offset)
public function offsetExists($offset): bool
{
return $this->has($offset);
}
Expand All @@ -35,9 +36,9 @@ public function offsetExists($offset)
* Gets the value at specified offset.
*
* @param string $offset The key to get
* @return object|closure $value The object instance or closure if a factory class
* @return object $value The object instance or closure if a factory class
*/
public function offsetGet($offset)
public function offsetGet($offset): object
{
return $this->get($offset);
}
Expand All @@ -46,10 +47,9 @@ public function offsetGet($offset)
* Unsets the value at specified offset.
*
* @param string $offset The key to set
* @return bool
*/
public function offsetUnset($offset)
public function offsetUnset($offset): void
{
return $this->remove($offset);
$this->remove($offset);
}
}
13 changes: 13 additions & 0 deletions tests/Classes/PrimitivesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Hodl\Tests\Classes;

class PrimitivesTest
{
public $foo = null;

public function __construct(string $string = 'not_set')
{
$this->foo = $string;
}
}
13 changes: 13 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Hodl\Tests\Classes\NeedsResolving;
use Hodl\Tests\Classes\NeedsServiceAndConstructorParams;
use Hodl\Tests\Classes\NoConstructor;
use Hodl\Tests\Classes\PrimitivesTest;
use Hodl\Tests\Classes\Resolver;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -205,6 +206,18 @@ public function an_object_can_be_resolved_explicitly_with_params(): void
$this->assertEquals('has_been_set', $doesntNeedResolving->foo);
}

/**
* @test
*/
public function an_object_can_be_resolved_explicitly_with_typehinted_primitive_params(): void
{
$hodl = new Container();

$doesntNeedResolving = $hodl->resolve(PrimitivesTest::class, ['string' => 'has_been_set']);

$this->assertEquals('has_been_set', $doesntNeedResolving->foo);
}

/**
* @test
*/
Expand Down

0 comments on commit 3be42e0

Please sign in to comment.