Skip to content
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

[Php80] Add get_debug_type() #226

Merged
merged 1 commit into from
Mar 3, 2020
Merged
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 @@ -44,6 +44,7 @@ Polyfills are provided for:
- the `get_mangled_object_vars`, `mb_str_split` and `password_algos` functions
introduced in PHP 7.4;
- the `fdiv` function introduced in PHP 8.0;
- the `get_debug_type` function introduced in PHP 8.0;
- the `preg_last_error_msg` function introduced in PHP 8.0;
- the `ValueError` class introduced in PHP 8.0;
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;
Expand Down
33 changes: 33 additions & 0 deletions src/Php80/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';
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
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 src/Php80/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Php80/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
}
}
28 changes: 28 additions & 0 deletions tests/Php80/Php80Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ public function invalidFloatProvider()
);
}

/**
* @covers \Symfony\Polyfill\Php80\Php80::get_debug_type
*/
public function testGetDebugType()
{
$this->assertSame(__CLASS__, get_debug_type($this));
$this->assertSame('stdClass', get_debug_type(new \stdClass()));
$this->assertSame('class@anonymous', get_debug_type(eval('return new class() {};')));
$this->assertSame('stdClass@anonymous', get_debug_type(eval('return new class() extends stdClass {};')));
$this->assertSame('Reflector@anonymous', get_debug_type(eval('return new class() implements Reflector { function __toString() {} public static function export() {} };')));

$this->assertSame('string', get_debug_type('foo'));
$this->assertSame('bool', get_debug_type(false));
$this->assertSame('bool', get_debug_type(true));
$this->assertSame('null', get_debug_type(null));
$this->assertSame('array', get_debug_type(array()));
$this->assertSame('int', get_debug_type(1));
$this->assertSame('float', get_debug_type(1.2));
$this->assertSame('resource (stream)', get_debug_type($h = fopen(__FILE__, 'r')));
$this->assertSame('resource (closed)', get_debug_type(fclose($h) ? $h : $h));

$unserializeCallbackHandler = ini_set('unserialize_callback_func', null);
$var = unserialize('O:8:"Foo\Buzz":0:{}');
ini_set('unserialize_callback_func', $unserializeCallbackHandler);

$this->assertSame('__PHP_Incomplete_Class', get_debug_type($var));
}

public function setExpectedException($exception, $message = '', $code = null)
{
if (!class_exists('PHPUnit\Framework\Error\Notice')) {
Expand Down