Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd8ced4d

Browse files
DanielEScherzernicolas-grekas
authored andcommitted
[8.4] Add polyfill forReflectionConstant
1 parentcadee0e commitd8ced4d

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

‎Php84.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public static function bcdivmod(string $num1, string $num2, ?int $scale = null):
210210
if (null ===$quot =\bcdiv($num1,$num2,0)) {
211211
returnnull;
212212
}
213-
$scale =$scale ?? (\PHP_VERSION_ID >=70300 ?\bcscale() : (ini_get('bcmath.scale') ?:0);
213+
$scale =$scale ?? (\PHP_VERSION_ID >=70300 ?\bcscale() : (ini_get('bcmath.scale') ?:0));
214214

215215
return [$quot,\bcmod($num1,$num2,$scale)];
216216
}

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This component provides features added to PHP 8.4 core:
1111
-[`grapheme_str_split`](https://wiki.php.net/rfc/grapheme_str_split)
1212
-[`mb_trim`,`mb_ltrim` and`mb_rtrim`](https://wiki.php.net/rfc/mb_trim)
1313
-[`mb_ucfirst` and`mb_lcfirst`](https://wiki.php.net/rfc/mb_ucfirst)
14+
-[`ReflectionConstant`](https://github.com/php/php-src/pull/13669)
1415

1516
More information can be found in the
1617
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
/**
14+
* @author Daniel Scherzer <daniel.e.scherzer@gmail.com>
15+
*/
16+
finalclass ReflectionConstant
17+
{
18+
/**
19+
* @var string
20+
*
21+
* @readonly
22+
*/
23+
public$name;
24+
25+
private$value;
26+
private$deprecated;
27+
28+
privatestatic$persistentConstants = [];
29+
30+
publicfunction__construct(string$name)
31+
{
32+
if (!defined($name) ||false !==strpos($name,'::')) {
33+
thrownewReflectionException("Constant\"$name\" does not exist");
34+
}
35+
36+
$this->name =ltrim($name,'\\');
37+
$deprecated =false;
38+
$eh =set_error_handler(staticfunction ($type,$msg,$file,$line)use ($name, &$deprecated, &$eh) {
39+
if (\E_DEPRECATED ===$type &&"Constant$name is deprecated" ===$msg) {
40+
return$deprecated =true;
41+
}
42+
43+
return$eh &&$eh($type,$msg,$file,$line);
44+
});
45+
46+
try {
47+
$this->value =constant($name);
48+
$this->deprecated =$deprecated;
49+
}finally {
50+
restore_error_handler();
51+
}
52+
}
53+
54+
publicfunctiongetName():string
55+
{
56+
return$this->name;
57+
}
58+
59+
publicfunctiongetValue()
60+
{
61+
return$this->value;
62+
}
63+
64+
publicfunctiongetNamespaceName():string
65+
{
66+
if (false ===$slashPos =strrpos($this->name,'\\')) {
67+
return'';
68+
}
69+
70+
returnsubstr($this->name,0,$slashPos);
71+
}
72+
73+
publicfunctiongetShortName():string
74+
{
75+
if (false ===$slashPos =strrpos($this->name,'\\')) {
76+
return$this->name;
77+
}
78+
79+
returnsubstr($this->name,$slashPos +1);
80+
}
81+
82+
publicfunctionisDeprecated():bool
83+
{
84+
return$this->deprecated;
85+
}
86+
87+
publicfunction__toString():string
88+
{
89+
// A constant is persistent if provided by PHP itself rather than
90+
// being defined by users. If we got here, we know that it *is*
91+
// defined, so we just need to figure out if it is defined by the
92+
// user or not
93+
if (!self::$persistentConstants) {
94+
$persistentConstants =get_defined_constants(true);
95+
unset($persistentConstants['user']);
96+
foreach ($persistentConstantsas$constants) {
97+
self::$persistentConstants +=$constants;
98+
}
99+
}
100+
$persistent =array_key_exists($this->name,self::$persistentConstants);
101+
102+
// Can't match the inclusion of `no_file_cache` but the rest is
103+
// possible to match
104+
$result ='Constant [';
105+
if ($persistent ||$this->deprecated) {
106+
$result .='<';
107+
if ($persistent) {
108+
$result .='persistent';
109+
if ($this->deprecated) {
110+
$result .=',';
111+
}
112+
}
113+
if ($this->deprecated) {
114+
$result .='deprecated';
115+
}
116+
$result .='>';
117+
}
118+
// Cannot just use gettype() to match zend_zval_type_name()
119+
if (is_object($this->value)) {
120+
$result .= \PHP_VERSION_ID >=80000 ?get_debug_type($this->value) :gettype($this->value);
121+
}elseif (is_bool($this->value)) {
122+
$result .='bool';
123+
}elseif (is_int($this->value)) {
124+
$result .='int';
125+
}elseif (is_float($this->value)) {
126+
$result .='float';
127+
}elseif (null ===$this->value) {
128+
$result .='null';
129+
}else {
130+
$result .=gettype($this->value);
131+
}
132+
$result .='';
133+
$result .=$this->name;
134+
$result .=' ] {';
135+
if (is_array($this->value)) {
136+
$result .='Array';
137+
}else {
138+
// This will throw an exception if the value is an object that
139+
// cannot be converted to string; that is expected and matches
140+
// the behavior of zval_get_string_func()
141+
$result .= (string)$this->value;
142+
}
143+
$result .=" }\n";
144+
145+
return$result;
146+
}
147+
148+
publicfunction__sleep():array
149+
{
150+
thrownewException("Serialization of 'ReflectionConstant' is not allowed");
151+
}
152+
153+
publicfunction__wakeup():void
154+
{
155+
thrownewException("Unserialization of 'ReflectionConstant' is not allowed");
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp