Debian Codegraph @

lib/3rdParty/composer/symfony/polyfill-php84/Resources/stubs/ReflectionConstant.php

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