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

Add support for checking trait usage to class_uses() #26

Closed
wants to merge 1 commit 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
29 changes: 24 additions & 5 deletions ext/spl/php_spl.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,13 @@ PHP_FUNCTION(class_uses)
{
zval *obj;
zend_bool autoload = 1;
char *trait_name = NULL;
int trait_name_len;
int i;
zend_class_entry *ce;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &obj, &autoload) == FAILURE) {
zend_class_entry **trait_ce;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bs", &obj, &autoload, &trait_name, &trait_name_len) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
Expand All @@ -173,9 +177,24 @@ PHP_FUNCTION(class_uses)
} else {
ce = Z_OBJCE_P(obj);
}

array_init(return_value);
spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT TSRMLS_CC);

if (trait_name == NULL) {
array_init(return_value);
spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT TSRMLS_CC);
} else {
if (zend_lookup_class_ex(trait_name, trait_name_len, NULL, 0, &trait_ce TSRMLS_CC) != FAILURE) {
while (ce) {
for (i=0; i < ce->num_traits; i++) {
if (ce->traits[i] == *trait_ce) {
RETURN_TRUE;
}
}
ce = ce->parent;
}
}

RETURN_FALSE;
}
}
/* }}} */

Expand Down
30 changes: 30 additions & 0 deletions ext/spl/tests/class_uses_basic3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
SPL: Test class_implements() function : basic
--FILE--
<?php
/* Prototype : array class_uses(mixed what [, bool autoload ], [, string trait_name])
* Description: Return all traits used by a class or check if a trait is used
* Source code: ext/spl/php_spl.c
* Alias to functions:
*/

echo "*** Testing class_uses() : basic ***\n";


trait foo { }
class bar { use foo; }

var_dump(class_uses(new bar, false, 'foo'));
var_dump(class_uses('bar', false, 'foo'));
var_dump(class_uses(new bar, false, 'bar'));
var_dump(class_uses(new bar, false, 'bat'));

?>
===DONE===
--EXPECT--
*** Testing class_uses() : basic ***
bool(true)
bool(true)
bool(false)
bool(false)
===DONE===
72 changes: 72 additions & 0 deletions ext/spl/tests/class_uses_basic4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--TEST--
SPL: Test class_uses() function : basic
--FILE--
<?php
/* Prototype : array class_uses(mixed what [, bool autoload ], [, string trait_name])
* Description: Return all traits used by a class or check if a trait is used
* Source code: ext/spl/php_spl.c
* Alias to functions:
*/

echo "*** Testing class_uses() : basic ***\n";


trait foo { }
class fooUser { use foo; }

trait bar { }
class barUser { use bar; }

class foobarUser { use foo, bar; }

/** There is no semantics for traits in the inheritance chain.
Traits are flattend into a class, and that semantics is nothing
like a type, or interface, and thus, not propergated. */
class fooViaBarUser extends barUser { use foo; }

class fooExtended extends fooUser {}

var_dump(class_uses(new foobarUser, false, 'foo'));
var_dump(class_uses(new foobarUser, false, 'bar'));
var_dump(class_uses(new foobarUser, false, 'baz'));
var_dump(class_uses('foobarUser', false, 'foo'));
var_dump(class_uses('foobarUser', false, 'bar'));
var_dump(class_uses('foobarUser', false, 'baz'));

var_dump(class_uses(new fooViaBarUser, false, 'foo'));
var_dump(class_uses(new fooViaBarUser, false, 'bar'));
var_dump(class_uses(new fooViaBarUser, false, 'baz'));
var_dump(class_uses('fooViaBarUser', false, 'foo'));
var_dump(class_uses('fooViaBarUser', false, 'bar'));
var_dump(class_uses('fooViaBarUser', false, 'baz'));

var_dump(class_uses(new fooExtended, false, 'foo'));
var_dump(class_uses(new fooExtended, false, 'bar'));
var_dump(class_uses(new fooExtended, false, 'baz'));
var_dump(class_uses('fooExtended', false, 'foo'));
var_dump(class_uses('fooExtended', false, 'bar'));
var_dump(class_uses('fooExtended', false, 'baz'));

?>
===DONE===
--EXPECT--
*** Testing class_uses() : basic ***
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
===DONE===