Skip to content

Commit 81d287f

Browse files
[RFC] Allow #[\Deprecated] on interfaces
1 parent fe52e5b commit 81d287f

14 files changed

Lines changed: 246 additions & 39 deletions

Zend/tests/attributes/delayed_target_validation/validator_Deprecated.phpt

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
--FILE--
44
<?php
55

6-
#[DelayedTargetValidation]
7-
#[Deprecated]
8-
interface DemoInterface {}
9-
106
#[DelayedTargetValidation]
117
#[Deprecated]
128
class DemoClass {}
@@ -16,7 +12,6 @@ class DemoClass {}
1612
enum DemoEnum {}
1713

1814
$cases = [
19-
new ReflectionClass('DemoInterface'),
2015
new ReflectionClass('DemoClass'),
2116
new ReflectionClass('DemoEnum'),
2217
];
@@ -35,39 +30,6 @@ foreach ($cases as $r) {
3530
?>
3631
--EXPECTF--
3732
********************
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-
********************
7133
Class [ <user> class DemoClass ] {
7234
@@ %s %d-%d
7335

Zend/tests/attributes/delayed_target_validation/with_Deprecated.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ class WithDeprecatedTrait {
4848
use DeprecatedTrait;
4949
}
5050

51+
#[DelayedTargetValidation]
52+
#[Deprecated] // Does something here
53+
interface DeprecatedInterface {}
54+
55+
class ImplementsDeprecatedInterface implements DeprecatedInterface {}
56+
5157
#[DelayedTargetValidation]
5258
#[Deprecated] // Does something here
5359
function demoFn() {
@@ -70,6 +76,8 @@ var_dump(GLOBAL_CONST);
7076
?>
7177
--EXPECTF--
7278
Deprecated: Trait DeprecatedTrait used by WithDeprecatedTrait is deprecated in %s on line %d
79+
80+
Deprecated: Interface DeprecatedInterface implemented by ImplementsDeprecatedInterface is deprecated in %s on line %d
7381
Got: example
7482

7583
Deprecated: Method DemoClass::printVal() is deprecated in %s on line %d
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
#[\Deprecated]: Basic interface deprecation
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface DemoInterface1 {}
8+
9+
#[\Deprecated("will be removed in 3.0", since: "2.7")]
10+
interface DemoInterface2 {}
11+
12+
#[\Deprecated(message: "going away")]
13+
interface DemoInterface3 {}
14+
15+
#[\Deprecated(since: "3.5")]
16+
interface DemoInterface4 {}
17+
18+
class DemoClass implements
19+
DemoInterface1,
20+
DemoInterface2,
21+
DemoInterface3,
22+
DemoInterface4
23+
{
24+
}
25+
26+
?>
27+
--EXPECTF--
28+
Deprecated: Interface DemoInterface1 implemented by DemoClass is deprecated, please do not use in %s on line %d
29+
30+
Deprecated: Interface DemoInterface2 implemented by DemoClass is deprecated since 2.7, will be removed in 3.0 in %s on line %d
31+
32+
Deprecated: Interface DemoInterface3 implemented by DemoClass is deprecated, going away in %s on line %d
33+
34+
Deprecated: Interface DemoInterface4 implemented by DemoClass is deprecated since 3.5 in %s on line %d
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
#[\Deprecated]: Interfaces extending deprecated interfaces trigger warnings
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface DemoInterface1 {}
8+
9+
#[\Deprecated("will be removed in 3.0", since: "2.7")]
10+
interface DemoInterface2 {}
11+
12+
#[\Deprecated(message: "going away")]
13+
interface DemoInterface3 {}
14+
15+
#[\Deprecated(since: "3.5")]
16+
interface DemoInterface4 {}
17+
18+
interface ExtendsDemo1 extends DemoInterface1 {}
19+
interface ExtendsDemo2 extends DemoInterface2 {}
20+
interface ExtendsDemo3 extends DemoInterface3 {}
21+
interface ExtendsDemo4 extends DemoInterface4 {}
22+
23+
?>
24+
--EXPECTF--
25+
Deprecated: Interface DemoInterface1 extended by ExtendsDemo1 is deprecated, please do not use in %s on line %d
26+
27+
Deprecated: Interface DemoInterface2 extended by ExtendsDemo2 is deprecated since 2.7, will be removed in 3.0 in %s on line %d
28+
29+
Deprecated: Interface DemoInterface3 extended by ExtendsDemo3 is deprecated, going away in %s on line %d
30+
31+
Deprecated: Interface DemoInterface4 extended by ExtendsDemo4 is deprecated since 3.5 in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
#[\Deprecated]: Implementing a child of a deprecated interface
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface BaseInterface {}
8+
9+
interface ChildInterface extends BaseInterface {}
10+
11+
class DemoClass implements ChildInterface {}
12+
13+
?>
14+
--EXPECTF--
15+
Deprecated: Interface BaseInterface extended by ChildInterface is deprecated, please do not use in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
#[\Deprecated]: Implementing a deprecated interface and a child of it
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface BaseInterface {}
8+
9+
interface ChildInterface extends BaseInterface {}
10+
11+
class DemoClass implements BaseInterface, ChildInterface {}
12+
13+
?>
14+
--EXPECTF--
15+
Deprecated: Interface BaseInterface extended by ChildInterface is deprecated, please do not use in %s on line %d
16+
17+
Deprecated: Interface BaseInterface implemented by DemoClass is deprecated, please do not use in %s on line %d
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
#[\Deprecated]: Parent class implements deprecated interface, child class inherits
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface DemoInterface {}
8+
9+
class Base implements DemoInterface {}
10+
11+
class Child extends Base {}
12+
13+
?>
14+
--EXPECTF--
15+
Deprecated: Interface DemoInterface implemented by Base is deprecated, please do not use in %s on line %d
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
#[\Deprecated]: Parent class implements deprecated interface, child class inherits and also implements
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface DemoInterface {}
8+
9+
class Base implements DemoInterface {}
10+
11+
class Child extends Base implements DemoInterface {}
12+
13+
?>
14+
--EXPECTF--
15+
Deprecated: Interface DemoInterface implemented by Base is deprecated, please do not use in %s on line %d
16+
17+
Deprecated: Interface DemoInterface implemented by Child is deprecated, please do not use in %s on line %d
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
#[\Deprecated]: Deprecation converted to ErrorException does not break
3+
--FILE--
4+
<?php
5+
6+
function my_error_handler(int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
7+
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
8+
}
9+
10+
set_error_handler('my_error_handler');
11+
12+
#[\Deprecated]
13+
interface DemoInterface {}
14+
15+
class DemoClass implements DemoInterface {}
16+
17+
?>
18+
--EXPECTF--
19+
Fatal error: Uncaught ErrorException: Interface DemoInterface implemented by DemoClass is deprecated in %s:%d
20+
Stack trace:
21+
#0 %s(%d): my_error_handler(16384, 'Interface DemoI...', '/usr/src/php/Ze...', 12)
22+
#1 {main}
23+
thrown in %s on line %d
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
#[\Deprecated]: Instanceof, type checking, etc. for deprecated interfaces are okay
3+
--FILE--
4+
<?php
5+
6+
#[\Deprecated("please do not use")]
7+
interface DemoInterface {}
8+
9+
class Identity {
10+
public static function identity(
11+
DemoInterface $in
12+
): DemoInterface {
13+
return $in;
14+
}
15+
}
16+
17+
class ImplementsDemo implements DemoInterface {};
18+
19+
$instance = new ImplementsDemo();
20+
21+
var_dump(is_a(ImplementsDemo::class, DemoInterface::class, true));
22+
var_dump(is_a($instance, DemoInterface::class));
23+
var_dump(is_subclass_of(Implementsdemo::class, DemoInterface::class, true));
24+
var_dump(is_subclass_of($instance, DemoInterface::class));
25+
var_dump($instance === Identity::identity($instance));
26+
var_dump($instance instanceof DemoInterface);
27+
?>
28+
--EXPECTF--
29+
Deprecated: Interface DemoInterface implemented by ImplementsDemo is deprecated, please do not use in %s on line %d
30+
bool(true)
31+
bool(true)
32+
bool(true)
33+
bool(true)
34+
bool(true)
35+
bool(true)

0 commit comments

Comments
 (0)