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/268'
Browse files Browse the repository at this point in the history
Close #268
  • Loading branch information
Xerkus committed Dec 20, 2018
2 parents feb2cc9 + 8d83c41 commit b91d25f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

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

## 3.3.2 - 2018-01-29

Expand Down
15 changes: 11 additions & 4 deletions src/AbstractFactory/ReflectionBasedAbstractFactory.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
*/

namespace Zend\ServiceManager\AbstractFactory;
Expand Down Expand Up @@ -134,7 +134,14 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
*/
public function canCreate(ContainerInterface $container, $requestedName)
{
return class_exists($requestedName);
return class_exists($requestedName) && $this->canCallConstructor($requestedName);
}

private function canCallConstructor($requestedName)
{
$constructor = (new ReflectionClass($requestedName))->getConstructor();

return $constructor === null || $constructor->isPublic();
}

/**
Expand Down
32 changes: 29 additions & 3 deletions test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\ServiceManager\AbstractFactory;

use ArrayAccess;
use Interop\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
use Zend\ServiceManager\Exception\ServiceNotFoundException;

class ReflectionBasedAbstractFactoryTest extends TestCase
{
/** @var ContainerInterface|ObjectProphecy */
private $container;

public function setUp()
{
$this->container = $this->prophesize(ContainerInterface::class);
Expand All @@ -36,6 +40,28 @@ public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedNam
$this->assertFalse($factory->canCreate($this->container->reveal(), $requestedName));
}

public function testCanCreateReturnsFalseWhenConstructorIsPrivate()
{
self::assertFalse(
(new ReflectionBasedAbstractFactory())->canCreate(
$this->container->reveal(),
TestAsset\ClassWithPrivateConstructor::class
),
'ReflectionBasedAbstractFactory should not be able to instantiate a class with a private constructor'
);
}

public function testCanCreateReturnsTrueWhenClassHasNoConstructor()
{
self::assertTrue(
(new ReflectionBasedAbstractFactory())->canCreate(
$this->container->reveal(),
TestAsset\ClassWithNoConstructor::class
),
'ReflectionBasedAbstractFactory should be able to instantiate a class without a constructor'
);
}

public function testFactoryInstantiatesClassDirectlyIfItHasNoConstructor()
{
$factory = new ReflectionBasedAbstractFactory();
Expand Down
15 changes: 15 additions & 0 deletions test/AbstractFactory/TestAsset/ClassWithPrivateConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* @see https://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-servicemanager/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;

class ClassWithPrivateConstructor
{
private function __construct()
{
}
}

0 comments on commit b91d25f

Please sign in to comment.