summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/reflection/php_reflection.c77
-rw-r--r--ext/reflection/tests/ReflectionClass_toString_001.phpt20
-rw-r--r--ext/reflection/tests/traits004.phpt58
-rw-r--r--ext/reflection/tests/traits005.phpt41
4 files changed, 195 insertions, 1 deletions
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index a590bf5..a3aaf3e 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -4254,6 +4254,80 @@ ZEND_METHOD(reflection_class, getInterfaceNames)
}
/* }}} */
+/* {{{ proto public ReflectionClass[] ReflectionClass::getTraits()
+ Returns an array of traits used by this class */
+ZEND_METHOD(reflection_class, getTraits)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+ zend_uint i;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ array_init(return_value);
+
+ for (i=0; i < ce->num_traits; i++) {
+ zval *trait;
+ ALLOC_ZVAL(trait);
+ zend_reflection_class_factory(ce->traits[i], trait TSRMLS_CC);
+ add_assoc_zval_ex(return_value, ce->traits[i]->name, ce->traits[i]->name_length + 1, trait);
+ }
+}
+/* }}} */
+
+/* {{{ proto public String[] ReflectionClass::getTraitNames()
+ Returns an array of names of traits used by this class */
+ZEND_METHOD(reflection_class, getTraitNames)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+ zend_uint i;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ array_init(return_value);
+
+ for (i=0; i < ce->num_traits; i++) {
+ add_next_index_stringl(return_value, ce->traits[i]->name, ce->traits[i]->name_length, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto public arra ReflectionClass::getTraitaliases()
+ Returns an array of trait aliases */
+ZEND_METHOD(reflection_class, getTraitAliases)
+{
+ reflection_object *intern;
+ zend_class_entry *ce;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+ GET_REFLECTION_OBJECT_PTR(ce);
+
+ array_init(return_value);
+
+ if (ce->trait_aliases) {
+ zend_uint i = 0;
+ while (ce->trait_aliases[i]) {
+ char *method_name;
+ int method_name_len;
+ zend_trait_method_reference *cur_ref = ce->trait_aliases[i]->trait_method;
+
+ method_name_len = spprintf(&method_name, 0, "%s::%s", cur_ref->class_name, cur_ref->method_name);
+ add_assoc_stringl_ex(return_value, ce->trait_aliases[i]->alias, ce->trait_aliases[i]->alias_len + 1, method_name, method_name_len, 0);
+ i++;
+ }
+ }
+}
+/* }}} */
+
/* {{{ proto public ReflectionClass ReflectionClass::getParentClass()
Returns the class' parent class, or, if none exists, FALSE */
ZEND_METHOD(reflection_class, getParentClass)
@@ -5636,6 +5710,9 @@ static const zend_function_entry reflection_class_functions[] = {
ZEND_ME(reflection_class, getInterfaces, arginfo_reflection__void, 0)
ZEND_ME(reflection_class, getInterfaceNames, arginfo_reflection__void, 0)
ZEND_ME(reflection_class, isInterface, arginfo_reflection__void, 0)
+ ZEND_ME(reflection_class, getTraits, arginfo_reflection__void, 0)
+ ZEND_ME(reflection_class, getTraitNames, arginfo_reflection__void, 0)
+ ZEND_ME(reflection_class, getTraitAliases, arginfo_reflection__void, 0)
ZEND_ME(reflection_class, isTrait, arginfo_reflection__void, 0)
ZEND_ME(reflection_class, isAbstract, arginfo_reflection__void, 0)
ZEND_ME(reflection_class, isFinal, arginfo_reflection__void, 0)
diff --git a/ext/reflection/tests/ReflectionClass_toString_001.phpt b/ext/reflection/tests/ReflectionClass_toString_001.phpt
index 790ee4e..a593147 100644
--- a/ext/reflection/tests/ReflectionClass_toString_001.phpt
+++ b/ext/reflection/tests/ReflectionClass_toString_001.phpt
@@ -34,7 +34,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
Property [ <default> public $name ]
}
- - Methods [44] {
+ - Methods [47] {
Method [ <internal:Reflection> final private method __clone ] {
- Parameters [0] {
@@ -188,6 +188,24 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {
}
}
+ Method [ <internal:Reflection> public method getTraits ] {
+
+ - Parameters [0] {
+ }
+ }
+
+ Method [ <internal:Reflection> public method getTraitNames ] {
+
+ - Parameters [0] {
+ }
+ }
+
+ Method [ <internal:Reflection> public method getTraitAliases ] {
+
+ - Parameters [0] {
+ }
+ }
+
Method [ <internal:Reflection> public method isTrait ] {
- Parameters [0] {
diff --git a/ext/reflection/tests/traits004.phpt b/ext/reflection/tests/traits004.phpt
new file mode 100644
index 0000000..c9367c1
--- /dev/null
+++ b/ext/reflection/tests/traits004.phpt
@@ -0,0 +1,58 @@
+--TEST--
+ReflectionClass::getTraits() and ReflectionClass::getTraitNames
+--FILE--
+<?php
+trait T1 { }
+trait T2 { }
+
+class C1 { }
+class C2 { use T1; }
+class C3 { use T1; use T2; }
+
+for ($c = "C1"; $c <= "C3"; $c++) {
+ echo "class $c:\n";
+ $r = new ReflectionClass($c);
+ var_dump($r->getTraitNames());
+ var_dump($r->getTraits());
+ echo "\n";
+}
+--EXPECT--
+class C1:
+array(0) {
+}
+array(0) {
+}
+
+class C2:
+array(1) {
+ [0]=>
+ string(2) "T1"
+}
+array(1) {
+ ["T1"]=>
+ &object(ReflectionClass)#1 (1) {
+ ["name"]=>
+ string(2) "T1"
+ }
+}
+
+class C3:
+array(2) {
+ [0]=>
+ string(2) "T1"
+ [1]=>
+ string(2) "T2"
+}
+array(2) {
+ ["T1"]=>
+ &object(ReflectionClass)#2 (1) {
+ ["name"]=>
+ string(2) "T1"
+ }
+ ["T2"]=>
+ &object(ReflectionClass)#3 (1) {
+ ["name"]=>
+ string(2) "T2"
+ }
+}
+
diff --git a/ext/reflection/tests/traits005.phpt b/ext/reflection/tests/traits005.phpt
new file mode 100644
index 0000000..1496a35
--- /dev/null
+++ b/ext/reflection/tests/traits005.phpt
@@ -0,0 +1,41 @@
+--TEST--
+ReflectionClass::getTraitAlias
+--FILE--
+<?php
+trait T1 { function m1() { } function m2() { } }
+
+class C1 { }
+class C2 { use T1; }
+class C3 { use T1 { m1 as a1; } }
+class C4 { use T1 { m1 as a1; m2 as a2; } }
+
+for ($c = "C1"; $c <= "C4"; $c++) {
+ echo "class $c:\n";
+ $r = new ReflectionClass($c);
+ var_dump($r->getTraitAliases());
+ echo "\n";
+}
+?>
+--EXPECT--
+class C1:
+array(0) {
+}
+
+class C2:
+array(0) {
+}
+
+class C3:
+array(1) {
+ ["a1"]=>
+ string(10) "(null)::m1"
+}
+
+class C4:
+array(2) {
+ ["a1"]=>
+ string(10) "(null)::m1"
+ ["a2"]=>
+ string(10) "(null)::m2"
+}
+