Skip to content

Commit

Permalink
Reflection support for type hints and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeccati committed Jun 8, 2015
1 parent cf8898f commit ec281fe
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 114 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@
- Reflection
. Fixed inheritance chain of Reflector interface. (Tjerk)
. Added ReflectionGenerator class. (Bob)
. Added reflection support for return types and type declarations. (Sara,
Matteo)

- Session:
. Fixed bug #67694 (Regression in session_regenerate_id()). (Tjerk)
Expand Down
7 changes: 6 additions & 1 deletion UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,12 @@ Other
accessible through stream_get_meta_data() output.

- Reflection
. Added a ReflectionGenerator class (yield from Traces, current file/line etc.)
. Added a ReflectionGenerator class (yield from Traces, current file/line,
etc.)
. Added a ReflectionType class to better support the new return type and
scalar type declarations features. The new ReflectionParameter::getType()
and ReflectionFunctionAbstract::getReturnType() methods both return an
instance of ReflectionType.

========================================
3. Changes in SAPI modules
Expand Down
173 changes: 107 additions & 66 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PHPAPI zend_class_entry *reflection_function_abstract_ptr;
PHPAPI zend_class_entry *reflection_function_ptr;
PHPAPI zend_class_entry *reflection_generator_ptr;
PHPAPI zend_class_entry *reflection_parameter_ptr;
PHPAPI zend_class_entry *reflection_typeannotation_ptr;
PHPAPI zend_class_entry *reflection_type_ptr;
PHPAPI zend_class_entry *reflection_class_ptr;
PHPAPI zend_class_entry *reflection_object_ptr;
PHPAPI zend_class_entry *reflection_method_ptr;
Expand Down Expand Up @@ -202,17 +202,18 @@ typedef struct _parameter_reference {
zend_function *fptr;
} parameter_reference;

/* Struct for type annotations */
typedef struct _typeannotation_reference {
/* Struct for type hints */
typedef struct _type_reference {
struct _zend_arg_info *arg_info;
} typeannotation_reference;
zend_function *fptr;
} type_reference;

typedef enum {
REF_TYPE_OTHER, /* Must be 0 */
REF_TYPE_FUNCTION,
REF_TYPE_GENERATOR,
REF_TYPE_PARAMETER,
REF_TYPE_ANNOTATION,
REF_TYPE_TYPE,
REF_TYPE_PROPERTY,
REF_TYPE_DYNAMIC_PROPERTY
} reflection_type_t;
Expand Down Expand Up @@ -306,14 +307,18 @@ static void reflection_free_objects_storage(zend_object *object) /* {{{ */
reflection_object *intern = reflection_object_from_obj(object);
parameter_reference *reference;
property_reference *prop_reference;
type_reference *typ_reference;

