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

Commit b91d25f

Browse files
committed
Merge branch 'hotfix/268'
Close #268
2 parents feb2cc9 + 8d83c41 commit b91d25f

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#268](https://github.com/zendframework/zend-servicemanager/pull/268) Fixes
26+
ReflectionBasedAbstractFactory trying to instantiate classes with private
27+
constructors
2628

2729
## 3.3.2 - 2018-01-29
2830

src/AbstractFactory/ReflectionBasedAbstractFactory.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
/**
3-
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4-
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5-
* @license http://framework.zend.com/license/new-bsd New BSD License
3+
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
66
*/
77

88
namespace Zend\ServiceManager\AbstractFactory;
@@ -134,7 +134,14 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
134134
*/
135135
public function canCreate(ContainerInterface $container, $requestedName)
136136
{
137-
return class_exists($requestedName);
137+
return class_exists($requestedName) && $this->canCallConstructor($requestedName);
138+
}
139+
140+
private function canCallConstructor($requestedName)
141+
{
142+
$constructor = (new ReflectionClass($requestedName))->getConstructor();
143+
144+
return $constructor === null || $constructor->isPublic();
138145
}
139146

140147
/**

test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<?php
22
/**
3-
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4-
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5-
* @license http://framework.zend.com/license/new-bsd New BSD License
3+
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
66
*/
77

88
namespace ZendTest\ServiceManager\AbstractFactory;
99

1010
use ArrayAccess;
1111
use Interop\Container\ContainerInterface;
1212
use PHPUnit\Framework\TestCase;
13+
use Prophecy\Prophecy\ObjectProphecy;
1314
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
1415
use Zend\ServiceManager\Exception\ServiceNotFoundException;
1516

1617
class ReflectionBasedAbstractFactoryTest extends TestCase
1718
{
19+
/** @var ContainerInterface|ObjectProphecy */
20+
private $container;
21+
1822
public function setUp()
1923
{
2024
$this->container = $this->prophesize(ContainerInterface::class);
@@ -36,6 +40,28 @@ public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedNam
3640
$this->assertFalse($factory->canCreate($this->container->reveal(), $requestedName));
3741
}
3842

43+
public function testCanCreateReturnsFalseWhenConstructorIsPrivate()
44+
{
45+
self::assertFalse(
46+
(new ReflectionBasedAbstractFactory())->canCreate(
47+
$this->container->reveal(),
48+
TestAsset\ClassWithPrivateConstructor::class
49+
),
50+
'ReflectionBasedAbstractFactory should not be able to instantiate a class with a private constructor'
51+
);
52+
}
53+
54+
public function testCanCreateReturnsTrueWhenClassHasNoConstructor()
55+
{
56+
self::assertTrue(
57+
(new ReflectionBasedAbstractFactory())->canCreate(
58+
$this->container->reveal(),
59+
TestAsset\ClassWithNoConstructor::class
60+
),
61+
'ReflectionBasedAbstractFactory should be able to instantiate a class without a constructor'
62+
);
63+
}
64+
3965
public function testFactoryInstantiatesClassDirectlyIfItHasNoConstructor()
4066
{
4167
$factory = new ReflectionBasedAbstractFactory();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;
9+
10+
class ClassWithPrivateConstructor
11+
{
12+
private function __construct()
13+
{
14+
}
15+
}

0 commit comments

Comments
 (0)