forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix attribute target validation on fake closures
Fixes phpGH-8982 Closes phpGH-9173
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--TEST-- | ||
GH-8982 (Attribute target validation fails when read via ReflectionFunction) | ||
--FILE-- | ||
<?php | ||
|
||
#[Attribute(Attribute::TARGET_FUNCTION)] | ||
class F | ||
{ | ||
} | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
class M | ||
{ | ||
} | ||
|
||
class C | ||
{ | ||
#[F] | ||
#[M] | ||
public function m() | ||
{ | ||
} | ||
} | ||
|
||
#[F] | ||
#[M] | ||
function f() {} | ||
|
||
function test(string $attributeClass, $value) { | ||
try { | ||
var_dump((new ReflectionFunction($value))->getAttributes($attributeClass)[0]->newInstance()); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
} | ||
|
||
$m = [new C(), 'm'](...); | ||
$f = f(...); | ||
|
||
test(F::class, $f); | ||
test(M::class, $f); | ||
test(F::class, $m); | ||
test(M::class, $m); | ||
|
||
?> | ||
--EXPECT-- | ||
object(F)#4 (0) { | ||
} | ||
Attribute "M" cannot target function (allowed targets: method) | ||
Attribute "F" cannot target method (allowed targets: function) | ||
object(M)#4 (0) { | ||
} |