if (intern->ptr) {
switch (intern->ref_type) {
case REF_TYPE_PARAMETER:
reference = (parameter_reference*)intern->ptr;
_free_function(reference->fptr);
/* fallthrough */
case REF_TYPE_ANNOTATION:
efree(intern->ptr);
break;
case REF_TYPE_TYPE:
typ_reference = (type_reference*)intern->ptr;
_free_function(typ_reference->fptr);
efree(intern->ptr);
break;
case REF_TYPE_FUNCTION:
Expand Down Expand Up @@ -1245,18 +1250,19 @@ static void reflection_parameter_factory(zend_function *fptr, zval *closure_obje
}
/* }}} */

/* {{{ reflection_typeannotation_factory */
static void reflection_typeannotation_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, zval *object)
/* {{{ reflection_type_factory */
static void reflection_type_factory(zend_function *fptr, zval *closure_object, struct _zend_arg_info *arg_info, zval *object)
{
reflection_object *intern;
typeannotation_reference *reference;
type_reference *reference;

reflection_instantiate(reflection_typeannotation_ptr, object);
reflection_instantiate(reflection_type_ptr, object);
intern = Z_REFLECTION_P(object);
reference = (typeannotation_reference*) emalloc(sizeof(typeannotation_reference));
reference = (type_reference*) emalloc(sizeof(type_reference));
reference->arg_info = arg_info;
reference->fptr = fptr;
intern->ptr = reference;
intern->ref_type = REF_TYPE_ANNOTATION;
intern->ref_type = REF_TYPE_TYPE;
intern->ce = fptr->common.scope;
if (closure_object) {
Z_ADDREF_P(closure_object);
Expand Down Expand Up @@ -2533,6 +2539,7 @@ ZEND_METHOD(reflection_parameter, __toString)
_parameter_string(&str, param->fptr, param->arg_info, param->offset, param->required, "");
RETURN_NEW_STR(str.buf);
}

/* }}} */

/* {{{ proto public string ReflectionParameter::getName()
Expand Down Expand Up @@ -2567,7 +2574,7 @@ ZEND_METHOD(reflection_parameter, getDeclaringFunction)
/* }}} */

/* {{{ proto public ReflectionClass|NULL ReflectionParameter::getDeclaringClass()
Returns in which class this parameter is defined (not the typehint of the parameter) */
Returns in which class this parameter is defined (not the type of the parameter) */
ZEND_METHOD(reflection_parameter, getDeclaringClass)
{
reflection_object *intern;
Expand Down Expand Up @@ -2659,9 +2666,9 @@ ZEND_METHOD(reflection_parameter, getClass)
}
/* }}} */

/* {{{ proto public bool ReflectionParameter::hasTypeAnnotation()
Rethern whether parameter has a type hint */
ZEND_METHOD(reflection_parameter, hasTypeAnnotation)
/* {{{ proto public bool ReflectionParameter::hasType()
Returns whether parameter has a type */
ZEND_METHOD(reflection_parameter, hasType)
{
reflection_object *intern;
parameter_reference *param;
Expand All @@ -2675,9 +2682,9 @@ ZEND_METHOD(reflection_parameter, hasTypeAnnotation)
}
/* }}} */

/* {{{ proto public string ReflectionParameter::getTypeAnnotation()
Returns the typehint associated with the parameter */
ZEND_METHOD(reflection_parameter, getTypeAnnotation)
/* {{{ proto public ReflectionType ReflectionParameter::getType()
Returns the type associated with the parameter */
ZEND_METHOD(reflection_parameter, getType)
{
reflection_object *intern;
parameter_reference *param;
Expand All @@ -2687,10 +2694,13 @@ ZEND_METHOD(reflection_parameter, getTypeAnnotation)
}
GET_REFLECTION_OBJECT_PTR(param);

if (!param->arg_info->type_hint) {
if ((param->fptr->type == ZEND_INTERNAL_FUNCTION ?
((zend_internal_arg_info*)param->arg_info)->type_hint :
param->arg_info->type_hint) == 0)
{
RETURN_NULL();
}
reflection_typeannotation_factory(param->fptr, Z_ISUNDEF(intern->obj)? NULL : &intern->obj, param->arg_info, return_value);
reflection_type_factory(_copy_function(param->fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, param->arg_info, return_value);
}
/* }}} */

Expand Down Expand Up @@ -2931,60 +2941,44 @@ ZEND_METHOD(reflection_parameter, isVariadic)
}
/* }}} */

/* {{{ proto public bool ReflectionTypeAnnotation::isArray()
Returns whether parameter MUST be an array */
ZEND_METHOD(reflection_typeannotation, isArray)
{
reflection_object *intern;
typeannotation_reference *param;

if (zend_parse_parameters_none() == FAILURE) {
return;
}
GET_REFLECTION_OBJECT_PTR(param);

RETVAL_BOOL(param->arg_info->type_hint == IS_ARRAY);
}
/* }}} */

/* {{{ proto public bool ReflectionTypeAnnotation::isCallable()
Returns whether parameter MUST be callable */
ZEND_METHOD(reflection_typeannotation, isCallable)
/* {{{ proto public bool ReflectionType::allowsNull()
Returns whether parameter MAY be null */
ZEND_METHOD(reflection_type, allowsNull)
{
reflection_object *intern;
typeannotation_reference *param;
type_reference *param;

if (zend_parse_parameters_none() == FAILURE) {
return;
}
GET_REFLECTION_OBJECT_PTR(param);

RETVAL_BOOL(param->arg_info->type_hint == IS_CALLABLE);
RETVAL_BOOL(param->arg_info->allow_null);
}
/* }}} */

/* {{{ proto public bool ReflectionTypeAnnotation::isNullable()
Returns whether parameter MAY be null */
ZEND_METHOD(reflection_typeannotation, isNullable)
/* {{{ proto public bool ReflectionType::isBuiltin()
Returns whether parameter is a builtin type */
ZEND_METHOD(reflection_type, isBuiltin)
{
reflection_object *intern;
typeannotation_reference *param;
type_reference *param;

if (zend_parse_parameters_none() == FAILURE) {
return;
}
GET_REFLECTION_OBJECT_PTR(param);

RETVAL_BOOL(param->arg_info->allow_null);
RETVAL_BOOL(param->arg_info->type_hint != IS_OBJECT);
}
/* }}} */

/* {{{ proto public string ReflectionTypeAnnotation::__toString()
Return the text of the type annotation */
ZEND_METHOD(reflection_typeannotation, __toString)
/* {{{ proto public string ReflectionType::__toString()
Return the text of the type hint */
ZEND_METHOD(reflection_type, __toString)
{
reflection_object *intern;
typeannotation_reference *param;
type_reference *param;

if (zend_parse_parameters_none() == FAILURE) {
return;
Expand All @@ -2994,10 +2988,18 @@ ZEND_METHOD(reflection_typeannotation, __toString)
switch (param->arg_info->type_hint) {
case IS_ARRAY: RETURN_STRINGL("array", sizeof("array") - 1);
case IS_CALLABLE: RETURN_STRINGL("callable", sizeof("callable") - 1);
case IS_OBJECT: RETURN_STR(zend_string_copy(param->arg_info->class_name));
default:
php_error_docref(NULL, E_ERROR, "Unknown type annotation: %d", (int)param->arg_info->type_hint);
RETURN_EMPTY_STRING();
case IS_OBJECT:
if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
if (!(param->fptr->internal_function.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
RETURN_STRING(((zend_internal_arg_info*)param->arg_info)->class_name);
}
}
RETURN_STR_COPY(param->arg_info->class_name);
case IS_STRING: RETURN_STRINGL("string", sizeof("string") - 1);
case _IS_BOOL: RETURN_STRINGL("bool", sizeof("bool") - 1);
case IS_LONG: RETURN_STRINGL("int", sizeof("int") - 1);
case IS_DOUBLE: RETURN_STRINGL("float", sizeof("float") - 1);
EMPTY_SWITCH_DEFAULT_CASE()
}
}
/* }}} */
Expand Down Expand Up @@ -3514,6 +3516,44 @@ ZEND_METHOD(reflection_function, getShortName)
}
/* }}} */

/* {{{ proto public bool ReflectionFunctionAbstract:hasReturnType()
Return whether the function has a return type */
ZEND_METHOD(reflection_function, hasReturnType)
{
reflection_object *intern;
zend_function *fptr;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

GET_REFLECTION_OBJECT_PTR(fptr);

RETVAL_BOOL(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE);
}
/* }}} */

/* {{{ proto public string ReflectionFunctionAbstract::getReturnType()
Returns the return type associated with the function */
ZEND_METHOD(reflection_function, getReturnType)
{
reflection_object *intern;
zend_function *fptr;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

GET_REFLECTION_OBJECT_PTR(fptr);

if (!(fptr->op_array.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
RETURN_NULL();
}

reflection_type_factory(_copy_function(fptr), Z_ISUNDEF(intern->obj)? NULL : &intern->obj, &fptr->common.arg_info[-1], return_value);
}
/* }}} */

/* {{{ proto public bool ReflectionMethod::isConstructor()
Returns whether this method is the constructor */
ZEND_METHOD(reflection_method, isConstructor)
Expand Down Expand Up @@ -6119,6 +6159,8 @@ static const zend_function_entry reflection_function_abstract_functions[] = {
ZEND_ME(reflection_function, getStartLine, arginfo_reflection__void, 0)
ZEND_ME(reflection_function, getStaticVariables, arginfo_reflection__void, 0)
ZEND_ME(reflection_function, returnsReference, arginfo_reflection__void, 0)
ZEND_ME(reflection_function, hasReturnType, arginfo_reflection__void, 0)
ZEND_ME(reflection_function, getReturnType, arginfo_reflection__void, 0)
PHP_FE_END
};

Expand Down Expand Up @@ -6199,7 +6241,7 @@ static const zend_function_entry reflection_method_functions[] = {
ZEND_ME(reflection_method, invokeArgs, arginfo_reflection_method_invokeArgs, 0)
ZEND_ME(reflection_method, getDeclaringClass, arginfo_reflection__void, 0)
ZEND_ME(reflection_method, getPrototype, arginfo_reflection__void, 0)
ZEND_ME(reflection_property, setAccessible, arginfo_reflection_method_setAccessible, 0)
ZEND_ME(reflection_method, setAccessible, arginfo_reflection_method_setAccessible, 0)
PHP_FE_END
};

Expand Down Expand Up @@ -6416,8 +6458,8 @@ static const zend_function_entry reflection_parameter_functions[] = {
ZEND_ME(reflection_parameter, getDeclaringFunction, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getDeclaringClass, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getClass, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, hasTypeAnnotation, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getTypeAnnotation, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, hasType, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getType, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, isArray, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, isCallable, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, allowsNull, arginfo_reflection__void, 0)
Expand All @@ -6431,12 +6473,11 @@ static const zend_function_entry reflection_parameter_functions[] = {
PHP_FE_END
};

static const zend_function_entry reflection_typeannotation_functions[] = {
static const zend_function_entry reflection_type_functions[] = {
ZEND_ME(reflection, __clone, arginfo_reflection__void, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
ZEND_ME(reflection_typeannotation, isArray, arginfo_reflection__void, 0)
ZEND_ME(reflection_typeannotation, isCallable, arginfo_reflection__void, 0)
ZEND_ME(reflection_typeannotation, isNullable, arginfo_reflection__void, 0)
ZEND_ME(reflection_typeannotation, __toString, arginfo_reflection__void, 0)
ZEND_ME(reflection_type, allowsNull, arginfo_reflection__void, 0)
ZEND_ME(reflection_type, isBuiltin, arginfo_reflection__void, 0)
ZEND_ME(reflection_type, __toString, arginfo_reflection__void, 0)
PHP_FE_END
};

Expand Down Expand Up @@ -6553,9 +6594,9 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
zend_class_implements(reflection_parameter_ptr, 1, reflector_ptr);
zend_declare_property_string(reflection_parameter_ptr, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC);

INIT_CLASS_ENTRY(_reflection_entry, "ReflectionTypeAnnotation", reflection_typeannotation_functions);
INIT_CLASS_ENTRY(_reflection_entry, "ReflectionType", reflection_type_functions);
_reflection_entry.create_object = reflection_objects_new;
reflection_typeannotation_ptr = zend_register_internal_class(&_reflection_entry);
reflection_type_ptr = zend_register_internal_class(&_reflection_entry);

INIT_CLASS_ENTRY(_reflection_entry, "ReflectionMethod", reflection_method_functions);
_reflection_entry.create_object = reflection_objects_new;
Expand Down
1 change: 1 addition & 0 deletions ext/reflection/php_reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern PHPAPI zend_class_entry *reflection_ptr;
extern PHPAPI zend_class_entry *reflection_function_abstract_ptr;
extern PHPAPI zend_class_entry *reflection_function_ptr;
extern PHPAPI zend_class_entry *reflection_parameter_ptr;
extern PHPAPI zend_class_entry *reflection_type_ptr;
extern PHPAPI zend_class_entry *reflection_class_ptr;
extern PHPAPI zend_class_entry *reflection_object_ptr;
extern PHPAPI zend_class_entry *reflection_method_ptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ array(14) {
["name"]=>
string(19) "ReflectionParameter"
}
["ReflectionTypeAnnotation"]=>
["ReflectionType"]=>
object(ReflectionClass)#9 (1) {
["name"]=>
string(24) "ReflectionTypeAnnotation"
string(14) "ReflectionType"
}
["ReflectionMethod"]=>
object(ReflectionClass)#10 (1) {
Expand Down
Loading

0 comments on commit ec281fe

Please sign in to comment.