Skip to content

Commit 6c68b10

Browse files
[Php80] Add get_debug_type()
1 parent 13f8d62 commit 6c68b10

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Php80.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/**
1515
* @author Ion Bazan <[email protected]>
1616
* @author Nico Oelgart <[email protected]>
17+
* @author Nicolas Grekas <[email protected]>
1718
*
1819
* @internal
1920
*/
@@ -24,6 +25,38 @@ public static function fdiv(float $dividend, float $divisor): float
2425
return @($dividend / $divisor);
2526
}
2627

28+
public static function get_debug_type($value): string
29+
{
30+
switch (true) {
31+
case null === $value: return 'null';
32+
case \is_bool($value): return 'bool';
33+
case \is_string($value): return 'string';
34+
case \is_array($value): return 'array';
35+
case \is_int($value): return 'int';
36+
case \is_float($value): return 'float';
37+
case \is_object($value): break;
38+
case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
39+
default:
40+
if (null === $type = @get_resource_type($value)) {
41+
return 'unknown';
42+
}
43+
44+
if ('Unknown' === $type) {
45+
$type = 'closed';
46+
}
47+
48+
return "resource ($type)";
49+
}
50+
51+
$class = \get_class($value);
52+
53+
if (false === strpos($class, '@')) {
54+
return $class;
55+
}
56+
57+
return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous';
58+
}
59+
2760
public static function preg_last_error_msg(): string
2861
{
2962
switch (preg_last_error()) {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This component provides functions added to PHP 8.0 core:
66
- [`fdiv`](https://php.net/fdiv)
77
- `ValueError` class
88
- `FILTER_VALIDATE_BOOL` constant
9+
- [`get_debug_type`](https://php.net/get_debug_type)
910
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
1011

1112
More information can be found in the

bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg();
2323
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2424
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2525
}
26+
27+
if (!function_exists('get_debug_type')) {
28+
function get_debug_type($value): string { return p\Php80::get_debug_type($value); }
29+
}
2630
}

0 commit comments

Comments
 (0)