Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ Constants listed in valid targets when used wrong (internal attribute)
--FILE--
<?php

#[Deprecated]
class Example {}
function demo(
#[Deprecated] $v
) {}

?>
--EXPECTF--
Fatal error: Attribute "Deprecated" cannot target class (allowed targets: function, method, class constant, constant) in %s on line %d
Fatal error: Attribute "Deprecated" cannot target parameter (allowed targets: class, function, method, class constant, constant) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
--TEST--
#[\DelayedTargetValidation] with #[\Deprecated]: validator errors delayed
--FILE--
<?php

#[DelayedTargetValidation]
#[Deprecated]
interface DemoInterface {}

#[DelayedTargetValidation]
#[Deprecated]
class DemoClass {}

#[DelayedTargetValidation]
#[Deprecated]
enum DemoEnum {}

$cases = [
new ReflectionClass('DemoInterface'),
new ReflectionClass('DemoClass'),
new ReflectionClass('DemoEnum'),
];
foreach ($cases as $r) {
echo str_repeat("*", 20) . "\n";
echo $r . "\n";
$attributes = $r->getAttributes();
var_dump($attributes);
try {
$attributes[1]->newInstance();
} catch (Error $e) {
echo get_class($e) . ": " . $e->getMessage() . "\n";
}
}

?>
--EXPECTF--
********************
Interface [ <user> interface DemoInterface ] {
@@ %s %d-%d

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [0] {
}

- Properties [0] {
}

- Methods [0] {
}
}

array(2) {
[0]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(23) "DelayedTargetValidation"
}
[1]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(10) "Deprecated"
}
}
Error: Cannot apply #[\Deprecated] to interface DemoInterface
********************
Class [ <user> class DemoClass ] {
@@ %s %d-%d

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [0] {
}

- Properties [0] {
}

- Methods [0] {
}
}

array(2) {
[0]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(23) "DelayedTargetValidation"
}
[1]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(10) "Deprecated"
}
}
Error: Cannot apply #[\Deprecated] to class DemoClass
********************
Enum [ <user> enum DemoEnum implements UnitEnum ] {
@@ %s %d-%d

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [1] {
Method [ <internal, prototype UnitEnum> static public method cases ] {

- Parameters [0] {
}
- Return [ array ]
}
}

- Properties [1] {
Property [ public protected(set) readonly string $name ]
}

- Methods [0] {
}
}

array(2) {
[0]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(23) "DelayedTargetValidation"
}
[1]=>
object(ReflectionAttribute)#%d (1) {
["name"]=>
string(10) "Deprecated"
}
}
Error: Cannot apply #[\Deprecated] to enum DemoEnum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class DemoClass {
}
}

#[DelayedTargetValidation]
#[Deprecated] // Does something here
trait DeprecatedTrait {}

class WithDeprecatedTrait {
use DeprecatedTrait;
}

#[DelayedTargetValidation]
#[Deprecated] // Does something here
function demoFn() {
Expand All @@ -61,6 +69,7 @@ demoFn();
var_dump(GLOBAL_CONST);
?>
--EXPECTF--
Deprecated: Trait DeprecatedTrait used by WithDeprecatedTrait is deprecated in %s on line %d
Got: example

Deprecated: Method DemoClass::printVal() is deprecated in %s on line %d
Expand Down
11 changes: 11 additions & 0 deletions Zend/tests/attributes/deprecated/error_on_class.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
#[\Deprecated]: Using on a class
--FILE--
<?php

#[\Deprecated]
class Demo {}

?>
--EXPECTF--
Fatal error: Cannot apply #[\Deprecated] to class Demo in %s on line %d
11 changes: 11 additions & 0 deletions Zend/tests/attributes/deprecated/error_on_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
#[\Deprecated]: Using on an enum
--FILE--
<?php

#[\Deprecated]
enum Demo {}

?>
--EXPECTF--
Fatal error: Cannot apply #[\Deprecated] to enum Demo in %s on line %d
11 changes: 11 additions & 0 deletions Zend/tests/attributes/deprecated/error_on_interface.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
#[\Deprecated]: Using on an interface
--FILE--
<?php

#[\Deprecated]
interface Demo {}

?>
--EXPECTF--
Fatal error: Cannot apply #[\Deprecated] to interface Demo in %s on line %d
33 changes: 33 additions & 0 deletions Zend/tests/attributes/deprecated/traits/basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
#[\Deprecated]: Basic trait deprecation
--FILE--
<?php

#[\Deprecated("please do not use")]
trait DemoTrait1 {}

#[\Deprecated("will be removed in 3.0", since: "2.7")]
trait DemoTrait2 {}

#[\Deprecated(message: "going away")]
trait DemoTrait3 {}

#[\Deprecated(since: "3.5")]
trait DemoTrait4 {}

class DemoClass {
use DemoTrait1;
use DemoTrait2;
use DemoTrait3;
use DemoTrait4;
}

?>
--EXPECTF--
Deprecated: Trait DemoTrait1 used by DemoClass is deprecated, please do not use in %s on line %d

Deprecated: Trait DemoTrait2 used by DemoClass is deprecated since 2.7, will be removed in 3.0 in %s on line %d

Deprecated: Trait DemoTrait3 used by DemoClass is deprecated, going away in %s on line %d

Deprecated: Trait DemoTrait4 used by DemoClass is deprecated since 3.5 in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/attributes/deprecated/traits/inheritance.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
#[\Deprecated]: Deprecated traits only apply to direct use, not inheritance
--FILE--
<?php

#[\Deprecated]
trait DemoTrait {}

class DemoClass {
use DemoTrait;
}

class ChildClass extends DemoClass {
}

?>
--EXPECTF--
Deprecated: Trait DemoTrait used by DemoClass is deprecated in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
#[\Deprecated]: `insteadof` rendering a trait unused still triggers deprecation messages
--FILE--
<?php

#[Deprecated]
trait DemoTraitA {
public function lowerCase(): string {
return 'a';
}
public function upperCase(): string {
return 'A';
}
}

#[Deprecated]
trait DemoTraitB {
public function lowerCase(): string {
return 'b';
}
public function upperCase(): string {
return 'B';
}
}

class DemoClass {
use DemoTraita, DemoTraitB {
DemoTraitA::lowerCase insteadof DemoTraitB;
DemoTraitA::upperCase insteadof DemoTraitB;
}
}

$d = new DemoClass();
var_dump($d->lowerCase());
var_dump($d->upperCase());

?>
--EXPECTF--
Deprecated: Trait DemoTraitA used by DemoClass is deprecated in %s on line %d

Deprecated: Trait DemoTraitB used by DemoClass is deprecated in %s on line %d
string(1) "a"
string(1) "A"
22 changes: 22 additions & 0 deletions Zend/tests/attributes/deprecated/traits/multiple_traits.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
#[\Deprecated]: Using multiple traits
--FILE--
<?php

#[\Deprecated]
trait DemoTraitA {}

#[\Deprecated]
trait DemoTraitB {}

trait DemoTraitC {}

class DemoClass {
use DemoTraitA, DemoTraitB, DemoTraitC;
}

?>
--EXPECTF--
Deprecated: Trait DemoTraitA used by DemoClass is deprecated in %s on line %d

Deprecated: Trait DemoTraitB used by DemoClass is deprecated in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
#[\Deprecated]: Deprecation converted to ErrorException does not break
--FILE--
<?php

function my_error_handler(int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}

set_error_handler('my_error_handler');

#[\Deprecated]
trait DemoTrait {}

class DemoClass {
use DemoTrait;
}

?>
--EXPECTF--
Fatal error: Uncaught ErrorException: Trait DemoTrait used by DemoClass is deprecated in %s:%d
Stack trace:
#0 %s: my_error_handler(16384, 'Trait DemoTrait...', '%s', %d)
#1 {main}
thrown in %s on line %d
19 changes: 19 additions & 0 deletions Zend/tests/attributes/deprecated/traits/trait_using_trait.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
#[\Deprecated]: Trait using a deprecated trait
--FILE--
<?php

#[\Deprecated]
trait DemoTraitA {}

trait DemoTraitB {
use DemoTraitA;
}

class DemoClass {
use DemoTraitB;
}

?>
--EXPECTF--
Deprecated: Trait DemoTraitA used by DemoTraitB is deprecated in %s on line %d
Loading