Skip to content

Object and Filter Support #5

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

Merged
merged 6 commits into from
Apr 6, 2020
Merged
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
File renamed without changes.
File renamed without changes.
112 changes: 112 additions & 0 deletions Zend/tests/attributes/filter.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
--TEST--
Attributes can be filtered by name and base type.
--FILE--
<?php

$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { });
$attr = $ref->getAttributes(A3::class);

var_dump(count($attr));

$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { });
$attr = $ref->getAttributes(A2::class);

var_dump(count($attr), $attr[0]->getName());

$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A2>> function () { });
$attr = $ref->getAttributes(A2::class);

var_dump(count($attr), $attr[0]->getName(), $attr[1]->getName());

echo "\n";

interface Base { }
class A1 implements Base { }
class A2 implements Base { }
class A3 extends A2 { }

$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A5>> function () { });
$attr = $ref->getAttributes(\stdClass::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { });
$attr = $ref->getAttributes(A1::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { });
$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A3>> function () { });
$attr = $ref->getAttributes(A2::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A3>> function () { });
$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

echo "\n";

$ref = new \ReflectionFunction(function () { });

try {
$ref->getAttributes(A1::class, 3);
} catch (\Error $e) {
var_dump('ERROR 1', $e->getMessage());
}

$ref = new \ReflectionFunction(function () { });

try {
$ref->getAttributes(SomeMissingClass::class, \ReflectionAttribute::IS_INSTANCEOF);
} catch (\Error $e) {
var_dump('ERROR 2', $e->getMessage());
}

?>
--EXPECT--
int(0)
int(1)
string(2) "A2"
int(2)
string(2) "A2"
string(2) "A2"

int(0)
Array
(
)
int(1)
Array
(
[0] => A1
)
int(2)
Array
(
[0] => A1
[1] => A2
)
int(2)
Array
(
[0] => A2
[1] => A3
)
int(3)
Array
(
[0] => A1
[1] => A2
[2] => A3
)

string(7) "ERROR 1"
string(39) "Invalid attribute filter flag specified"
string(7) "ERROR 2"
string(34) "Class 'SomeMissingClass' not found"
102 changes: 102 additions & 0 deletions Zend/tests/attributes/objects.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
--TEST--
Attributes can be converted into objects.
--FILE--
<?php

class A1
{
public string $name;
public int $ttl;

public function __construct(string $name, int $ttl = 50)
{
$this->name = $name;
$this->ttl = $ttl;
}
}

$ref = new \ReflectionFunction(<<A1('test')>> function () { });

foreach ($ref->getAttributes() as $attr) {
$obj = $attr->getAsObject();

var_dump(get_class($obj), $obj->name, $obj->ttl);
}

echo "\n";

$ref = new \ReflectionFunction(<<A1>> function () { });

try {
$ref->getAttributes()[0]->getAsObject();
} catch (\ArgumentCountError $e) {
var_dump('ERROR 1', $e->getMessage());
}

echo "\n";

$ref = new \ReflectionFunction(<<A1([])>> function () { });

try {
$ref->getAttributes()[0]->getAsObject();
} catch (\TypeError $e) {
var_dump('ERROR 2', $e->getMessage());
}

echo "\n";

$ref = new \ReflectionFunction(<<A2>> function () { });

try {
$ref->getAttributes()[0]->getAsObject();
} catch (\Error $e) {
var_dump('ERROR 3', $e->getMessage());
}

echo "\n";

class A3
{
private function __construct() { }
}

$ref = new \ReflectionFunction(<<A3>> function () { });

try {
$ref->getAttributes()[0]->getAsObject();
} catch (\Error $e) {
var_dump('ERROR 4', $e->getMessage());
}

echo "\n";

class A4 { }

$ref = new \ReflectionFunction(<<A4(1)>> function () { });

try {
$ref->getAttributes()[0]->getAsObject();
} catch (\Error $e) {
var_dump('ERROR 5', $e->getMessage());
}

?>
--EXPECT--
string(2) "A1"
string(4) "test"
int(50)

string(7) "ERROR 1"
string(81) "Too few arguments to function A1::__construct(), 0 passed and at least 1 expected"

string(7) "ERROR 2"
string(74) "A1::__construct(): Argument #1 ($name) must be of type string, array given"

string(7) "ERROR 3"
string(30) "Attribute class 'A2' not found"

string(7) "ERROR 4"
string(50) "Attribute constructor of class 'A3' must be public"

string(7) "ERROR 5"
string(71) "Attribute class 'A4' does not have a constructor, cannot pass arguments"
File renamed without changes.
12 changes: 3 additions & 9 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5729,19 +5729,14 @@ static void zend_compile_attribute(zval *v, zend_ast *ast) /* {{{ */
if (ast->child[1]) {
zend_ast_list *list;
uint32_t i;

zval tmp;
zval *x;

ZEND_ASSERT(ast->child[1]->kind == ZEND_AST_ARG_LIST);

ZVAL_NULL(&tmp);

for (list = zend_ast_get_list(ast->child[1]), i = 0; i < list->children; i++) {
zend_ast *el = list->child[i];

x = zend_hash_next_index_insert(Z_ARRVAL_P(v), &tmp);
zend_const_expr_to_zval(x, el);
zend_const_expr_to_zval(zend_hash_next_index_insert(Z_ARRVAL_P(v), &tmp), list->child[i]);
}
}
}
Expand Down Expand Up @@ -5782,14 +5777,13 @@ static HashTable *zend_compile_attributes(zend_ast *ast) /* {{{ */
add_next_index_zval(x, &a);
} else {
zval array;
zval ref;

ZEND_ASSERT(Z_TYPE_P(zend_hash_index_find(Z_ARRVAL_P(x), 0)) == IS_STRING);

ZVAL_COPY(&ref, x);
Z_ADDREF_P(x);

array_init(&array);
add_next_index_zval(&array, &ref);
add_next_index_zval(&array, x);
add_next_index_zval(&array, &a);
zend_hash_update(attr, name, &array);
}
Expand Down
Loading