PageRenderTime 90ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/VarDumper/Cloner/VarCloner.php

https://github.com/FabienD/symfony
PHP | 248 lines | 209 code | 20 blank | 19 comment | 47 complexity | ac70bf924393918e417fe84732a3c0fe MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Cloner;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. */
  14. class VarCloner extends AbstractCloner
  15. {
  16. private static string $gid;
  17. private static array $arrayCache = [];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function doClone(mixed $var): array
  22. {
  23. $len = 1; // Length of $queue
  24. $pos = 0; // Number of cloned items past the minimum depth
  25. $refsCounter = 0; // Hard references counter
  26. $queue = [[$var]]; // This breadth-first queue is the return value
  27. $hardRefs = []; // Map of original zval ids to stub objects
  28. $objRefs = []; // Map of original object handles to their stub object counterpart
  29. $objects = []; // Keep a ref to objects to ensure their handle cannot be reused while cloning
  30. $resRefs = []; // Map of original resource handles to their stub object counterpart
  31. $values = []; // Map of stub objects' ids to original values
  32. $maxItems = $this->maxItems;
  33. $maxString = $this->maxString;
  34. $minDepth = $this->minDepth;
  35. $currentDepth = 0; // Current tree depth
  36. $currentDepthFinalIndex = 0; // Final $queue index for current tree depth
  37. $minimumDepthReached = 0 === $minDepth; // Becomes true when minimum tree depth has been reached
  38. $cookie = (object) []; // Unique object used to detect hard references
  39. $a = null; // Array cast for nested structures
  40. $stub = null; // Stub capturing the main properties of an original item value
  41. // or null if the original value is used directly
  42. $gid = self::$gid ??= md5(random_bytes(6)); // Unique string used to detect the special $GLOBALS variable
  43. $arrayStub = new Stub();
  44. $arrayStub->type = Stub::TYPE_ARRAY;
  45. $fromObjCast = false;
  46. for ($i = 0; $i < $len; ++$i) {
  47. // Detect when we move on to the next tree depth
  48. if ($i > $currentDepthFinalIndex) {
  49. ++$currentDepth;
  50. $currentDepthFinalIndex = $len - 1;
  51. if ($currentDepth >= $minDepth) {
  52. $minimumDepthReached = true;
  53. }
  54. }
  55. $refs = $vals = $queue[$i];
  56. foreach ($vals as $k => $v) {
  57. // $v is the original value or a stub object in case of hard references
  58. $zvalRef = ($r = \ReflectionReference::fromArrayElement($vals, $k)) ? $r->getId() : null;
  59. if ($zvalRef) {
  60. $vals[$k] = &$stub; // Break hard references to make $queue completely
  61. unset($stub); // independent from the original structure
  62. if (null !== $vals[$k] = $hardRefs[$zvalRef] ?? null) {
  63. $v = $vals[$k];
  64. if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  65. ++$v->value->refCount;
  66. }
  67. ++$v->refCount;
  68. continue;
  69. }
  70. $vals[$k] = new Stub();
  71. $vals[$k]->value = $v;
  72. $vals[$k]->handle = ++$refsCounter;
  73. $hardRefs[$zvalRef] = $vals[$k];
  74. }
  75. // Create $stub when the original value $v cannot be used directly
  76. // If $v is a nested structure, put that structure in array $a
  77. switch (true) {
  78. case null === $v:
  79. case \is_bool($v):
  80. case \is_int($v):
  81. case \is_float($v):
  82. continue 2;
  83. case \is_string($v):
  84. if ('' === $v) {
  85. continue 2;
  86. }
  87. if (!preg_match('//u', $v)) {
  88. $stub = new Stub();
  89. $stub->type = Stub::TYPE_STRING;
  90. $stub->class = Stub::STRING_BINARY;
  91. if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
  92. $stub->cut = $cut;
  93. $stub->value = substr($v, 0, -$cut);
  94. } else {
  95. $stub->value = $v;
  96. }
  97. } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = mb_strlen($v, 'UTF-8') - $maxString) {
  98. $stub = new Stub();
  99. $stub->type = Stub::TYPE_STRING;
  100. $stub->class = Stub::STRING_UTF8;
  101. $stub->cut = $cut;
  102. $stub->value = mb_substr($v, 0, $maxString, 'UTF-8');
  103. } else {
  104. continue 2;
  105. }
  106. $a = null;
  107. break;
  108. case \is_array($v):
  109. if (!$v) {
  110. continue 2;
  111. }
  112. $stub = $arrayStub;
  113. $stub->class = array_is_list($v) ? Stub::ARRAY_INDEXED : Stub::ARRAY_ASSOC;
  114. $a = $v;
  115. break;
  116. case \is_object($v):
  117. if (empty($objRefs[$h = spl_object_id($v)])) {
  118. $stub = new Stub();
  119. $stub->type = Stub::TYPE_OBJECT;
  120. $stub->class = \get_class($v);
  121. $stub->value = $v;
  122. $stub->handle = $h;
  123. $a = $this->castObject($stub, 0 < $i);
  124. if ($v !== $stub->value) {
  125. if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  126. break;
  127. }
  128. $stub->handle = $h = spl_object_id($stub->value);
  129. }
  130. $stub->value = null;
  131. if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  132. $stub->cut = \count($a);
  133. $a = null;
  134. }
  135. }
  136. if (empty($objRefs[$h])) {
  137. $objRefs[$h] = $stub;
  138. $objects[] = $v;
  139. } else {
  140. $stub = $objRefs[$h];
  141. ++$stub->refCount;
  142. $a = null;
  143. }
  144. break;
  145. default: // resource
  146. if (empty($resRefs[$h = (int) $v])) {
  147. $stub = new Stub();
  148. $stub->type = Stub::TYPE_RESOURCE;
  149. if ('Unknown' === $stub->class = @get_resource_type($v)) {
  150. $stub->class = 'Closed';
  151. }
  152. $stub->value = $v;
  153. $stub->handle = $h;
  154. $a = $this->castResource($stub, 0 < $i);
  155. $stub->value = null;
  156. if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  157. $stub->cut = \count($a);
  158. $a = null;
  159. }
  160. }
  161. if (empty($resRefs[$h])) {
  162. $resRefs[$h] = $stub;
  163. } else {
  164. $stub = $resRefs[$h];
  165. ++$stub->refCount;
  166. $a = null;
  167. }
  168. break;
  169. }
  170. if ($a) {
  171. if (!$minimumDepthReached || 0 > $maxItems) {
  172. $queue[$len] = $a;
  173. $stub->position = $len++;
  174. } elseif ($pos < $maxItems) {
  175. if ($maxItems < $pos += \count($a)) {
  176. $a = \array_slice($a, 0, $maxItems - $pos, true);
  177. if ($stub->cut >= 0) {
  178. $stub->cut += $pos - $maxItems;
  179. }
  180. }
  181. $queue[$len] = $a;
  182. $stub->position = $len++;
  183. } elseif ($stub->cut >= 0) {
  184. $stub->cut += \count($a);
  185. $stub->position = 0;
  186. }
  187. }
  188. if ($arrayStub === $stub) {
  189. if ($arrayStub->cut) {
  190. $stub = [$arrayStub->cut, $arrayStub->class => $arrayStub->position];
  191. $arrayStub->cut = 0;
  192. } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  193. $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
  194. } else {
  195. self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = [$arrayStub->class => $arrayStub->position];
  196. }
  197. }
  198. if (!$zvalRef) {
  199. $vals[$k] = $stub;
  200. } else {
  201. $hardRefs[$zvalRef]->value = $stub;
  202. }
  203. }
  204. if ($fromObjCast) {
  205. $fromObjCast = false;
  206. $refs = $vals;
  207. $vals = [];
  208. $j = -1;
  209. foreach ($queue[$i] as $k => $v) {
  210. foreach ([$k => true] as $gk => $gv) {
  211. }
  212. if ($gk !== $k) {
  213. $vals = (object) $vals;
  214. $vals->{$k} = $refs[++$j];
  215. $vals = (array) $vals;
  216. } else {
  217. $vals[$k] = $refs[++$j];
  218. }
  219. }
  220. }
  221. $queue[$i] = $vals;
  222. }
  223. foreach ($values as $h => $v) {
  224. $hardRefs[$h] = $v;
  225. }
  226. return $queue;
  227. }
  228. }