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

Commit ff2ce4d

Browse files
asgrimXerkus
authored andcommitted
ReflectionBasedAbstractFactory should not be expected to instantiate private constructors
1 parent feb2cc9 commit ff2ce4d

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/AbstractFactory/ReflectionBasedAbstractFactory.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @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)
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license http://framework.zend.com/license/new-bsd New BSD License
66
*/
77

@@ -131,10 +131,27 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
131131

132132
/**
133133
* {@inheritDoc}
134+
* @throws \ReflectionException
134135
*/
135136
public function canCreate(ContainerInterface $container, $requestedName)
136137
{
137-
return class_exists($requestedName);
138+
return class_exists($requestedName) && $this->canCallConstructor($requestedName);
139+
}
140+
141+
/**
142+
* @param string $requestedName
143+
* @return bool
144+
* @throws \ReflectionException
145+
*/
146+
private function canCallConstructor(string $requestedName) : bool
147+
{
148+
$constructor = (new ReflectionClass($requestedName))->getConstructor();
149+
150+
if ($constructor === null) {
151+
return true;
152+
}
153+
154+
return $constructor->isPublic();
138155
}
139156

140157
/**

test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @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)
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license http://framework.zend.com/license/new-bsd New BSD License
66
*/
77

@@ -15,6 +15,8 @@
1515

1616
class ReflectionBasedAbstractFactoryTest extends TestCase
1717
{
18+
private $container;
19+
1820
public function setUp()
1921
{
2022
$this->container = $this->prophesize(ContainerInterface::class);
@@ -36,6 +38,20 @@ public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedNam
3638
$this->assertFalse($factory->canCreate($this->container->reveal(), $requestedName));
3739
}
3840

41+
/**
42+
* @throws \ReflectionException
43+
*/
44+
public function testCanCreateReturnsFalseWhenConstructorIsPrivate() : void
45+
{
46+
self::assertFalse(
47+
(new ReflectionBasedAbstractFactory())->canCreate(
48+
$this->container->reveal(),
49+
TestAsset\ClassWithPrivateConstructor::class
50+
),
51+
'ReflectionBasedAbstractFactory should not be able to instantiate a class with a private constructor'
52+
);
53+
}
54+
3955
public function testFactoryInstantiatesClassDirectlyIfItHasNoConstructor()
4056
{
4157
$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+
* @link http://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 http://framework.zend.com/license/new-bsd 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)