Skip to content

Commit 299c20c

Browse files
theodorejbkooldev
andcommitted
Implement Shorter Attribute Syntax
RFC: https://wiki.php.net/rfc/shorter_attribute_syntax Co-authored-by: Martin Schröder <m.schroeder2007@gmail.com>
1 parent e2be500 commit 299c20c

35 files changed

+153
-134
lines changed

Zend/tests/attributes/001_placement.phpt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ Attributes can be placed on all supported elements.
33
--FILE--
44
<?php
55

6-
<<A1(1)>>
6+
@@A1(1)
77
class Foo
88
{
9-
<<A1(2)>>
9+
@@A1(2)
1010
public const FOO = 'foo';
1111

12-
<<A1(3)>>
12+
@@A1(3)
1313
public $x;
1414

15-
<<A1(4)>>
16-
public function foo(<<A1(5)>> $a, <<A1(6)>> $b) { }
15+
@@A1(4)
16+
public function foo(@@A1(5) $a, @@A1(6) $b) { }
1717
}
1818

19-
$object = new <<A1(7)>> class () { };
19+
$object = new @@A1(7) class () { };
2020

21-
<<A1(8)>>
21+
@@A1(8)
2222
function f1() { }
2323

24-
$f2 = <<A1(9)>> function () { };
24+
$f2 = @@A1(9) function () { };
2525

26-
$f3 = <<A1(10)>> fn () => 1;
26+
$f3 = @@A1(10) fn () => 1;
2727

2828
$ref = new \ReflectionClass(Foo::class);
2929

