Skip to content

Attribute Amendments #5751

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

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
6 changes: 3 additions & 3 deletions Zend/tests/attributes/002_rfcexample.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Attributes: Example from Attributes RFC
<?php
// https://wiki.php.net/rfc/attributes_v2#attribute_syntax
namespace My\Attributes {
use PhpAttribute;
use Attribute;

<<PhpAttribute>>
<<Attribute>>
class SingleArgument {
public $argumentValue;

Expand Down Expand Up @@ -37,7 +37,7 @@ array(1) {
[0]=>
string(11) "Hello World"
}
object(My\Attributes\SingleArgument)#3 (1) {
object(My\Attributes\SingleArgument)#%d (1) {
["argumentValue"]=>
string(11) "Hello World"
}
2 changes: 1 addition & 1 deletion Zend/tests/attributes/003_ast_nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var_dump($ref->getAttributes()[0]->getArguments());

echo "\n";

<<PhpAttribute>>
<<Attribute>>
class C5
{
public function __construct() { }
Expand Down
8 changes: 4 additions & 4 deletions Zend/tests/attributes/005_objects.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Attributes can be converted into objects.
--FILE--
<?php

<<PhpAttribute>>
<<Attribute(Attribute::TARGET_FUNCTION)>>
class A1
{
public string $name;
Expand Down Expand Up @@ -56,7 +56,7 @@ try {

echo "\n";

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

echo "\n";

<<PhpAttribute>>
<<Attribute>>
class A4 { }

$ref = new \ReflectionFunction(<<A4(1)>> function () { });
Expand Down Expand Up @@ -117,4 +117,4 @@ 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>>."
string(55) "Attempting to use non-attribute class 'A5' as attribute"
15 changes: 9 additions & 6 deletions Zend/tests/attributes/007_self_reflect_attribute.phpt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
--TEST--
Attributes: attributes on PhpAttribute return itself
Attributes: attributes on Attribute return itself
--FILE--
<?php

$reflection = new \ReflectionClass(PhpAttribute::class);
$reflection = new \ReflectionClass(Attribute::class);
$attributes = $reflection->getAttributes();

foreach ($attributes as $attribute) {
var_dump($attribute->getName());
var_dump($attribute->getArguments());
var_dump($attribute->newInstance());

$a = $attribute->newInstance();
var_dump(get_class($a));
var_dump($a->flags == Attribute::TARGET_ALL);
}
--EXPECTF--
string(12) "PhpAttribute"
string(9) "Attribute"
array(0) {
}
object(PhpAttribute)#3 (0) {
}
string(9) "Attribute"
bool(true)
6 changes: 3 additions & 3 deletions Zend/tests/attributes/008_wrong_attribution.phpt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
--TEST--
Attributes: Prevent PhpAttribute on non classes
Attributes: Prevent Attribute on non classes
--FILE--
<?php

<<PhpAttribute>>
<<Attribute>>
function foo() {}
--EXPECTF--
Fatal error: Only classes can be marked with <<PhpAttribute>> in %s
Fatal error: Attribute "Attribute" cannot target function (allowed targets: class) in %s
70 changes: 70 additions & 0 deletions Zend/tests/attributes/020_userland_attribute_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--TEST--
Attributes expose and verify target and repeatable data.
--FILE--
<?php

<<Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)>>
class A1 { }

$ref = new \ReflectionFunction(<<A1>> function () { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_FUNCTION, $attr->isRepeated());
var_dump(get_class($attr->newInstance()));

echo "\n";

$ref = new \ReflectionObject(new <<A1>> class() { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_CLASS, $attr->isRepeated());

try {
$attr->newInstance();
} catch (\Throwable $e) {
var_dump('ERROR 1', $e->getMessage());
}

echo "\n";

$ref = new \ReflectionFunction(<<A1>> <<A1>> function () { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_FUNCTION, $attr->isRepeated());

try {
$attr->newInstance();
} catch (\Throwable $e) {
var_dump('ERROR 2', $e->getMessage());
}

echo "\n";

<<Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)>>
class A2 { }

$ref = new \ReflectionObject(new <<A2>> <<A2>> class() { });
$attr = $ref->getAttributes()[0];
var_dump($attr->getName(), $attr->getTarget() == Attribute::TARGET_CLASS, $attr->isRepeated());
var_dump(get_class($attr->newInstance()));

?>
--EXPECT--
string(2) "A1"
bool(true)
bool(false)
string(2) "A1"

string(2) "A1"
bool(true)
bool(false)
string(7) "ERROR 1"
string(70) "Attribute "A1" cannot target class (allowed targets: function, method)"

string(2) "A1"
bool(true)
bool(true)
string(7) "ERROR 2"
string(35) "Attribute "A1" must not be repeated"

string(2) "A2"
bool(true)
bool(true)
string(2) "A2"
11 changes: 11 additions & 0 deletions Zend/tests/attributes/021_attribute_flags_type_is_validated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Attribute flags type is validated.
--FILE--
<?php

<<Attribute("foo")>>
class A1 { }

?>
--EXPECTF--
Fatal error: Attribute::__construct(): Argument #1 ($flags) must must be of type int, string given in %s
11 changes: 11 additions & 0 deletions Zend/tests/attributes/022_attribute_flags_value_is_validated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Attribute flags value is validated.
--FILE--
<?php

<<Attribute(-1)>>
class A1 { }

?>
--EXPECTF--
Fatal error: Invalid attribute flags specified in %s
11 changes: 11 additions & 0 deletions Zend/tests/attributes/023_ast_node_in_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Attribute flags value is validated.
--FILE--
<?php

<<Attribute(Foo::BAR)>>
class A1 { }

?>
--EXPECTF--
Fatal error: Class 'Foo' not found in %s
11 changes: 11 additions & 0 deletions Zend/tests/attributes/024_internal_target_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Internal attribute targets are validated.
--FILE--
<?php

<<Attribute>>
function a1() { }

?>
--EXPECTF--
Fatal error: Attribute "Attribute" cannot target function (allowed targets: class) in %s
12 changes: 12 additions & 0 deletions Zend/tests/attributes/025_internal_repeatable_validation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Internal attribute targets are validated.
--FILE--
<?php

<<Attribute>>
<<Attribute>>
class A1 { }

?>
--EXPECTF--
Fatal error: Attribute "Attribute" must not be repeated in %s
Loading