Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34a6e86

Browse files
[RFC] Allow #[\Deprecated] on traits (#19045)
https://wiki.php.net/rfc/deprecated_traits
1 parent 8cd085a commit 34a6e86

20 files changed

+448
-11
lines changed

‎Zend/tests/attributes/constants/constant_listed_as_target-internal.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ Constants listed in valid targets when used wrong (internal attribute)
33
--FILE--
44
<?php
55

6-
#[Deprecated]
7-
class Example {}
6+
function demo(
7+
#[Deprecated] $v
8+
) {}
89

910
?>
1011
--EXPECTF--
11-
Fatal error: Attribute "Deprecated" cannot target class (allowed targets: function, method, class constant, constant) in %s on line %d
12+
Fatal error: Attribute "Deprecated" cannot target parameter (allowed targets: class, function, method, class constant, constant) in %s on line %d
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\Deprecated]: validator errors delayed
3+
--FILE--
4+
<?php
5+
6+
#[DelayedTargetValidation]
7+
#[Deprecated]
8+
interface DemoInterface {}
9+
10+
#[DelayedTargetValidation]
11+
#[Deprecated]
12+
class DemoClass {}
13+
14+
#[DelayedTargetValidation]
15+
#[Deprecated]
16+
enum DemoEnum {}
17+
18+
$cases = [
19+
new ReflectionClass('DemoInterface'),
20+
new ReflectionClass('DemoClass'),
21+
new ReflectionClass('DemoEnum'),
22+
];
23+
foreach ($cases as $r) {
24+
echo str_repeat("*", 20) . "\n";
25+
echo $r . "\n";
26+
$attributes = $r->getAttributes();
27+
var_dump($attributes);
28+
try {
29+
$attributes[1]->newInstance();
30+
} catch (Error $e) {
31+
echo get_class($e) . ": " . $e->getMessage() . "\n";
32+
}
33+
}
34+
35+
?>
36+
--EXPECTF--
37+
********************
38+
Interface [ <user> interface DemoInterface ] {
39+
@@ %s %d-%d
40+
41+
- Constants [0] {
42+
}
43+
44+
- Static properties [0] {
45+
}
46+
47+
- Static methods [0] {
48+
}
49+
50+
- Properties [0] {
51+
}
52+
53+
- Methods [0] {
54+
}
55+
}
56+
57+
array(2) {
58+
[0]=>
59+
object(ReflectionAttribute)#%d (1) {
60+
["name"]=>
61+
string(23) "DelayedTargetValidation"
62+
}
63+
[1]=>
64+
object(ReflectionAttribute)#%d (1) {
65+
["name"]=>
66+
string(10) "Deprecated"
67+
}
68+
}
69+
Error: Cannot apply #[\Deprecated] to interface DemoInterface
70+
********************
71+
Class [ <user> class DemoClass ] {
72+
@@ %s %d-%d
73+
74+
- Constants [0] {
75+
}
76+
77+
- Static properties [0] {
78+
}
79+
80+
- Static methods [0] {
81+
}
82+
83+
- Properties [0] {
84+
}
85+
86+
- Methods [0] {
87+
}
88+
}
89+
90+
array(2) {
91+
[0]=>
92+
object(ReflectionAttribute)#%d (1) {
93+
["name"]=>
94+
string(23) "DelayedTargetValidation"
95+
}
96+
[1]=>
97+
object(ReflectionAttribute)#%d (1) {
98+
["name"]=>
99+
string(10) "Deprecated"
100+
}
101+
}
102+
Error: Cannot apply #[\Deprecated] to class DemoClass
103+
********************
104+
Enum [ <user> enum DemoEnum implements UnitEnum ] {
105+
@@ %s %d-%d
106+
107+
- Constants [0] {
108+
}
109+
110+
- Static properties [0] {
111+
}
112+
113+
- Static methods [1] {
114+
Method [ <internal, prototype UnitEnum> static public method cases ] {
115+
116+
- Parameters [0] {
117+
}
118+
- Return [ array ]
119+
}
120+
}
121+
122+
- Properties [1] {
123+
Property [ public protected(set) readonly string $name ]
124+
}
125+
126+
- Methods [0] {
127+
}
128+
}
129+
130+
array(2) {
131+
[0]=>
132+
object(ReflectionAttribute)#%d (1) {
133+
["name"]=>
134+
string(23) "DelayedTargetValidation"
135+
}
136+
[1]=>
137+
object(ReflectionAttribute)#%d (1) {
138+
["name"]=>
139+
string(10) "Deprecated"
140+
}
141+
}
142+
Error: Cannot apply #[\Deprecated] to enum DemoEnum

