Skip to content

Commit 9c82f85

Browse files
Test application of all internal attributes, fix 2 bugs
1 parent 598f9f8 commit 9c82f85

10 files changed

+526
-5
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\AllowDynamicProperties]: invalid targets don't error
3+
--FILE--
4+
<?php
5+
6+
#[DelayedTargetValidation]
7+
#[AllowDynamicProperties] // Does something here
8+
class DemoClass {
9+
#[DelayedTargetValidation]
10+
#[AllowDynamicProperties] // Does nothing here
11+
public $val;
12+
13+
#[DelayedTargetValidation]
14+
#[AllowDynamicProperties] // Does nothing here
15+
public const CLASS_CONST = 'FOO';
16+
17+
public function __construct(
18+
#[DelayedTargetValidation]
19+
#[AllowDynamicProperties] // Does nothing here
20+
$str
21+
) {
22+
echo "Got: $str\n";
23+
$this->val = $str;
24+
}
25+
26+
#[DelayedTargetValidation]
27+
#[AllowDynamicProperties] // Does nothing here
28+
public function printVal() {
29+
echo 'Value is: ' . $this->val . "\n";
30+
}
31+
32+
}
33+
34+
#[DelayedTargetValidation]
35+
#[AllowDynamicProperties] // Does nothing here
36+
function demoFn() {
37+
echo __FUNCTION__ . "\n";
38+
return 456;
39+
}
40+
41+
#[DelayedTargetValidation]
42+
#[AllowDynamicProperties] // Does nothing here
43+
const GLOBAL_CONST = 'BAR';
44+
45+
$d = new DemoClass('example');
46+
$d->printVal();
47+
var_dump($d->val);
48+
var_dump(DemoClass::CLASS_CONST);
49+
demoFn();
50+
var_dump(GLOBAL_CONST);
51+
52+
$d->missingProp = 'foo';
53+
var_dump($d);
54+
?>
55+
--EXPECTF--
56+
Got: example
57+
Value is: example
58+
string(7) "example"
59+
string(3) "FOO"
60+
demoFn
61+
string(3) "BAR"
62+
object(DemoClass)#%d (2) {
63+
["val"]=>
64+
string(7) "example"
65+
["missingProp"]=>
66+
string(3) "foo"
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\Attribute]: invalid targets don't error
3+
--FILE--
4+
<?php
5+
6+
class NonAttribute {}
7+
8+
#[DelayedTargetValidation]
9+
#[Attribute] // Does something here
10+
class DemoClass {
11+
#[DelayedTargetValidation]
12+
#[Attribute] // Does nothing here
13+
public $val;
14+
15+
#[DelayedTargetValidation]
16+
#[Attribute] // Does nothing here
17+
public const CLASS_CONST = 'FOO';
18+
19+
public function __construct(
20+
#[DelayedTargetValidation]
21+
#[Attribute] // Does nothing here
22+
$str
23+
) {
24+
echo "Got: $str\n";
25+
$this->val = $str;
26+
}
27+
28+
#[DelayedTargetValidation]
29+
#[Attribute] // Does nothing here
30+
public function printVal() {
31+
echo 'Value is: ' . $this->val . "\n";
32+
}
33+
34+
}
35+
36+
#[DelayedTargetValidation]
37+
#[Attribute] // Does nothing here
38+
function demoFn() {
39+
echo __FUNCTION__ . "\n";
40+
return 456;
41+
}
42+
43+
#[DelayedTargetValidation]
44+
#[Attribute] // Does nothing here
45+
const GLOBAL_CONST = 'BAR';
46+
47+
$d = new DemoClass('example');
48+
$d->printVal();
49+
var_dump($d->val);
50+
var_dump(DemoClass::CLASS_CONST);
51+
demoFn();
52+
var_dump(GLOBAL_CONST);
53+
54+
#[DemoClass('BAZ')]
55+
#[NonAttribute]
56+
class WithDemoAttribs {}
57+
58+
$ref = new ReflectionClass(WithDemoAttribs::class);
59+
$attribs = $ref->getAttributes();
60+
var_dump($attribs[0]->newInstance());
61+
var_dump($attribs[1]->newInstance());
62+
63+
?>
64+
--EXPECTF--
65+
Got: example
66+
Value is: example
67+
string(7) "example"
68+
string(3) "FOO"
69+
demoFn
70+
string(3) "BAR"
71+
Got: BAZ
72+
object(DemoClass)#5 (1) {
73+
["val"]=>
74+
string(3) "BAZ"
75+
}
76+
77+
Fatal error: Uncaught Error: Attempting to use non-attribute class "NonAttribute" as attribute in %s:%d
78+
Stack trace:
79+
#0 %s(%d): ReflectionAttribute->newInstance()
80+
#1 {main}
81+
thrown in %s on line %d
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\Deprecated]: valid targets are deprecated
3+
--FILE--
4+
<?php
5+
6+
#[DelayedTargetValidation]
7+
#[Deprecated] // Does nothing here
8+
class DemoClass {
9+
#[DelayedTargetValidation]
10+
#[Deprecated] // Does nothing here
11+
public $val;
12+
13+
#[DelayedTargetValidation]
14+
#[Deprecated] // Does something here
15+
public const CLASS_CONST = 'FOO';
16+
17+
public function __construct(
18+
#[DelayedTargetValidation]
19+
#[Deprecated] // Does nothing here
20+
$str
21+
) {
22+
echo "Got: $str\n";
23+
$this->val = $str;
24+
}
25+
26+
#[DelayedTargetValidation]
27+
#[Deprecated] // Does something here
28+
public function printVal() {
29+
echo 'Value is: ' . $this->val . "\n";
30+
return 123;
31+
}
32+
}
33+
34+
#[DelayedTargetValidation]
35+
#[Deprecated] // Does something here
36+
function demoFn() {
37+
echo __FUNCTION__ . "\n";
38+
return 456;
39+
}
40+
41+
#[DelayedTargetValidation]
42+
#[Deprecated] // Does something here
43+
const GLOBAL_CONST = 'BAR';
44+
45+
$d = new DemoClass('example');
46+
$d->printVal();
47+
var_dump($d->val);
48+
var_dump(DemoClass::CLASS_CONST);
49+
demoFn();
50+
var_dump(GLOBAL_CONST);
51+
?>
52+
--EXPECTF--
53+
Got: example
54+
55+
Deprecated: Method DemoClass::printVal() is deprecated in %s on line %d
56+
Value is: example
57+
string(7) "example"
58+
59+
Deprecated: Constant DemoClass::CLASS_CONST is deprecated in %s on line %d
60+
string(3) "FOO"
61+
62+
Deprecated: Function demoFn() is deprecated in %s on line %d
63+
demoFn
64+
65+
Deprecated: Constant GLOBAL_CONST is deprecated in %s on line %d
66+
string(3) "BAR"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\NoDiscard]: valid targets complain about discarding
3+
--FILE--
4+
<?php
5+
6+
#[DelayedTargetValidation]
7+
#[NoDiscard] // Does nothing here
8+
class DemoClass {
9+
#[DelayedTargetValidation]
10+
#[NoDiscard] // Does nothing here
11+
public $val;
12+
13+
#[DelayedTargetValidation]
14+
#[NoDiscard] // Does nothing here
15+
public const CLASS_CONST = 'FOO';
16+
17+
public function __construct(
18+
#[DelayedTargetValidation]
19+
#[NoDiscard] // Does nothing here
20+
$str
21+
) {
22+
echo "Got: $str\n";
23+
$this->val = $str;
24+
}
25+
26+
#[DelayedTargetValidation]
27+
#[NoDiscard] // Does something here
28+
public function printVal() {
29+
echo 'Value is: ' . $this->val . "\n";
30+
return 123;
31+
}
32+
}
33+
34+
#[DelayedTargetValidation]
35+
#[NoDiscard] // Does something here
36+
function demoFn() {
37+
echo __FUNCTION__ . "\n";
38+
return 456;
39+
}
40+
41+
#[DelayedTargetValidation]
42+
#[NoDiscard] // Does nothing here
43+
const GLOBAL_CONST = 'BAR';
44+
45+
$d = new DemoClass('example');
46+
$d->printVal();
47+
$v = $d->printVal();
48+
var_dump($d->val);
49+
var_dump(DemoClass::CLASS_CONST);
50+
demoFn();
51+
$v = demoFn();
52+
var_dump(GLOBAL_CONST);
53+
?>
54+
--EXPECTF--
55+
Got: example
56+
57+
Warning: The return value of method DemoClass::printVal() should either be used or intentionally ignored by casting it as (void) in %s on line %d
58+
Value is: example
59+
Value is: example
60+
string(7) "example"
61+
string(3) "FOO"
62+
63+
Warning: The return value of function demoFn() should either be used or intentionally ignored by casting it as (void) in %s on line %d
64+
demoFn
65+
demoFn
66+
string(3) "BAR"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\Override]: non-overrides still error
3+
--FILE--
4+
<?php
5+
6+
class DemoClass {
7+
8+
#[DelayedTargetValidation]
9+
#[Override] // Does something here
10+
public function printVal() {
11+
echo 'Value is: ' . $this->val . "\n";
12+
return 123;
13+
}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: DemoClass::printVal() has #[\Override] attribute, but no matching parent method exists in %s on line %d
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--TEST--
2+
#[\DelayedTargetValidation] with #[\Override]: invalid targets or actual overrides don't do anything
3+
--FILE--
4+
<?php
5+
6+
class Base {
7+
public function printVal() {
8+
echo __METHOD__ . "\n";
9+
}
10+
}
11+
12+
#[DelayedTargetValidation]
13+
#[Override] // Does nothing here
14+
class DemoClass extends Base {
15+
#[DelayedTargetValidation]
16+
#[Override] // Does nothing here
17+
public $val;
18+
19+
#[DelayedTargetValidation]
20+
#[Override] // Does nothing here
21+
public const CLASS_CONST = 'FOO';
22+
23+
public function __construct(
24+
#[DelayedTargetValidation]
25+
#[Override] // Does nothing here
26+
$str
27+
) {
28+
echo "Got: $str\n";
29+
$this->val = $str;
30+
}
31+
32+
#[DelayedTargetValidation]
33+
#[Override] // Does something here
34+
public function printVal() {
35+
echo 'Value is: ' . $this->val . "\n";
36+
return 123;
37+
}
38+
}
39+
40+
#[DelayedTargetValidation]
41+
#[Override] // Does nothing here
42+
function demoFn() {
43+
echo __FUNCTION__ . "\n";
44+
return 456;
45+
}
46+
47+
#[DelayedTargetValidation]
48+
#[Override] // Does nothing here
49+
const GLOBAL_CONST = 'BAR';
50+
51+
$d = new DemoClass('example');
52+
$d->printVal();
53+
var_dump($d->val);
54+
var_dump(DemoClass::CLASS_CONST);
55+
demoFn();
56+
var_dump(GLOBAL_CONST);
57+
?>
58+
--EXPECT--
59+
Got: example
60+
Value is: example
61+
string(7) "example"
62+
string(3) "FOO"
63+
demoFn
64+
string(3) "BAR"

0 commit comments

Comments
 (0)