gh8982.phpt 834 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. --TEST--
  2. GH-8982 (Attribute target validation fails when read via ReflectionFunction)
  3. --FILE--
  4. <?php
  5. #[Attribute(Attribute::TARGET_FUNCTION)]
  6. class F
  7. {
  8. }
  9. #[Attribute(Attribute::TARGET_METHOD)]
  10. class M
  11. {
  12. }
  13. class C
  14. {
  15. #[F]
  16. #[M]
  17. public function m()
  18. {
  19. }
  20. }
  21. #[F]
  22. #[M]
  23. function f() {}
  24. function test(string $attributeClass, $value) {
  25. try {
  26. var_dump((new ReflectionFunction($value))->getAttributes($attributeClass)[0]->newInstance());
  27. } catch (Error $e) {
  28. echo $e->getMessage(), "\n";
  29. }
  30. }
  31. $m = [new C(), 'm'](...);
  32. $f = f(...);
  33. test(F::class, $f);
  34. test(M::class, $f);
  35. test(F::class, $m);
  36. test(M::class, $m);
  37. ?>
  38. --EXPECT--
  39. object(F)#4 (0) {
  40. }
  41. Attribute "M" cannot target function (allowed targets: method)
  42. Attribute "F" cannot target method (allowed targets: function)
  43. object(M)#4 (0) {
  44. }