From 6c68b103f5e71309cd305974df4eaed59b8f8142 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 5 Feb 2020 13:43:05 +0100 Subject: [PATCH] [Php80] Add get_debug_type() --- Php80.php | 33 +++++++++++++++++++++++++++++++++ README.md | 1 + bootstrap.php | 4 ++++ 3 files changed, 38 insertions(+) diff --git a/Php80.php b/Php80.php index 77cc230..31c5188 100644 --- a/Php80.php +++ b/Php80.php @@ -14,6 +14,7 @@ /** * @author Ion Bazan * @author Nico Oelgart + * @author Nicolas Grekas * * @internal */ @@ -24,6 +25,38 @@ public static function fdiv(float $dividend, float $divisor): float return @($dividend / $divisor); } + public static function get_debug_type($value): string + { + switch (true) { + case null === $value: return 'null'; + case \is_bool($value): return 'bool'; + case \is_string($value): return 'string'; + case \is_array($value): return 'array'; + case \is_int($value): return 'int'; + case \is_float($value): return 'float'; + case \is_object($value): break; + case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class'; + default: + if (null === $type = @get_resource_type($value)) { + return 'unknown'; + } + + if ('Unknown' === $type) { + $type = 'closed'; + } + + return "resource ($type)"; + } + + $class = \get_class($value); + + if (false === strpos($class, '@')) { + return $class; + } + + return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous'; + } + public static function preg_last_error_msg(): string { switch (preg_last_error()) { diff --git a/README.md b/README.md index 6d89cbe..ce23ca3 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ This component provides functions added to PHP 8.0 core: - [`fdiv`](https://php.net/fdiv) - `ValueError` class - `FILTER_VALIDATE_BOOL` constant +- [`get_debug_type`](https://php.net/get_debug_type) - [`preg_last_error_msg`](https://php.net/preg_last_error_msg) More information can be found in the diff --git a/bootstrap.php b/bootstrap.php index f239357..b20fb6b 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -23,4 +23,8 @@ function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) { define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN); } + + if (!function_exists('get_debug_type')) { + function get_debug_type($value): string { return p\Php80::get_debug_type($value); } + } }