Skip to content

Implement Changes Requested in PR Review #8

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 5 commits into from
May 1, 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
1 change: 1 addition & 0 deletions Zend/tests/attributes/002_rfcexample.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace {
var_dump($attributes[0]->getArguments());
var_dump($attributes[0]->newInstance());
}
?>
--EXPECTF--
string(28) "My\Attributes\SingleArgument"
array(1) {
Expand Down
32 changes: 32 additions & 0 deletions Zend/tests/attributes/003_ast_nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,37 @@ var_dump(count($args));
var_dump($args[0] === 'foo');
var_dump($args[1] === C1::BAR);

echo "\n";

<<ExampleWithShift(4 >> 1)>>
class C4 {}
$ref = new \ReflectionClass(C4::class);
var_dump($ref->getAttributes()[0]->getArguments());

echo "\n";

<<PhpAttribute>>
class C5
{
public function __construct() { }
}

$ref = new \ReflectionFunction(<<C5(MissingClass::SOME_CONST)>> function () { });
$attr = $ref->getAttributes();
var_dump(count($attr));

try {
$attr[0]->getArguments();
} catch (\Error $e) {
var_dump($e->getMessage());
}

try {
$attr[0]->newInstance();
} catch (\Error $e) {
var_dump($e->getMessage());
}

?>
--EXPECT--
int(1)
Expand All @@ -71,7 +97,13 @@ int(1)
int(2)
bool(true)
bool(true)

array(1) {
[0]=>
int(2)
}

int(1)
string(30) "Class 'MissingClass' not found"
string(30) "Class 'MissingClass' not found"

1 change: 1 addition & 0 deletions Zend/tests/attributes/004_name_resolution.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Foo {
namespace {
dump_attributes((new ReflectionFunction('Foo\foo'))->getAttributes());
}
?>
--EXPECTF--
array(1) {
["Doctrine\ORM\Mapping\Entity"]=>
Expand Down
18 changes: 18 additions & 0 deletions Zend/tests/attributes/005_objects.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Attributes can be converted into objects.
--FILE--
<?php

<<PhpAttribute>>
class A1
{
public string $name;
Expand Down Expand Up @@ -55,6 +56,7 @@ try {

echo "\n";

<<PhpAttribute>>
class A3
{
private function __construct() { }
Expand All @@ -70,6 +72,7 @@ try {

echo "\n";

<<PhpAttribute>>
class A4 { }

$ref = new \ReflectionFunction(<<A4(1)>> function () { });
Expand All @@ -80,6 +83,18 @@ try {
var_dump('ERROR 5', $e->getMessage());
}

echo "\n";

class A5 { }

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

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

?>
--EXPECT--
string(2) "A1"
Expand All @@ -100,3 +115,6 @@ 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"

string(7) "ERROR 6"
string(78) "Attempting to use class 'A5' as attribute that does not have <<PhpAttribute>>."
11 changes: 11 additions & 0 deletions Zend/tests/attributes/010_unsupported_const_expression.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Attribute arguments support only const expressions.
--FILE--
<?php

<<A1(foo())>>
class C1 { }

?>
--EXPECTF--
Fatal error: Constant expression contains invalid operations in %s
99 changes: 99 additions & 0 deletions Zend/tests/attributes/011-inheritance.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
--TEST--
Attributes comply with inheritance rules.
--FILE--
<?php

<<A2>>
class C1
{
<<A1>>
public function foo() { }
}

class C2 extends C1
{
public function foo() { }
}

class C3 extends C1
{
<<A1>>
public function bar() { }
}

$ref = new \ReflectionClass(C1::class);
print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));

$ref = new \ReflectionClass(C2::class);
print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));

$ref = new \ReflectionClass(C3::class);
print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));

echo "\n";

trait T1
{
<<A2>>
public $a;
}

class C4
{
use T1;
}

class C5
{
use T1;

public $a;
}

$ref = new \ReflectionClass(T1::class);
print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));

$ref = new \ReflectionClass(C4::class);
print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));

$ref = new \ReflectionClass(C5::class);
print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));

?>
--EXPECT--
Array
(
[0] => A2
)
Array
(
[0] => A1
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => A1
)

Array
(
[0] => A2
)
Array
(
[0] => A2
)
Array
(
)
3 changes: 1 addition & 2 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -2141,8 +2141,7 @@ zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
ast = zend_ast_create(ZEND_AST_CLASS_CONST_DECL_ATTRIBUTES, ast, attr);
ast->lineno = ast->child[0]->lineno;
break;
default:
zend_error_noreturn(E_COMPILE_ERROR, "Invalid use of attributes");
EMPTY_SWITCH_DEFAULT_CASE()
}

return ast;
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#include "zend_API.h"
#include "zend_attributes.h"

void zend_attribute_validate_phpattribute(zval *attribute, int target)
void zend_attribute_validate_phpattribute(zend_attribute *attr, int target)
{
if (target != ZEND_ATTRIBUTE_TARGET_CLASS) {
zend_error(E_COMPILE_ERROR, "Only classes can be marked with <<PhpAttribute>>");
}
}

void zend_attribute_validate_phpcompilerattribute(zval *attribute, int target)
void zend_attribute_validate_phpcompilerattribute(zend_attribute *attr, int target)
{
zend_error(E_COMPILE_ERROR, "The PhpCompilerAttribute can only be used by internal classes, use PhpAttribute instead");
}
Expand Down
54 changes: 53 additions & 1 deletion Zend/zend_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,59 @@
zend_class_entry *zend_ce_php_attribute;
zend_class_entry *zend_ce_php_compiler_attribute;

typedef void (*zend_attributes_internal_validator)(zval *attribute, int target);
#define ZEND_ATTRIBUTE_SIZE(argc) (sizeof(zend_attribute) + sizeof(zval) * (argc) - sizeof(zval))

typedef struct _zend_attribute {
zend_string *name;
zend_string *lcname;
uint32_t offset;
uint32_t argc;
zval argv[1];
} zend_attribute;

static zend_always_inline void zend_attribute_release(zend_attribute *attr)
{
uint32_t i;

zend_string_release(attr->name);
zend_string_release(attr->lcname);

for (i = 0; i < attr->argc; i++) {
zval_ptr_dtor(&attr->argv[i]);
}

efree(attr);
}

static zend_always_inline zend_bool zend_has_attribute(HashTable *attributes, zend_string *name, uint32_t offset)
{
if (attributes) {
zend_attribute *attr;

ZEND_HASH_FOREACH_PTR(attributes, attr) {
if (attr->offset == offset && zend_string_equals(attr->lcname, name)) {
return 1;
}
} ZEND_HASH_FOREACH_END();
}

return 0;
}

static zend_always_inline zend_bool zend_has_attribute_str(HashTable *attributes, const char *str, size_t len, uint32_t offset)
{
zend_bool result = 0;

if (attributes) {
zend_string *name = zend_string_init(str, len, 0);
result = zend_has_attribute(attributes, name, offset);
zend_string_release(name);
}

return result;
}

typedef void (*zend_attributes_internal_validator)(zend_attribute *attr, int target);
HashTable zend_attributes_internal_validators;

void zend_compiler_attribute_register(zend_class_entry *ce, zend_attributes_internal_validator validator);
Expand Down
Loading