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

Commit 16dd464

Browse files
committed
Merge branch 'hotfix/268' into develop
Forward port #268
2 parents b7e56ba + 8d83c41 commit 16dd464

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
@@ -54,7 +54,9 @@ All notable changes to this project will be documented in this file, in reverse
5454

5555
### Fixed
5656

57-
- Nothing.
57+
- [#268](https://github.com/zendframework/zend-servicemanager/pull/268) Fixes
58+
ReflectionBasedAbstractFactory trying to instantiate classes with private
59+
constructors
5860

5961
## 3.3.2 - 2018-01-29
6062

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;
@@ -138,7 +138,14 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
138138
*/
139139
public function canCreate(ContainerInterface $container, $requestedName)
140140
{
141-
return class_exists($requestedName);
141+
return class_exists($requestedName) && $this->canCallConstructor($requestedName);
142+
}
143+
144+
private function canCallConstructor(string $requestedName) : bool
145+
{
146+
$constructor = (new ReflectionClass($requestedName))->getConstructor();
147+
148+
return $constructor === null || $constructor->isPublic();
142149
}
143150

144151
/**

test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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 PHPUnit\Framework\TestCase;
12+
use Prophecy\Prophecy\ObjectProphecy;
1213
use Psr\Container\ContainerInterface;
1314
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
1415
use Zend\ServiceManager\Exception\ServiceNotFoundException;
@@ -17,6 +18,9 @@
1718

1819
class ReflectionBasedAbstractFactoryTest extends TestCase
1920
{
21+
/** @var ContainerInterface|ObjectProphecy */
22+
private $container;
23+
2024
public function setUp()
2125
{
2226
$this->container = $this->prophesize(ContainerInterface::class);
@@ -38,6 +42,28 @@ public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedNam
3842
self::assertFalse($factory->canCreate($this->container->reveal(), $requestedName));
3943
}
4044

45+
public function testCanCreateReturnsFalseWhenConstructorIsPrivate() : void
46+
{
47+
self::assertFalse(
48+
(new ReflectionBasedAbstractFactory())->canCreate(
49+
$this->container->reveal(),
50+
TestAsset\ClassWithPrivateConstructor::class
51+
),
52+
'ReflectionBasedAbstractFactory should not be able to instantiate a class with a private constructor'
53+
);
54+
}
55+
56+
public function testCanCreateReturnsTrueWhenClassHasNoConstructor() : void
57+
{
58+
self::assertTrue(
59+
(new ReflectionBasedAbstractFactory())->canCreate(
60+
$this->container->reveal(),
61+
TestAsset\ClassWithNoConstructor::class
62+
),
63+
'ReflectionBasedAbstractFactory should be able to instantiate a class without a constructor'
64+
);
65+
}
66+
4167
public function testFactoryInstantiatesClassDirectlyIfItHasNoConstructor()
4268
{
4369
$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)