Zend/tests/attributes/002_rfcexample.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Attributes: Example from Attributes RFC
66
namespace My\Attributes {
77
use Attribute;
88

9-
<<Attribute>>
9+
@@Attribute
1010
class SingleArgument {
1111
public $argumentValue;
1212

@@ -19,7 +19,7 @@ namespace My\Attributes {
1919
namespace {
2020
use My\Attributes\SingleArgument;
2121

22-
<<SingleArgument("Hello World")>>
22+
@@SingleArgument("Hello World")
2323
class Foo {
2424
}
2525

Zend/tests/attributes/003_ast_nodes.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Attributes can deal with AST nodes.
55

66
define('V1', strtoupper(php_sapi_name()));
77

8-
<<A1([V1 => V1])>>
8+
@@A1([V1 => V1])
99
class C1
1010
{
1111
public const BAR = 'bar';
@@ -20,7 +20,7 @@ var_dump(count($args), $args[0][V1] === V1);
2020

2121
echo "\n";
2222

23-
<<A1(V1, 1 + 2, C1::class)>>
23+
@@A1(V1, 1 + 2, C1::class)
2424
class C2 { }
2525

2626
$ref = new \ReflectionClass(C2::class);
@@ -35,7 +35,7 @@ var_dump($args[2] === C1::class);
3535

3636
echo "\n";
3737

38-
<<A1(self::FOO, C1::BAR)>>
38+
@@A1(self::FOO, C1::BAR)
3939
class C3
4040
{
4141
private const FOO = 'foo';
@@ -52,20 +52,20 @@ var_dump($args[1] === C1::BAR);
5252

5353
echo "\n";
5454

55-
<<ExampleWithShift(4 >> 1)>>
55+
@@ExampleWithShift(4 >> 1)
5656
class C4 {}
5757
$ref = new \ReflectionClass(C4::class);
5858
var_dump($ref->getAttributes()[0]->getArguments());
5959

6060
echo "\n";
6161

62-
<<Attribute>>
62+
@@Attribute
6363
class C5
6464
{
6565
public function __construct() { }
6666
}
6767

68-
$ref = new \ReflectionFunction(<<C5(MissingClass::SOME_CONST)>> function () { });
68+
$ref = new \ReflectionFunction(@@C5(MissingClass::SOME_CONST) function () { });
6969
$attr = $ref->getAttributes();
7070
var_dump(count($attr));
7171

Zend/tests/attributes/004_name_resolution.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ namespace Foo {
2525
use Doctrine\ORM\Mapping as ORM;
2626
use Doctrine\ORM\Attributes;
2727

28-
<<Entity("imported class")>>
29-
<<ORM\Entity("imported namespace")>>
30-
<<\Doctrine\ORM\Mapping\Entity("absolute from namespace")>>
31-
<<\Entity("import absolute from global")>>
32-
<<Attributes\Table()>>
28+
@@Entity("imported class")
29+
@@ORM\Entity("imported namespace")
30+
@@\Doctrine\ORM\Mapping\Entity("absolute from namespace")
31+
@@\Entity("import absolute from global")
32+
@@Attributes\Table()
3333
function foo() {
3434
}
3535
}

Zend/tests/attributes/005_objects.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Attributes can be converted into objects.
33
--FILE--
44
<?php
55

6-
<<Attribute(Attribute::TARGET_FUNCTION)>>
6+
@@Attribute(Attribute::TARGET_FUNCTION)
77
class A1
88
{
99
public string $name;
@@ -16,7 +16,7 @@ class A1
1616
}
1717
}
1818

19-
$ref = new \ReflectionFunction(<<A1('test')>> function () { });
19+
$ref = new \ReflectionFunction(@@A1('test') function () { });
2020

2121
foreach ($ref->getAttributes() as $attr) {
2222
$obj = $attr->newInstance();
@@ -26,7 +26,7 @@ foreach ($ref->getAttributes() as $attr) {
2626

2727
echo "\n";
2828

29-
$ref = new \ReflectionFunction(<<A1>> function () { });
29+
$ref = new \ReflectionFunction(@@A1 function () { });
3030

3131
try {
3232
$ref->getAttributes()[0]->newInstance();
@@ -36,7 +36,7 @@ try {
3636

3737
echo "\n";
3838

39-
$ref = new \ReflectionFunction(<<A1([])>> function () { });
39+
$ref = new \ReflectionFunction(@@A1([]) function () { });
4040

4141
try {
4242
$ref->getAttributes()[0]->newInstance();
@@ -46,7 +46,7 @@ try {
4646

4747
echo "\n";
4848

49-
$ref = new \ReflectionFunction(<<A2>> function () { });
49+
$ref = new \ReflectionFunction(@@A2 function () { });
5050

5151
try {
5252
$ref->getAttributes()[0]->newInstance();
@@ -56,13 +56,13 @@ try {
5656

5757
echo "\n";
5858

59-
<<Attribute>>
59+
@@Attribute
6060
class A3
6161
{
6262
private function __construct() { }
6363
}
6464

65-
$ref = new \ReflectionFunction(<<A3>> function () { });
65+
$ref = new \ReflectionFunction(@@A3 function () { });
6666

6767
try {
6868
$ref->getAttributes()[0]->newInstance();
@@ -72,10 +72,10 @@ try {
7272

7373
echo "\n";
7474

75-
<<Attribute>>
75+
@@Attribute
7676
class A4 { }
7777

78-
$ref = new \ReflectionFunction(<<A4(1)>> function () { });
78+
$ref = new \ReflectionFunction(@@A4(1) function () { });
7979

8080
try {
8181
$ref->getAttributes()[0]->newInstance();
@@ -87,7 +87,7 @@ echo "\n";
8787

8888
class A5 { }
8989

90-
$ref = new \ReflectionFunction(<<A5>> function () { });
90+
$ref = new \ReflectionFunction(@@A5 function () { });
9191

9292
try {
9393
$ref->getAttributes()[0]->newInstance();

Zend/tests/attributes/006_filter.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ Attributes can be filtered by name and base type.
33
--FILE--
44
<?php
55

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

99
var_dump(count($attr));
1010

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

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

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

1919
var_dump(count($attr), $attr[0]->getName(), $attr[1]->getName());
@@ -25,27 +25,27 @@ class A1 implements Base { }
2525
class A2 implements Base { }
2626
class A3 extends A2 { }
2727

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

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

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

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

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

Zend/tests/attributes/008_wrong_attribution.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Attributes: Prevent Attribute on non classes
33
--FILE--
44
<?php
55

6-
<<Attribute>>
6+
@@Attribute
77
function foo() {}
88
--EXPECTF--
99
Fatal error: Attribute "Attribute" cannot target function (allowed targets: class) in %s

Zend/tests/attributes/009_doctrine_annotations_example.phpt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@ namespace {
2525
use Doctrine\ORM\Attributes as ORM;
2626
use Symfony\Component\Validator\Constraints as Assert;
2727

28-
<<ORM\Entity>>
28+
@@ORM\Entity
2929
/** @ORM\Entity */
3030
class User
3131
{
3232
/** @ORM\Id @ORM\Column(type="integer"*) @ORM\GeneratedValue */
33-
<<ORM\Id>><<ORM\Column("integer")>><<ORM\GeneratedValue>>
33+
@@ORM\Id
34+
@@ORM\Column("integer")
35+
@@ORM\GeneratedValue
3436
private $id;
3537

3638
/**
3739
* @ORM\Column(type="string", unique=true)
3840
* @Assert\Email(message="The email '{{ value }}' is not a valid email.")
3941
*/
40-
<<ORM\Column("string", ORM\Column::UNIQUE)>>
41-
<<Assert\Email(array("message" => "The email '{{ value }}' is not a valid email."))>>
42+
@@ORM\Column("string", ORM\Column::UNIQUE)
43+
@@Assert\Email(array("message" => "The email '{{ value }}' is not a valid email."))
4244
private $email;
4345

4446
/**
@@ -50,8 +52,8 @@ class User
5052
* maxMessage = "You cannot be taller than {{ limit }}cm to enter"
5153
* )
5254
*/
53-
<<Assert\Range(["min" => 120, "max" => 180, "minMessage" => "You must be at least {{ limit }}cm tall to enter"])>>
54-
<<ORM\Column(ORM\Column::T_INTEGER)>>
55+
@@Assert\Range(["min" => 120, "max" => 180, "minMessage" => "You must be at least {{ limit }}cm tall to enter"])
56+
@@ORM\Column(ORM\Column::T_INTEGER)
5557
protected $height;
5658

5759
/**
@@ -61,10 +63,10 @@ class User
6163
* inverseJoinColumns={@ORM\JoinColumn(name="phonenumber_id", referencedColumnName="id", unique=true)}
6264
* )
6365
*/
64-
<<ORM\ManyToMany(Phonenumber::class)>>
65-
<<ORM\JoinTable("users_phonenumbers")>>
66-
<<ORM\JoinColumn("user_id", "id")>>
67-
<<ORM\InverseJoinColumn("phonenumber_id", "id", ORM\JoinColumn::UNIQUE)>>
66+
@@ORM\ManyToMany(Phonenumber::class)
67+
@@ORM\JoinTable("users_phonenumbers")
68+
@@ORM\JoinColumn("user_id", "id")
69+
@@ORM\InverseJoinColumn("phonenumber_id", "id", ORM\JoinColumn::UNIQUE)
6870
private $phonenumbers;
6971
}
7072

Zend/tests/attributes/010_unsupported_const_expression.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Attribute arguments support only const expressions.
33
--FILE--
44
<?php
55

6-
<<A1(foo())>>
6+
@@A1(foo())
77
class C1 { }
88

99
?>

Zend/tests/attributes/011_inheritance.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Attributes comply with inheritance rules.
33
--FILE--
44
<?php
55

6-
<<A2>>
6+
@@A2
77
class C1
88
{
9-
<<A1>>
9+
@@A1
1010
public function foo() { }
1111
}
1212

@@ -17,7 +17,7 @@ class C2 extends C1
1717

1818
class C3 extends C1
1919
{
20-
<<A1>>
20+
@@A1
2121
public function bar() { }
2222
}
2323

@@ -37,7 +37,7 @@ echo "\n";
3737

3838
trait T1
3939
{
40-
<<A2>>
40+
@@A2
4141
public $a;
4242
}
4343

0 commit comments

Comments
 (0)