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

Commit 41c3c4e

Browse files
committed
Merge branch 'hotfix/239-reflection-factory-default-values'
Close #243 Fixes #239
2 parents e11039a + 9a2c497 commit 41c3c4e

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22

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

5+
## 3.3.2 - TBD
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Nothing.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- [#243](https://github.com/zendframework/zend-servicemanager/pull/243) provides
26+
a fix to the `ReflectionBasedAbstractFactory` to resolve type-hinted arguments
27+
with default values to their default values if no matching type is found in
28+
the container.
29+
530
## 3.3.1 - 2017-11-27
631

732
### Added

src/AbstractFactory/ReflectionBasedAbstractFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
218218
$type = $parameter->getClass()->getName();
219219
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;
220220

221-
if (! $container->has($type)) {
221+
if ($container->has($type)) {
222+
return $container->get($type);
223+
}
224+
225+
if (! $parameter->isOptional()) {
222226
throw new ServiceNotFoundException(sprintf(
223227
'Unable to create service "%s"; unable to resolve parameter "%s" using type hint "%s"',
224228
$requestedName,
@@ -227,6 +231,8 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
227231
));
228232
}
229233

230-
return $container->get($type);
234+
// Type not available in container, but the value is optional and has a
235+
// default defined.
236+
return $parameter->getDefaultValue();
231237
}
232238
}

test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace ZendTest\ServiceManager\AbstractFactory;
99

10+
use ArrayAccess;
1011
use Interop\Container\ContainerInterface;
1112
use PHPUnit\Framework\TestCase;
1213
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
@@ -154,4 +155,20 @@ public function testFactoryWillUseDefaultValueWhenPresentForScalarArgument()
154155
$this->assertInstanceOf(TestAsset\ClassWithScalarDependencyDefiningDefaultValue::class, $instance);
155156
$this->assertEquals('bar', $instance->foo);
156157
}
158+
159+
/**
160+
* @see https://github.com/zendframework/zend-servicemanager/issues/239
161+
*/
162+
public function testFactoryWillUseDefaultValueForTypeHintedArgument()
163+
{
164+
$this->container->has('config')->willReturn(false);
165+
$this->container->has(ArrayAccess::class)->willReturn(false);
166+
$factory = new ReflectionBasedAbstractFactory();
167+
$instance = $factory(
168+
$this->container->reveal(),
169+
TestAsset\ClassWithTypehintedDefaultValue::class
170+
);
171+
$this->assertInstanceOf(TestAsset\ClassWithTypehintedDefaultValue::class, $instance);
172+
$this->assertNull($instance->value);
173+
}
157174
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-2018 for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-2018/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;
9+
10+
use ArrayAccess;
11+
12+
class ClassWithTypehintedDefaultValue
13+
{
14+
public $value;
15+
16+
public function __construct(ArrayAccess $value = null)
17+
{
18+
$this->value = null;
19+
}
20+
}

0 commit comments

Comments
 (0)