Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiator attempts to clone classes that are not cloneable #7

Closed
davedevelopment opened this issue Oct 4, 2014 · 6 comments · Fixed by #8
Closed

Instantiator attempts to clone classes that are not cloneable #7

davedevelopment opened this issue Oct 4, 2014 · 6 comments · Fixed by #8
Assignees
Labels
Milestone

Comments

@davedevelopment
Copy link
Contributor

I don't know how this might be solved, but some builtin classes are not cloneable:

PHP 5.6.0 (cli) (built: Oct  4 2014 20:41:37)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
<?php

require "vendor/autoload.php";

$instantiator = new \Doctrine\Instantiator\Instantiator();
$instance = $instantiator->instantiate('MongoCollection');
PHP Fatal error:  Trying to clone an uncloneable object of class MongoCollection in /Users/davem/src/instantiator/src/Doctrine/Instantiator/Instantiator.php on line 74
PHP Stack trace:
PHP   1. {main}() /Users/davem/src/instantiator/test.php:0
PHP   2. Doctrine\Instantiator\Instantiator->instantiate() /Users/davem/src/instantiator/test.php:6

I'm not aware of any way in which to tell if something isn't cloneable...

@whatthejeff
Copy link
Contributor

There's isCloneable(), but it's not 100% reliable (see https://bugs.php.net/bug.php?id=53967).

@Ocramius
Copy link
Member

Ocramius commented Oct 4, 2014

Interesting, I wasn't aware of it at all, but indeed, isCloneable should be checked as well.

Are there internal classes in the SPL that are not cloneable? I don't want to require ext-mongo just for a single test :-)

@davedevelopment
Copy link
Contributor Author

@Ocramius about to send a PR, using XMLReader

@whatthejeff
Copy link
Contributor

There are a handful:

php -r'print_r(array_filter(get_declared_classes(),function($n){$c=new ReflectionClass($n);return !$c->isCloneable();}));'
Array
(
    [1] => Exception
    [2] => ErrorException
    [9] => SQLite3
    [10] => SQLite3Stmt
    [11] => SQLite3Result
    [12] => DOMException
    [44] => LogicException
    [45] => BadFunctionCallException
    [46] => BadMethodCallException
    [47] => DomainException
    [48] => InvalidArgumentException
    [49] => LengthException
    [50] => OutOfRangeException
    [51] => RuntimeException
    [52] => OutOfBoundsException
    [53] => OverflowException
    [54] => RangeException
    [55] => UnderflowException
    [56] => UnexpectedValueException
    [57] => RecursiveIteratorIterator
    [58] => IteratorIterator
    [59] => FilterIterator
    [60] => RecursiveFilterIterator
    [61] => CallbackFilterIterator
    [62] => RecursiveCallbackFilterIterator
    [63] => ParentIterator
    [64] => LimitIterator
    [65] => CachingIterator
    [66] => RecursiveCachingIterator
    [67] => NoRewindIterator
    [68] => AppendIterator
    [69] => InfiniteIterator
    [70] => RegexIterator
    [71] => RecursiveRegexIterator
    [73] => RecursiveTreeIterator
    [87] => SplHeap
    [98] => mysqli_sql_exception
    [99] => mysqli_driver
    [100] => mysqli
    [101] => mysqli_warning
    [102] => mysqli_result
    [103] => mysqli_stmt
    [104] => PDOException
    [107] => PDORow
    [108] => PharException
    [112] => ReflectionException
    [114] => ReflectionFunctionAbstract
    [115] => ReflectionFunction
    [116] => ReflectionParameter
    [117] => ReflectionMethod
    [118] => ReflectionClass
    [119] => ReflectionObject
    [120] => ReflectionProperty
    [121] => ReflectionExtension
    [122] => ReflectionZendExtension
    [125] => SNMP
    [126] => SNMPException
    [130] => SoapFault
    [133] => tidy
    [134] => tidyNode
    [135] => XMLReader
    [136] => XMLWriter
    [137] => XSLTProcessor
    [138] => ZipArchive
)

@whatthejeff
Copy link
Contributor

Just be sure to test for the ones I've linked in my isCloneable() bug.

@davedevelopment
Copy link
Contributor Author

@whatthejeff you need to exclude those with a __clone method

davem@dutch:instantiator:avoid-cloning-uncloneables$ php -r'print_r(array_filter(get_declared_classes(),function($n){$c=new ReflectionClass($n);return !$c->isCloneable() && !$c->hasMethod("__clone");}));'
Array
(
    [4] => Generator
    [11] => SQLite3
    [12] => SQLite3Stmt
    [13] => SQLite3Result
    [60] => RecursiveIteratorIterator
    [61] => IteratorIterator
    [62] => FilterIterator
    [63] => RecursiveFilterIterator
    [64] => CallbackFilterIterator
    [65] => RecursiveCallbackFilterIterator
    [66] => ParentIterator
    [67] => LimitIterator
    [68] => CachingIterator
    [69] => RecursiveCachingIterator
    [70] => NoRewindIterator
    [71] => AppendIterator
    [72] => InfiniteIterator
    [73] => RegexIterator
    [74] => RecursiveRegexIterator
    [76] => RecursiveTreeIterator
    [90] => SplHeap
    [102] => mysqli_driver
    [103] => mysqli
    [104] => mysqli_warning
    [105] => mysqli_result
    [106] => mysqli_stmt
    [110] => PDORow
    [134] => tidy
    [135] => tidyNode
    [136] => XMLReader
    [137] => XMLWriter
    [138] => XSLTProcessor
    [139] => ZipArchive
    [140] => MongoClient
    [141] => Mongo
    [142] => MongoDB
    [143] => MongoCollection
    [144] => MongoCursor
    [145] => MongoCommandCursor
    [146] => MongoGridFS
    [148] => MongoGridFSCursor
    [153] => MongoId
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants