php-src/Zend/tests/attributes/005_objects.phpt
Nikita Popov 5686c16db4 Honor strict_types=1 for attributes, improve backtraces
Make ReflectionAttribute::newInstance() respect the strict_types=1
declaration at the attribute use-site. More generally, pretend that
we are calling the attribute constructor from the place where the
attribute is used, which also means that the attribute location will
show up properly in backtraces and inside "called in" error information.

This requires us to store the attributes strict_types scope (as flags),
as well as the attribute line number. The attribute filename can be
recovered from the symbol it is used on. We might want to expose the
attribute line number via reflection as well.

See also https://externals.io/message/111915.

Closes GH-6201.
2020-09-27 10:42:58 +02:00

120 lines
2.4 KiB
PHP

--TEST--
Attributes can be converted into objects.
--FILE--
<?php
#[Attribute(Attribute::TARGET_FUNCTION)]
class A1
{
public string $name;
public int $ttl;
public function __construct(string $name, int $ttl = 50)
{
$this->name = $name;
$this->ttl = $ttl;
}
}
$ref = new \ReflectionFunction(#[A1('test')] function () { });
foreach ($ref->getAttributes() as $attr) {
$obj = $attr->newInstance();
var_dump(get_class($obj), $obj->name, $obj->ttl);
}
echo "\n";
$ref = new \ReflectionFunction(#[A1] function () { });
try {
$ref->getAttributes()[0]->newInstance();
} catch (\ArgumentCountError $e) {
var_dump('ERROR 1', $e->getMessage());
}
echo "\n";
$ref = new \ReflectionFunction(#[A1([])] function () { });
try {
$ref->getAttributes()[0]->newInstance();
} catch (\TypeError $e) {
var_dump('ERROR 2', $e->getMessage());
}
echo "\n";
$ref = new \ReflectionFunction(#[A2] function () { });
try {
$ref->getAttributes()[0]->newInstance();
} catch (\Error $e) {
var_dump('ERROR 3', $e->getMessage());
}
echo "\n";
#[Attribute]
class A3
{
private function __construct() { }
}
$ref = new \ReflectionFunction(#[A3] function () { });
try {
$ref->getAttributes()[0]->newInstance();
} catch (\Error $e) {
var_dump('ERROR 4', $e->getMessage());
}
echo "\n";
#[Attribute]
class A4 { }
$ref = new \ReflectionFunction(#[A4(1)] function () { });
try {
$ref->getAttributes()[0]->newInstance();
} catch (\Error $e) {
var_dump('ERROR 5', $e->getMessage());
}
echo "\n";
class A5 { }
$ref = new \ReflectionFunction(#[A5] function () { });
try {
$ref->getAttributes()[0]->newInstance();
} catch (\Error $e) {
var_dump('ERROR 6', $e->getMessage());
}
?>
--EXPECTF--
string(2) "A1"
string(4) "test"
int(50)
string(7) "ERROR 1"
string(%d) "Too few arguments to function A1::__construct(), 0 passed in %s005_objects.php on line 26 and at least 1 expected"
string(7) "ERROR 2"
string(%d) "A1::__construct(): Argument #1 ($name) must be of type string, array given, called in %s005_objects.php on line 36"
string(7) "ERROR 3"
string(30) "Attribute class "A2" not found"
string(7) "ERROR 4"
string(48) "Attribute constructor of class A3 must be public"
string(7) "ERROR 5"
string(69) "Attribute class A4 does not have a constructor, cannot pass arguments"
string(7) "ERROR 6"
string(55) "Attempting to use non-attribute class "A5" as attribute"