‎Zend/tests/attributes/delayed_target_validation/with_Deprecated.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ class DemoClass {
4040
}
4141
}
4242

43+
#[DelayedTargetValidation]
44+
#[Deprecated] // Does something here
45+
trait DeprecatedTrait {}
46+
47+
class WithDeprecatedTrait {
48+
use DeprecatedTrait;
49+
}
50+
4351
#[DelayedTargetValidation]
4452
#[Deprecated] // Does something here
4553
function demoFn() {
@@ -61,6 +69,7 @@ demoFn();
6169
var_dump(GLOBAL_CONST);
6270
?>
6371
--EXPECTF--
72+
Deprecated: Trait DeprecatedTrait used by WithDeprecatedTrait is deprecated in %s on line %d
6473
Got: example
6574

6675
Deprecated: Method DemoClass::printVal() is deprecated in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
#[\Deprecated]: Using on a class
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
class Demo {}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Cannot apply #[\Deprecated] to class Demo in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
#[\Deprecated]: Using on an enum
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
enum Demo {}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Cannot apply #[\Deprecated] to enum Demo in %s on line %d
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
#[\Deprecated]: Using on an interface
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
interface Demo {}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Cannot apply #[\Deprecated] to interface Demo in %s on line %d
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
#[\Deprecated]: Basic trait deprecation
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
trait DemoTrait1 {}
8+
9+
#[\Deprecated("will be removed in 3.0", since: "2.7")]
10+
trait DemoTrait2 {}
11+
12+
#[\Deprecated(message: "going away")]
13+
trait DemoTrait3 {}
14+
15+
#[\Deprecated(since: "3.5")]
16+
trait DemoTrait4 {}
17+
18+
class DemoClass {
19+
use DemoTrait1;
20+
use DemoTrait2;
21+
use DemoTrait3;
22+
use DemoTrait4;
23+
}
24+
25+
?>
26+
--EXPECTF--
27+
Deprecated: Trait DemoTrait1 used by DemoClass is deprecated, please do not use in %s on line %d
28+
29+
Deprecated: Trait DemoTrait2 used by DemoClass is deprecated since 2.7, will be removed in 3.0 in %s on line %d
30+
31+
Deprecated: Trait DemoTrait3 used by DemoClass is deprecated, going away in %s on line %d
32+
33+
Deprecated: Trait DemoTrait4 used by DemoClass is deprecated since 3.5 in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
#[\Deprecated]: Deprecated traits only apply to direct use, not inheritance
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
trait DemoTrait {}
8+
9+
class DemoClass {
10+
use DemoTrait;
11+
}
12+
13+
class ChildClass extends DemoClass {
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Deprecated: Trait DemoTrait used by DemoClass is deprecated in %s on line %d
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
#[\Deprecated]: `insteadof` rendering a trait unused still triggers deprecation messages
3+
--FILE--
4+
<?php
5+
6+
#[Deprecated]
7+
trait DemoTraitA {
8+
public function lowerCase(): string {
9+
return 'a';
10+
}
11+
public function upperCase(): string {
12+
return 'A';
13+
}
14+
}
15+
16+
#[Deprecated]
17+
trait DemoTraitB {
18+
public function lowerCase(): string {
19+
return 'b';
20+
}
21+
public function upperCase(): string {
22+
return 'B';
23+
}
24+
}
25+
26+
class DemoClass {
27+
use DemoTraita, DemoTraitB {
28+
DemoTraitA::lowerCase insteadof DemoTraitB;
29+
DemoTraitA::upperCase insteadof DemoTraitB;
30+
}
31+
}
32+
33+
$d = new DemoClass();
34+
var_dump($d->lowerCase());
35+
var_dump($d->upperCase());
36+
37+
?>
38+
--EXPECTF--
39+
Deprecated: Trait DemoTraitA used by DemoClass is deprecated in %s on line %d
40+
41+
Deprecated: Trait DemoTraitB used by DemoClass is deprecated in %s on line %d
42+
string(1) "a"
43+
string(1) "A"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
#[\Deprecated]: Using multiple traits
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated]
7+
trait DemoTraitA {}
8+
9+
#[\Deprecated]
10+
trait DemoTraitB {}
11+
12+
trait DemoTraitC {}
13+
14+
class DemoClass {
15+
use DemoTraitA, DemoTraitB, DemoTraitC;
16+
}
17+
18+
?>
19+
--EXPECTF--
20+
Deprecated: Trait DemoTraitA used by DemoClass is deprecated in %s on line %d
21+
22+
Deprecated: Trait DemoTraitB used by DemoClass is deprecated in %s on line %d

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /