Caster.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * Helper for filtering out properties in casters.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @final
  18. */
  19. class Caster
  20. {
  21. public const EXCLUDE_VERBOSE = 1;
  22. public const EXCLUDE_VIRTUAL = 2;
  23. public const EXCLUDE_DYNAMIC = 4;
  24. public const EXCLUDE_PUBLIC = 8;
  25. public const EXCLUDE_PROTECTED = 16;
  26. public const EXCLUDE_PRIVATE = 32;
  27. public const EXCLUDE_NULL = 64;
  28. public const EXCLUDE_EMPTY = 128;
  29. public const EXCLUDE_NOT_IMPORTANT = 256;
  30. public const EXCLUDE_STRICT = 512;
  31. public const EXCLUDE_UNINITIALIZED = 1024;
  32. public const PREFIX_VIRTUAL = "\0~\0";
  33. public const PREFIX_DYNAMIC = "\0+\0";
  34. public const PREFIX_PROTECTED = "\0*\0";
  35. // usage: sprintf(Caster::PATTERN_PRIVATE, $class, $property)
  36. public const PATTERN_PRIVATE = "\0%s\0%s";
  37. private static array $classProperties = [];
  38. /**
  39. * Casts objects to arrays and adds the dynamic property prefix.
  40. *
  41. * @param bool $hasDebugInfo Whether the __debugInfo method exists on $obj or not
  42. */
  43. public static function castObject(object $obj, string $class, bool $hasDebugInfo = false, ?string $debugClass = null): array
  44. {
  45. if ($hasDebugInfo) {
  46. try {
  47. $debugInfo = $obj->__debugInfo();
  48. } catch (\Throwable) {
  49. // ignore failing __debugInfo()
  50. $hasDebugInfo = false;
  51. }
  52. }
  53. $a = $obj instanceof \Closure ? [] : (array) $obj;
  54. if ($obj instanceof \__PHP_Incomplete_Class) {
  55. return $a;
  56. }
  57. $classProperties = self::$classProperties[$class] ??= self::getClassProperties(new \ReflectionClass($class));
  58. $a = array_replace($classProperties, $a);
  59. if ($a) {
  60. $debugClass ??= get_debug_type($obj);
  61. $i = 0;
  62. $prefixedKeys = [];
  63. foreach ($a as $k => $v) {
  64. if ("\0" !== ($k[0] ?? '')) {
  65. if (!isset($classProperties[$k])) {
  66. $prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
  67. }
  68. } elseif ($debugClass !== $class && 1 === strpos($k, $class)) {
  69. $prefixedKeys[$i] = "\0".$debugClass.strrchr($k, "\0");
  70. }
  71. ++$i;
  72. }
  73. if ($prefixedKeys) {
  74. $keys = array_keys($a);
  75. foreach ($prefixedKeys as $i => $k) {
  76. $keys[$i] = $k;
  77. }
  78. $a = array_combine($keys, $a);
  79. }
  80. }
  81. if ($hasDebugInfo && \is_array($debugInfo)) {
  82. foreach ($debugInfo as $k => $v) {
  83. if (!isset($k[0]) || "\0" !== $k[0]) {
  84. if (\array_key_exists(self::PREFIX_DYNAMIC.$k, $a)) {
  85. continue;
  86. }
  87. $k = self::PREFIX_VIRTUAL.$k;
  88. }
  89. unset($a[$k]);
  90. $a[$k] = $v;
  91. }
  92. }
  93. return $a;
  94. }
  95. /**
  96. * Filters out the specified properties.
  97. *
  98. * By default, a single match in the $filter bit field filters properties out, following an "or" logic.
  99. * When EXCLUDE_STRICT is set, an "and" logic is applied: all bits must match for a property to be removed.
  100. *
  101. * @param array $a The array containing the properties to filter
  102. * @param int $filter A bit field of Caster::EXCLUDE_* constants specifying which properties to filter out
  103. * @param string[] $listedProperties List of properties to exclude when Caster::EXCLUDE_VERBOSE is set, and to preserve when Caster::EXCLUDE_NOT_IMPORTANT is set
  104. * @param int|null &$count Set to the number of removed properties
  105. */
  106. public static function filter(array $a, int $filter, array $listedProperties = [], ?int &$count = 0): array
  107. {
  108. $count = 0;
  109. foreach ($a as $k => $v) {
  110. $type = self::EXCLUDE_STRICT & $filter;
  111. if (null === $v) {
  112. $type |= self::EXCLUDE_NULL & $filter;
  113. $type |= self::EXCLUDE_EMPTY & $filter;
  114. } elseif (false === $v || '' === $v || '0' === $v || 0 === $v || 0.0 === $v || [] === $v) {
  115. $type |= self::EXCLUDE_EMPTY & $filter;
  116. } elseif ($v instanceof UninitializedStub) {
  117. $type |= self::EXCLUDE_UNINITIALIZED & $filter;
  118. }
  119. if ((self::EXCLUDE_NOT_IMPORTANT & $filter) && !\in_array($k, $listedProperties, true)) {
  120. $type |= self::EXCLUDE_NOT_IMPORTANT;
  121. }
  122. if ((self::EXCLUDE_VERBOSE & $filter) && \in_array($k, $listedProperties, true)) {
  123. $type |= self::EXCLUDE_VERBOSE;
  124. }
  125. if (!isset($k[1]) || "\0" !== $k[0]) {
  126. $type |= self::EXCLUDE_PUBLIC & $filter;
  127. } elseif ('~' === $k[1]) {
  128. $type |= self::EXCLUDE_VIRTUAL & $filter;
  129. } elseif ('+' === $k[1]) {
  130. $type |= self::EXCLUDE_DYNAMIC & $filter;
  131. } elseif ('*' === $k[1]) {
  132. $type |= self::EXCLUDE_PROTECTED & $filter;
  133. } else {
  134. $type |= self::EXCLUDE_PRIVATE & $filter;
  135. }
  136. if ((self::EXCLUDE_STRICT & $filter) ? $type === $filter : $type) {
  137. unset($a[$k]);
  138. ++$count;
  139. }
  140. }
  141. return $a;
  142. }
  143. public static function castPhpIncompleteClass(\__PHP_Incomplete_Class $c, array $a, Stub $stub, bool $isNested): array
  144. {
  145. if (isset($a['__PHP_Incomplete_Class_Name'])) {
  146. $stub->class .= '('.$a['__PHP_Incomplete_Class_Name'].')';
  147. unset($a['__PHP_Incomplete_Class_Name']);
  148. }
  149. return $a;
  150. }
  151. private static function getClassProperties(\ReflectionClass $class): array
  152. {
  153. $classProperties = [];
  154. $className = $class->name;
  155. if ($parent = $class->getParentClass()) {
  156. $classProperties += self::$classProperties[$parent->name] ??= self::getClassProperties($parent);
  157. }
  158. foreach ($class->getProperties() as $p) {
  159. if ($p->isStatic()) {
  160. continue;
  161. }
  162. $classProperties[match (true) {
  163. $p->isPublic() => $p->name,
  164. $p->isProtected() => self::PREFIX_PROTECTED.$p->name,
  165. default => "\0".$className."\0".$p->name,
  166. }] = \PHP_VERSION_ID >= 80400 && $p->isVirtual() ? new VirtualStub($p) : new UninitializedStub($p);
  167. }
  168. return $classProperties;
  169. }
  170. }