ClassStub.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * Represents a PHP class identifier.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class ClassStub extends ConstStub
  18. {
  19. /**
  20. * @param string $identifier A PHP identifier, e.g. a class, method, interface, etc. name
  21. * @param callable $callable The callable targeted by the identifier when it is ambiguous or not a real PHP identifier
  22. */
  23. public function __construct(string $identifier, $callable = null)
  24. {
  25. $this->value = $identifier;
  26. try {
  27. if (null !== $callable) {
  28. if ($callable instanceof \Closure) {
  29. $r = new \ReflectionFunction($callable);
  30. } elseif (\is_object($callable)) {
  31. $r = [$callable, '__invoke'];
  32. } elseif (\is_array($callable)) {
  33. $r = $callable;
  34. } elseif (false !== $i = strpos($callable, '::')) {
  35. $r = [substr($callable, 0, $i), substr($callable, 2 + $i)];
  36. } else {
  37. $r = new \ReflectionFunction($callable);
  38. }
  39. } elseif (0 < $i = strpos($identifier, '::') ?: strpos($identifier, '->')) {
  40. $r = [substr($identifier, 0, $i), substr($identifier, 2 + $i)];
  41. } else {
  42. $r = new \ReflectionClass($identifier);
  43. }
  44. if (\is_array($r)) {
  45. try {
  46. $r = new \ReflectionMethod($r[0], $r[1]);
  47. } catch (\ReflectionException $e) {
  48. $r = new \ReflectionClass($r[0]);
  49. }
  50. }
  51. if (str_contains($identifier, "@anonymous\0")) {
  52. $this->value = $identifier = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
  53. return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
  54. }, $identifier);
  55. }
  56. if (null !== $callable && $r instanceof \ReflectionFunctionAbstract) {
  57. $s = ReflectionCaster::castFunctionAbstract($r, [], new Stub(), true, Caster::EXCLUDE_VERBOSE);
  58. $s = ReflectionCaster::getSignature($s);
  59. if (str_ends_with($identifier, '()')) {
  60. $this->value = substr_replace($identifier, $s, -2);
  61. } else {
  62. $this->value .= $s;
  63. }
  64. }
  65. } catch (\ReflectionException $e) {
  66. return;
  67. } finally {
  68. if (0 < $i = strrpos($this->value, '\\')) {
  69. $this->attr['ellipsis'] = \strlen($this->value) - $i;
  70. $this->attr['ellipsis-type'] = 'class';
  71. $this->attr['ellipsis-tail'] = 1;
  72. }
  73. }
  74. if ($f = $r->getFileName()) {
  75. $this->attr['file'] = $f;
  76. $this->attr['line'] = $r->getStartLine();
  77. }
  78. }
  79. public static function wrapCallable($callable)
  80. {
  81. if (\is_object($callable) || !\is_callable($callable)) {
  82. return $callable;
  83. }
  84. if (!\is_array($callable)) {
  85. $callable = new static($callable, $callable);
  86. } elseif (\is_string($callable[0])) {
  87. $callable[0] = new static($callable[0], $callable);
  88. } else {
  89. $callable[1] = new static($callable[1], $callable);
  90. }
  91. return $callable;
  92. }
  93. }