Skip to content

Commit

Permalink
feature #226 [Php80] Add get_debug_type() (nicolas-grekas)
Browse files Browse the repository at this point in the history
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()
  • Loading branch information
nicolas-grekas committed Mar 3, 2020
2 parents fdc2e48 + 6c68b10 commit 8854dc8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @author Ion Bazan <ion.bazan@gmail.com>
* @author Nico Oelgart <nicoswd@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
Expand All @@ -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()) {
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This component provides features 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)
- [`str_contains`](https://php.net/str_contains)

Expand Down
4 changes: 4 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ function str_contains(string $haystack, string $needle): bool { return p\Php80::
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); }
}
}

0 comments on commit 8854dc8

Please sign in to comment.