comparison vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Invocation/Static.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /*
3 * This file is part of the PHPUnit_MockObject package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 use SebastianBergmann\Exporter\Exporter;
12
13 /**
14 * Represents a static invocation.
15 *
16 * @since Class available since Release 1.0.0
17 */
18 class PHPUnit_Framework_MockObject_Invocation_Static implements PHPUnit_Framework_MockObject_Invocation, PHPUnit_Framework_SelfDescribing
19 {
20 /**
21 * @var array
22 */
23 protected static $uncloneableExtensions = array(
24 'mysqli' => true,
25 'SQLite' => true,
26 'sqlite3' => true,
27 'tidy' => true,
28 'xmlwriter' => true,
29 'xsl' => true
30 );
31
32 /**
33 * @var array
34 */
35 protected static $uncloneableClasses = array(
36 'Closure',
37 'COMPersistHelper',
38 'IteratorIterator',
39 'RecursiveIteratorIterator',
40 'SplFileObject',
41 'PDORow',
42 'ZipArchive'
43 );
44
45 /**
46 * @var string
47 */
48 public $className;
49
50 /**
51 * @var string
52 */
53 public $methodName;
54
55 /**
56 * @var array
57 */
58 public $parameters;
59
60 /**
61 * @param string $className
62 * @param string $methodname
63 * @param array $parameters
64 * @param bool $cloneObjects
65 */
66 public function __construct($className, $methodName, array $parameters, $cloneObjects = false)
67 {
68 $this->className = $className;
69 $this->methodName = $methodName;
70 $this->parameters = $parameters;
71
72 if (!$cloneObjects) {
73 return;
74 }
75
76 foreach ($this->parameters as $key => $value) {
77 if (is_object($value)) {
78 $this->parameters[$key] = $this->cloneObject($value);
79 }
80 }
81 }
82
83 /**
84 * @return string
85 */
86 public function toString()
87 {
88 $exporter = new Exporter;
89
90 return sprintf(
91 '%s::%s(%s)',
92 $this->className,
93 $this->methodName,
94 implode(
95 ', ',
96 array_map(
97 array($exporter, 'shortenedExport'),
98 $this->parameters
99 )
100 )
101 );
102 }
103
104 /**
105 * @param object $original
106 * @return object
107 */
108 protected function cloneObject($original)
109 {
110 $cloneable = null;
111 $object = new ReflectionObject($original);
112
113 // Check the blacklist before asking PHP reflection to work around
114 // https://bugs.php.net/bug.php?id=53967
115 if ($object->isInternal() &&
116 isset(self::$uncloneableExtensions[$object->getExtensionName()])) {
117 $cloneable = false;
118 }
119
120 if ($cloneable === null) {
121 foreach (self::$uncloneableClasses as $class) {
122 if ($original instanceof $class) {
123 $cloneable = false;
124 break;
125 }
126 }
127 }
128
129 if ($cloneable === null && method_exists($object, 'isCloneable')) {
130 $cloneable = $object->isCloneable();
131 }
132
133 if ($cloneable === null && $object->hasMethod('__clone')) {
134 $method = $object->getMethod('__clone');
135 $cloneable = $method->isPublic();
136 }
137
138 if ($cloneable === null) {
139 $cloneable = true;
140 }
141
142 if ($cloneable) {
143 try {
144 return clone $original;
145 } catch (Exception $e) {
146 return $original;
147 }
148 } else {
149 return $original;
150 }
151 }
152 }