Debian Codegraph @

vendor/symfony/polyfill-php84/Resources/stubs/ReflectionConstant.php

raw
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 if (\PHP_VERSION_ID < 80400) {
13 // @author Daniel Scherzer <[email protected]>
14 final class ReflectionConstant
15 {
16 /**
17 * @var string
18 *
19 * @readonly
20 */
21 public $name;
22
23 private $value;
24 private $deprecated;
25
26 private static $persistentConstants = [];
27
28 public function __construct(string $name)
29 {
30 if (!defined($name) || false !== strpos($name, '::')) {
31 throw new ReflectionException("Constant \"$name\" does not exist");
32 }
33
34 $this->name = ltrim($name, '\\');
35 $deprecated = false;
36 $eh = set_error_handler(static function ($type, $msg, $file, $line) use ($name, &$deprecated, &$eh) {
37 if (\E_DEPRECATED === $type && "Constant $name is deprecated" === $msg) {
38 return $deprecated = true;
39 }
40
41 return $eh && $eh($type, $msg, $file, $line);
42 });
43
44 try {
45 $this->value = constant($name);
46 $this->deprecated = $deprecated;
47 } finally {
48 restore_error_handler();
49 }
50 }
51
52 public function getName(): string
53 {
54 return $this->name;
55 }
56
57 public function getValue()
58 {
59 return $this->value;
60 }
61
62 public function getNamespaceName(): string
63 {
64 if (false === $slashPos = strrpos($this->name, '\\')) {
65 return '';
66 }
67
68 return substr($this->name, 0, $slashPos);
69 }
70
71 public function getShortName(): string
72 {
73 if (false === $slashPos = strrpos($this->name, '\\')) {
74 return $this->name;
75 }
76
77 return substr($this->name, $slashPos + 1);
78 }
79
80 public function isDeprecated(): bool
81 {
82 return $this->deprecated;
83 }
84
85 public function __toString(): string
86 {
87 // A constant is persistent if provided by PHP itself rather than
88 // being defined by users. If we got here, we know that it *is*
89 // defined, so we just need to figure out if it is defined by the
90 // user or not
91 if (!self::$persistentConstants) {
92 $persistentConstants = get_defined_constants(true);
93 unset($persistentConstants['user']);
94 foreach ($persistentConstants as $constants) {
95 self::$persistentConstants += $constants;
96 }
97 }
98 $persistent = array_key_exists($this->name, self::$persistentConstants);
99
100 // Can't match the inclusion of `no_file_cache` but the rest is
101 // possible to match
102 $result = 'Constant [ ';
103 if ($persistent || $this->deprecated) {
104 $result .= '<';
105 if ($persistent) {
106 $result .= 'persistent';
107 if ($this->deprecated) {
108 $result .= ', ';
109 }
110 }
111 if ($this->deprecated) {
112 $result .= 'deprecated';
113 }
114 $result .= '> ';
115 }
116 // Cannot just use gettype() to match zend_zval_type_name()
117 if (is_object($this->value)) {
118 $result .= \PHP_VERSION_ID >= 80000 ? get_debug_type($this->value) : gettype($this->value);
119 } elseif (is_bool($this->value)) {
120 $result .= 'bool';
121 } elseif (is_int($this->value)) {
122 $result .= 'int';
123 } elseif (is_float($this->value)) {
124 $result .= 'float';
125 } elseif (null === $this->value) {
126 $result .= 'null';
127 } else {
128 $result .= gettype($this->value);
129 }
130 $result .= ' ';
131 $result .= $this->name;
132 $result .= ' ] { ';
133 if (is_array($this->value)) {
134 $result .= 'Array';
135 } else {
136 // This will throw an exception if the value is an object that
137 // cannot be converted to string; that is expected and matches
138 // the behavior of zval_get_string_func()
139 $result .= (string) $this->value;
140 }
141 $result .= " }\n";
142
143 return $result;
144 }
145
146 public function __sleep(): array
147 {
148 throw new Exception("Serialization of 'ReflectionConstant' is not allowed");
149 }
150
151 public function __wakeup(): void
152 {
153 throw new Exception("Unserialization of 'ReflectionConstant' is not allowed");
154 }
155 }
156 }