Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/239-reflection-factory-default-values'
Browse files Browse the repository at this point in the history
Close #243
Fixes #239
  • Loading branch information
weierophinney committed Jan 29, 2018
2 parents e11039a + 9a2c497 commit 41c3c4e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 3.3.2 - TBD

### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#243](https://github.com/zendframework/zend-servicemanager/pull/243) provides
a fix to the `ReflectionBasedAbstractFactory` to resolve type-hinted arguments
with default values to their default values if no matching type is found in
the container.

## 3.3.1 - 2017-11-27

### Added
Expand Down
10 changes: 8 additions & 2 deletions src/AbstractFactory/ReflectionBasedAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
$type = $parameter->getClass()->getName();
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;

if (! $container->has($type)) {
if ($container->has($type)) {
return $container->get($type);
}

if (! $parameter->isOptional()) {
throw new ServiceNotFoundException(sprintf(
'Unable to create service "%s"; unable to resolve parameter "%s" using type hint "%s"',
$requestedName,
Expand All @@ -227,6 +231,8 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
));
}

return $container->get($type);
// Type not available in container, but the value is optional and has a
// default defined.
return $parameter->getDefaultValue();
}
}
17 changes: 17 additions & 0 deletions test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ZendTest\ServiceManager\AbstractFactory;

use ArrayAccess;
use Interop\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
Expand Down Expand Up @@ -154,4 +155,20 @@ public function testFactoryWillUseDefaultValueWhenPresentForScalarArgument()
$this->assertInstanceOf(TestAsset\ClassWithScalarDependencyDefiningDefaultValue::class, $instance);
$this->assertEquals('bar', $instance->foo);
}

/**
* @see https://github.com/zendframework/zend-servicemanager/issues/239
*/
public function testFactoryWillUseDefaultValueForTypeHintedArgument()
{
$this->container->has('config')->willReturn(false);
$this->container->has(ArrayAccess::class)->willReturn(false);
$factory = new ReflectionBasedAbstractFactory();
$instance = $factory(
$this->container->reveal(),
TestAsset\ClassWithTypehintedDefaultValue::class
);
$this->assertInstanceOf(TestAsset\ClassWithTypehintedDefaultValue::class, $instance);
$this->assertNull($instance->value);
}
}
20 changes: 20 additions & 0 deletions test/AbstractFactory/TestAsset/ClassWithTypehintedDefaultValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @see https://github.com/zendframework/zend-2018 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-2018/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;

use ArrayAccess;

class ClassWithTypehintedDefaultValue
{
public $value;

public function __construct(ArrayAccess $value = null)
{
$this->value = null;
}
}

0 comments on commit 41c3c4e

Please sign in to comment.