vendor/symfony/var-dumper/Cloner/VarCloner.php line 25

Open in your IDE?
  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 $gid;
  17.     private static $arrayCache = [];
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     protected function doClone($var)
  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.         $indexedArrays = [];       // Map of queue indexes that hold numerically indexed arrays
  28.         $hardRefs = [];            // Map of original zval ids to stub objects
  29.         $objRefs = [];             // Map of original object handles to their stub object counterpart
  30.         $objects = [];             // Keep a ref to objects to ensure their handle cannot be reused while cloning
  31.         $resRefs = [];             // Map of original resource handles to their stub object counterpart
  32.         $values = [];              // Map of stub objects' ids to original values
  33.         $maxItems $this->maxItems;
  34.         $maxString $this->maxString;
  35.         $minDepth $this->minDepth;
  36.         $currentDepth 0;              // Current tree depth
  37.         $currentDepthFinalIndex 0;    // Final $queue index for current tree depth
  38.         $minimumDepthReached === $minDepth// Becomes true when minimum tree depth has been reached
  39.         $cookie = (object) [];          // Unique object used to detect hard references
  40.         $a null;                      // Array cast for nested structures
  41.         $stub null;                   // Stub capturing the main properties of an original item value
  42.                                         // or null if the original value is used directly
  43.         if (!$gid self::$gid) {
  44.             $gid self::$gid md5(random_bytes(6)); // Unique string used to detect the special $GLOBALS variable
  45.         }
  46.         $arrayStub = new Stub();
  47.         $arrayStub->type Stub::TYPE_ARRAY;
  48.         $fromObjCast false;
  49.         for ($i 0$i $len; ++$i) {
  50.             // Detect when we move on to the next tree depth
  51.             if ($i $currentDepthFinalIndex) {
  52.                 ++$currentDepth;
  53.                 $currentDepthFinalIndex $len 1;
  54.                 if ($currentDepth >= $minDepth) {
  55.                     $minimumDepthReached true;
  56.                 }
  57.             }
  58.             $refs $vals $queue[$i];
  59.             if (\PHP_VERSION_ID 70200 && empty($indexedArrays[$i])) {
  60.                 // see https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
  61.                 foreach ($vals as $k => $v) {
  62.                     if (\is_int($k)) {
  63.                         continue;
  64.                     }
  65.                     foreach ([$k => true] as $gk => $gv) {
  66.                     }
  67.                     if ($gk !== $k) {
  68.                         $fromObjCast true;
  69.                         $refs $vals array_values($queue[$i]);
  70.                         break;
  71.                     }
  72.                 }
  73.             }
  74.             foreach ($vals as $k => $v) {
  75.                 // $v is the original value or a stub object in case of hard references
  76.                 if (\PHP_VERSION_ID >= 70400) {
  77.                     $zvalIsRef null !== \ReflectionReference::fromArrayElement($vals$k);
  78.                 } else {
  79.                     $refs[$k] = $cookie;
  80.                     $zvalIsRef $vals[$k] === $cookie;
  81.                 }
  82.                 if ($zvalIsRef) {
  83.                     $vals[$k] = &$stub;         // Break hard references to make $queue completely
  84.                     unset($stub);               // independent from the original structure
  85.                     if ($v instanceof Stub && isset($hardRefs[spl_object_id($v)])) {
  86.                         $vals[$k] = $refs[$k] = $v;
  87.                         if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  88.                             ++$v->value->refCount;
  89.                         }
  90.                         ++$v->refCount;
  91.                         continue;
  92.                     }
  93.                     $refs[$k] = $vals[$k] = new Stub();
  94.                     $refs[$k]->value $v;
  95.                     $h spl_object_id($refs[$k]);
  96.                     $hardRefs[$h] = &$refs[$k];
  97.                     $values[$h] = $v;
  98.                     $vals[$k]->handle = ++$refsCounter;
  99.                 }
  100.                 // Create $stub when the original value $v can not be used directly
  101.                 // If $v is a nested structure, put that structure in array $a
  102.                 switch (true) {
  103.                     case null === $v:
  104.                     case \is_bool($v):
  105.                     case \is_int($v):
  106.                     case \is_float($v):
  107.                         continue 2;
  108.                     case \is_string($v):
  109.                         if ('' === $v) {
  110.                             continue 2;
  111.                         }
  112.                         if (!preg_match('//u'$v)) {
  113.                             $stub = new Stub();
  114.                             $stub->type Stub::TYPE_STRING;
  115.                             $stub->class Stub::STRING_BINARY;
  116.                             if (<= $maxString && $cut = \strlen($v) - $maxString) {
  117.                                 $stub->cut $cut;
  118.                                 $stub->value substr($v0, -$cut);
  119.                             } else {
  120.                                 $stub->value $v;
  121.                             }
  122.                         } elseif (<= $maxString && isset($v[+ ($maxString >> 2)]) && $cut mb_strlen($v'UTF-8') - $maxString) {
  123.                             $stub = new Stub();
  124.                             $stub->type Stub::TYPE_STRING;
  125.                             $stub->class Stub::STRING_UTF8;
  126.                             $stub->cut $cut;
  127.                             $stub->value mb_substr($v0$maxString'UTF-8');
  128.                         } else {
  129.                             continue 2;
  130.                         }
  131.                         $a null;
  132.                         break;
  133.                     case \is_array($v):
  134.                         if (!$v) {
  135.                             continue 2;
  136.                         }
  137.                         $stub $arrayStub;
  138.                         $stub->class Stub::ARRAY_INDEXED;
  139.                         $j = -1;
  140.                         foreach ($v as $gk => $gv) {
  141.                             if ($gk !== ++$j) {
  142.                                 $stub->class Stub::ARRAY_ASSOC;
  143.                                 break;
  144.                             }
  145.                         }
  146.                         $a $v;
  147.                         if (Stub::ARRAY_ASSOC === $stub->class) {
  148.                             // Copies of $GLOBALS have very strange behavior,
  149.                             // let's detect them with some black magic
  150.                             if (\PHP_VERSION_ID 80100 && ($a[$gid] = true) && isset($v[$gid])) {
  151.                                 unset($v[$gid]);
  152.                                 $a = [];
  153.                                 foreach ($v as $gk => &$gv) {
  154.                                     if ($v === $gv) {
  155.                                         unset($v);
  156.                                         $v = new Stub();
  157.                                         $v->value = [$v->cut = \count($gv), Stub::TYPE_ARRAY => 0];
  158.                                         $v->handle = -1;
  159.                                         $gv = &$hardRefs[spl_object_id($v)];
  160.                                         $gv $v;
  161.                                     }
  162.                                     $a[$gk] = &$gv;
  163.                                 }
  164.                                 unset($gv);
  165.                             } else {
  166.                                 $a $v;
  167.                             }
  168.                         } elseif (\PHP_VERSION_ID 70200) {
  169.                             $indexedArrays[$len] = true;
  170.                         }
  171.                         break;
  172.                     case \is_object($v):
  173.                     case $v instanceof \__PHP_Incomplete_Class:
  174.                         if (empty($objRefs[$h spl_object_id($v)])) {
  175.                             $stub = new Stub();
  176.                             $stub->type Stub::TYPE_OBJECT;
  177.                             $stub->class = \get_class($v);
  178.                             $stub->value $v;
  179.                             $stub->handle $h;
  180.                             $a $this->castObject($stub$i);
  181.                             if ($v !== $stub->value) {
  182.                                 if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  183.                                     break;
  184.                                 }
  185.                                 $stub->handle $h spl_object_id($stub->value);
  186.                             }
  187.                             $stub->value null;
  188.                             if (<= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  189.                                 $stub->cut = \count($a);
  190.                                 $a null;
  191.                             }
  192.                         }
  193.                         if (empty($objRefs[$h])) {
  194.                             $objRefs[$h] = $stub;
  195.                             $objects[] = $v;
  196.                         } else {
  197.                             $stub $objRefs[$h];
  198.                             ++$stub->refCount;
  199.                             $a null;
  200.                         }
  201.                         break;
  202.                     default: // resource
  203.                         if (empty($resRefs[$h = (int) $v])) {
  204.                             $stub = new Stub();
  205.                             $stub->type Stub::TYPE_RESOURCE;
  206.                             if ('Unknown' === $stub->class = @get_resource_type($v)) {
  207.                                 $stub->class 'Closed';
  208.                             }
  209.                             $stub->value $v;
  210.                             $stub->handle $h;
  211.                             $a $this->castResource($stub$i);
  212.                             $stub->value null;
  213.                             if (<= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  214.                                 $stub->cut = \count($a);
  215.                                 $a null;
  216.                             }
  217.                         }
  218.                         if (empty($resRefs[$h])) {
  219.                             $resRefs[$h] = $stub;
  220.                         } else {
  221.                             $stub $resRefs[$h];
  222.                             ++$stub->refCount;
  223.                             $a null;
  224.                         }
  225.                         break;
  226.                 }
  227.                 if ($a) {
  228.                     if (!$minimumDepthReached || $maxItems) {
  229.                         $queue[$len] = $a;
  230.                         $stub->position $len++;
  231.                     } elseif ($pos $maxItems) {
  232.                         if ($maxItems $pos += \count($a)) {
  233.                             $a = \array_slice($a0$maxItems $postrue);
  234.                             if ($stub->cut >= 0) {
  235.                                 $stub->cut += $pos $maxItems;
  236.                             }
  237.                         }
  238.                         $queue[$len] = $a;
  239.                         $stub->position $len++;
  240.                     } elseif ($stub->cut >= 0) {
  241.                         $stub->cut += \count($a);
  242.                         $stub->position 0;
  243.                     }
  244.                 }
  245.                 if ($arrayStub === $stub) {
  246.                     if ($arrayStub->cut) {
  247.                         $stub = [$arrayStub->cut$arrayStub->class => $arrayStub->position];
  248.                         $arrayStub->cut 0;
  249.                     } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  250.                         $stub self::$arrayCache[$arrayStub->class][$arrayStub->position];
  251.                     } else {
  252.                         self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = [$arrayStub->class => $arrayStub->position];
  253.                     }
  254.                 }
  255.                 if ($zvalIsRef) {
  256.                     $refs[$k]->value $stub;
  257.                 } else {
  258.                     $vals[$k] = $stub;
  259.                 }
  260.             }
  261.             if ($fromObjCast) {
  262.                 $fromObjCast false;
  263.                 $refs $vals;
  264.                 $vals = [];
  265.                 $j = -1;
  266.                 foreach ($queue[$i] as $k => $v) {
  267.                     foreach ([$k => true] as $gk => $gv) {
  268.                     }
  269.                     if ($gk !== $k) {
  270.                         $vals = (object) $vals;
  271.                         $vals->{$k} = $refs[++$j];
  272.                         $vals = (array) $vals;
  273.                     } else {
  274.                         $vals[$k] = $refs[++$j];
  275.                     }
  276.                 }
  277.             }
  278.             $queue[$i] = $vals;
  279.         }
  280.         foreach ($values as $h => $v) {
  281.             $hardRefs[$h] = $v;
  282.         }
  283.         return $queue;
  284.     }
  285. }