-
Notifications
You must be signed in to change notification settings - Fork 6
PHP 8 attributes #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP 8 attributes #243
Conversation
Unfortunately, PHP 8 attributes do not support all values which are supported in XP annotations, e.g. instantations, closures, static member references: use unittest\Assert;
use unittest\actions\VerifyThat;
#[@action(new VerifyThat(function() {
# return PHP_VERSION_ID >= 70000;
#}))]
class NameTest {
#[@test, @values([
# [Name::$EMPTY],
# [new Name('Test')],
# [new Name('Test J. West')],
#])]
public function is_valid($name) {
Assert::true($name->valid());
}
} PHP will yield a parse error - |
A possible workaround would be to use a special kind of array argument like this: use unittest\Action;
use unittest\actions\VerifyThat;
// Inline as code. Downside: Escaping, manual import resolution necessary
<<Action([this => 'new VerifyThat(function() { return PHP_VERSION_ID >= 70000; })'])>>
class NameTest {
}
// Via magic method. Downside: Far away from method declarations
<<Action([this => 'function'])>>
class NameTest {
static function __attributes() {
return [self::class => new VerifyThat(function() { return PHP_VERSION_ID >= 70000; })];
}
}
|
Another possibility is to mix syntaxes: use net\xp_framework\unittest\annotations\Name;
class NameTest {
<<Test>>
#[@Values([
# new Name('Test'),
# new Name('Test T. West'),
# Name::$EMPTY,
#])]
public function is_valid($name) {
// TBI
}
} (which even already works!) ...or maybe even with a special "inline" notation: use net\xp_framework\unittest\annotations\Name;
class NameTest {
<<Test>>
<<Values(
#[
# new Name('Test'),
# new Name('Test T. West'),
# Name::$EMPTY,
#]
)>>
public function is_valid($name) {
// TBI
}
} |
Implemented as noted above ✅ |
Waiting to see if koolkode/php-src#1 (Attribute Grouping) is merged - see https://wiki.php.net/rfc/attribute_amendments. This PR would then need some additional tweaks. |
Will need adjustments when php/php-src#5796 is merged, see https://wiki.php.net/rfc/shorter_attribute_syntax |
See https://wiki.php.net/rfc/attributes_v2 - another step forward to full PHP 8 support (see #211). Non-scalar values (which are not supported by PHP) can be written in an inline comment form.
For BC reasons, lowercased annotations are not resolved