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

Added get_debug_type as new function #5143

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -1317,6 +1317,9 @@ function stream_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 @@ -2010,6 +2010,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);
markrandall marked this conversation as resolved.
Show resolved Hide resolved
PHP_FUNCTION(settype);
PHP_FUNCTION(is_null);
PHP_FUNCTION(is_resource);
Expand Down
43 changes: 43 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,43 @@
--TEST--
Test get_debug_type() class reading
--FILE--
<?php

namespace Demo {
class ClassInNamespace {

}
}

namespace {
class ClassInGlobal {

}

/* tests against an object type */
echo get_debug_type(new ClassInGlobal()) . "\n";
echo get_debug_type(new Demo\ClassInNamespace()) . "\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";
markrandall marked this conversation as resolved.
Show resolved Hide resolved
echo get_debug_type(fopen(__FILE__, 'r')) . "\n";
markrandall marked this conversation as resolved.
Show resolved Hide resolved

}

--EXPECT--
ClassInGlobal
Demo\ClassInNamespace
string
bool
bool
int
nikic marked this conversation as resolved.
Show resolved Hide resolved
float
array
null
resource
35 changes: 35 additions & 0 deletions ext/standard/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ 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;

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:
RETURN_STR_COPY(Z_OBJ_P(arg)->ce->name);
nikic marked this conversation as resolved.
Show resolved Hide resolved
case IS_RESOURCE:
RETURN_INTERNED_STR(ZSTR_KNOWN(ZEND_STR_RESOURCE));
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