Skip to content

[8.4] Add polyfill for ReflectionConstant #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Polyfills are provided for:
- the `array_find`, `array_find_key`, `array_any` and `array_all` functions introduced in PHP 8.4;
- the `Deprecated` attribute introduced in PHP 8.4;
- the `mb_trim`, `mb_ltrim` and `mb_rtrim` functions introduced in PHP 8.4;
- the `ReflectionConstant` class introduced in PHP 8.4
- the `CURL_HTTP_VERSION_3` and `CURL_HTTP_VERSION_3ONLY` constants introduced in PHP 8.4;
- the `get_error_handler` and `get_exception_handler` functions introduced in PHP 8.5;
- the `NoDiscard` attribute introduced in PHP 8.5;
Expand Down
1 change: 1 addition & 0 deletions src/Php84/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This component provides features added to PHP 8.4 core:
- `CURL_HTTP_VERSION_3` and `CURL_HTTP_VERSION_3ONLY` constants
- [`fpow`](https://wiki.php.net/rfc/raising_zero_to_power_of_negative_number)
- [`mb_trim`, `mb_ltrim` and `mb_rtrim`](https://wiki.php.net/rfc/mb_trim)
- [`ReflectionConstant`](https://github.com/php/php-src/pull/13669)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).
Expand Down
164 changes: 164 additions & 0 deletions src/Php84/Resources/stubs/ReflectionConstant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* @author Daniel Scherzer <[email protected]>
*/
if (\PHP_VERSION_ID < 80400) {
final class ReflectionConstant {

/**
* @var string
* @readonly
*/
public $name;

private $value;

/** @var bool */
private $deprecated;

/** @var bool */
private $persistent;

public function __construct( string $name )
{
if ( !defined( $name ) ) {
throw new ReflectionException( "Constant \"$name\" does not exist" );
}
// ReflectionConstant cannot be used for class constants; constants
// with 2 `:` or more in a row are prohibited via define()
if ( strpos( $name, '::' ) !== false ) {
throw new ReflectionException( "Constant \"$name\" does not exist" );
}
$this->name = $name;
$deprecated = false;
$old = set_error_handler(
static function (
int $errno,
string $errstr
) use ( $name, &$deprecated ) {
if ( $errno === E_DEPRECATED
&& $errstr === "Constant $name is deprecated"
) {
$deprecated = true;
}
return true;
}
);
$this->value = constant( $name );
$this->deprecated = $deprecated;
set_error_handler( $old );

// A constant is persistent if provided by PHP itself rather than
// being defined by users. If we got here, we know that it *is*
// defined, so we just need to figure out if it is defined by the
// user or not
$allConstants = get_defined_constants(true);
$userConstants = $allConstants['user'] ?? [];
$key = ltrim( $this->name, '\\' );
$this->persistent = !array_key_exists( $key, $userConstants );
}

public function getName(): string
{
return ltrim( $this->name, '\\' );
}

public function getValue()
{
return $this->value;
}

public function getNamespaceName(): string
{
$slashPos = strrpos( $this->name, '\\' );
if ( $slashPos === false ) {
return '';
}
return substr( $this->name, 1, $slashPos - 1 );
}

public function getShortName(): string
{
$slashPos = strrpos( $this->name, '\\' );
if ( $slashPos === false ) {
return $this->name;
}
return substr( $this->name, $slashPos + 1 );
}

public function isDeprecated(): bool
{
return $this->deprecated;
}

public function __toString(): string
{
// Can't match the inclusion of `no_file_cache` but the rest is
// possible to match
$result = 'Constant [ ';
if ( $this->persistent || $this->deprecated ) {
$result .= '<';
if ( $this->persistent ) {
$result .= 'persistent';
if ( $this->deprecated ) {
$result .= ', ';
}
}
if ( $this->deprecated ) {
$result .= 'deprecated';
}
$result .= '> ';
}
// Cannot just use gettype() to match zend_zval_type_name()
if ( is_object( $this->value ) ) {
$result .= get_class( $this->value );
} elseif ( is_bool( $this->value ) ) {
$result .= 'bool';
} elseif ( is_int( $this->value ) ) {
$result .= 'int';
} elseif ( is_float( $this->value ) ) {
$result .= 'float';
} elseif ( is_string( $this->value ) ) {
$result .= 'string';
} elseif ( is_resource( $this->value ) ) {
$result .= 'resource';
} elseif ( $this->value === null ) {
$result .= 'null';
} else {
// Reasonable fallback
$result .= gettype( $this->value );
}
$result .= ' ';
$result .= $this->getName();
$result .= ' ] { ';
if ( is_array( $this->value ) ) {
$result .= 'Array';
} else {
$result .= (string)$this->value;
}
$result .= " }\n";
return $result;
}

public function __sleep(): array
{
throw new Exception("Serialization of 'ReflectionConstant' is not allowed");
}

public function __wakeup(): void
{
throw new Exception("Unserialization of 'ReflectionConstant' is not allowed");
}

}
}
Loading
Loading