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

ReflectionBasedAbstractFactory should not be expected to instantiate private constructors #268

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docblock:

/** @var ContainerInterface|ObjectProphecy $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()
{
}
}