Skip to content

Added get_debug_type as new function #5143

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

Closed
wants to merge 9 commits into from
Closed
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 ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ static const zend_function_entry basic_functions[] = { /* {{{ */
PHP_FE(strval, arginfo_strval)
PHP_FE(boolval, arginfo_boolval)
PHP_FE(gettype, arginfo_gettype)
PHP_FE(get_debug_type, arginfo_get_debug_type)
PHP_FE(settype, arginfo_settype)
PHP_FE(is_null, arginfo_is_null)
PHP_FE(is_resource, arginfo_is_resource)
Expand Down
3 changes: 3 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,9 @@ function socket_set_timeout($socket, int $seconds, int $microseconds = 0): bool
/** @param mixed $var */
function gettype($var): string {}

/** @param mixed $var */
function get_debug_type($var): string {}

function settype(&$var, string $type): bool {}

/** @param mixed $value */
Expand Down
2 changes: 2 additions & 0 deletions ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_gettype, 0, 1, IS_STRING, 0)
ZEND_ARG_INFO(0, var)
ZEND_END_ARG_INFO()

#define arginfo_get_debug_type arginfo_gettype

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_settype, 0, 2, _IS_BOOL, 0)
ZEND_ARG_INFO(1, var)
ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PHP_FUNCTION(floatval);
PHP_FUNCTION(strval);
PHP_FUNCTION(boolval);
PHP_FUNCTION(gettype);
PHP_FUNCTION(get_debug_type);
PHP_FUNCTION(settype);
PHP_FUNCTION(is_null);
PHP_FUNCTION(is_resource);
Expand Down
57 changes: 57 additions & 0 deletions ext/standard/tests/general_functions/get_debug_type_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
Test get_debug_type() class reading
--FILE--
<?php

namespace Demo {
class ClassInNamespace {

}
}

namespace {
class ClassInGlobal { }

class ToBeExtended { }
interface ToBeImplemented { }

$fp = fopen(__FILE__, 'r');

$fp_closed = fopen(__FILE__, 'r');
fclose($fp_closed);

/* tests against an object type */
echo get_debug_type(new ClassInGlobal()) . "\n";
echo get_debug_type(new Demo\ClassInNamespace()) . "\n";
echo get_debug_type(new class {}) . "\n";
echo get_debug_type(new class extends ToBeExtended {}) . "\n";
echo get_debug_type(new class implements ToBeImplemented {}) . "\n";

/* scalars */
echo get_debug_type("foo") . "\n";
echo get_debug_type(false) . "\n";
echo get_debug_type(true) . "\n";
echo get_debug_type(1) . "\n";
echo get_debug_type(1.1) . "\n";
echo get_debug_type([]) . "\n";
echo get_debug_type(null) . "\n";
echo get_debug_type($fp) . "\n";
echo get_debug_type($fp_closed) . "\n";

}

--EXPECT--
ClassInGlobal
Demo\ClassInNamespace
class@anonymous
ToBeExtended@anonymous
ToBeImplemented@anonymous
string
bool
bool
int
float
array
null
resource (stream)
resource (closed)
46 changes: 46 additions & 0 deletions ext/standard/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,52 @@ PHP_FUNCTION(gettype)
}
/* }}} */

/* {{{ proto string get_debug_type(mixed var)
Returns the type of the variable resolving class names */
PHP_FUNCTION(get_debug_type)
{
zval *arg;
const char *name;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(arg)
ZEND_PARSE_PARAMETERS_END();

switch (Z_TYPE_P(arg)) {
case IS_NULL:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE));
case IS_FALSE:
case IS_TRUE:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_BOOL));
case IS_LONG:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_INT));
case IS_DOUBLE:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_FLOAT));
case IS_STRING:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_STRING));
case IS_ARRAY:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_ARRAY));
case IS_OBJECT:
if (Z_OBJ_P(arg)->ce->ce_flags & ZEND_ACC_ANON_CLASS) {
name = ZSTR_VAL(Z_OBJ_P(arg)->ce->name);
RETURN_NEW_STR(zend_string_init(name, strlen(name), 0));
} else {
RETURN_STR_COPY(Z_OBJ_P(arg)->ce->name);
}
case IS_RESOURCE:
name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
if (name) {
RETURN_NEW_STR(zend_strpprintf(0, "resource (%s)", name));
} else {
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it miss "IS_UNINITIALIZED" used with typed properties?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They throw upon trying to read them, so could not be used in an expression.

default:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_UNKNOWN));
}
}
/* }}} */


/* {{{ proto bool settype(mixed &var, string type)
Set the type of the variable */
PHP_FUNCTION(settype)
Expand Down