Skip to content

Commit 8854dc8

Browse files
feature #226 [Php80] Add get_debug_type() (nicolas-grekas)
This PR was merged into the 1.15-dev branch. Discussion ---------- [Php80] Add get_debug_type() Waiting for - [ ] php/php-src#5143 - [ ] https://wiki.php.net/rfc/get_debug_type - [x] php/php-src#5153 Commits ------- 61168ea [Php80] Add get_debug_type()
2 parents fdc2e48 + 6c68b10 commit 8854dc8

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
@@ -7,6 +7,7 @@ This component provides features added to PHP 8.0 core:
77
- [`fdiv`](https://php.net/fdiv)
88
- `ValueError` class
99
- `FILTER_VALIDATE_BOOL` constant
10+
- [`get_debug_type`](https://php.net/get_debug_type)
1011
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
1112
- [`str_contains`](https://php.net/str_contains)
1213

bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ function str_contains(string $haystack, string $needle): bool { return p\Php80::
2727
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2828
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2929
}
30+
31+
if (!function_exists('get_debug_type')) {
32+
function get_debug_type($value): string { return p\Php80::get_debug_type($value); }
33+
}
3034
}

0 commit comments

Comments
 (0)