Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from Ocramius/hotfix/backport-doctrine/instant…
Browse files Browse the repository at this point in the history
…iator#7-fix

Hotfix/backport doctrine/instantiator#7 fix
  • Loading branch information
Ocramius committed Oct 4, 2014
2 parents e24a121 + 8936cdf commit 51bbc28
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Instantiator/Instantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ public function buildFactory($className)
};
}

/**
* Checks if a class is cloneable
*
* @internal
* @private
*
* This method is only exposed as public because of PHP 5.3 compatibility. Do not
* use this method in your own code
*
* @param ReflectionClass $class
*
* @return bool
*/
public function isSafeToClone(ReflectionClass $class)
{
if (method_exists($class, 'isCloneable') && ! $class->isCloneable()) {
return false;
}

// not cloneable if it implements `__clone`, as we want to avoid calling it
return ! $class->hasMethod('__clone');
}

/**
* @param string $className
*
Expand Down Expand Up @@ -258,16 +281,15 @@ private function getInstantiatorsMap()
private function getCloneablesMap()
{
$cachedInstantiators = $this->getInstantiatorsMap();
$that = $this;

return self::$cachedCloneables = self::$cachedCloneables
?: new CallbackLazyMap(function ($className) use ($cachedInstantiators) {
?: new CallbackLazyMap(function ($className) use ($cachedInstantiators, $that) {
/* @var $factory Closure */
$factory = $cachedInstantiators->$className;
$instance = $factory();
$reflection = new ReflectionClass($instance);

// not cloneable if it implements `__clone`, as we want to avoid calling it
if ($reflection->hasMethod('__clone')) {
if (! $that->isSafeToClone(new ReflectionClass($className))) {
return null;
}

Expand Down
1 change: 1 addition & 0 deletions tests/InstantiatorTest/InstantiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function getInstantiableClasses()
array('InstantiatorTestAsset\\SimpleSerializableAsset'),
array('InstantiatorTestAsset\\PharExceptionAsset'),
array('InstantiatorTestAsset\\UnCloneableAsset'),
array('InstantiatorTestAsset\\XMLReaderAsset'),
);

if (\PHP_VERSION_ID === 50429 || \PHP_VERSION_ID === 50513) {
Expand Down
42 changes: 42 additions & 0 deletions tests/InstantiatorTestAsset/XMLReaderAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace InstantiatorTestAsset;

use BadMethodCallException;
use XMLReader;

/**
* Test asset that extends an internal PHP class
*
* @author Dave Marshall <dave@atst.io>
* @link https://github.com/doctrine/instantiator/pull/8
*/
class XMLReaderAsset extends XMLReader
{
/**
* Constructor - should not be called
*
* @throws BadMethodCallException
*/
public function __construct()
{
throw new BadMethodCallException('Not supposed to be called!');
}
}

0 comments on commit 51bbc28

Please sign in to comment.