Skip to content

Attribute Amendments #5751

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

Closed
wants to merge 1 commit into from
Closed

Attribute Amendments #5751

wants to merge 1 commit into from

Conversation

kooldev
Copy link
Contributor

@kooldev kooldev commented Jun 22, 2020

Implements all changes that have been proposed by @beberlei and myself in RFC: Attribute Amendments. Support for attribute grouping is left out because Shorter Attribute Syntax will likely make it obsolete. All new features are demonstrated in this little example:

<<Attribute(Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)>>
class A1 { }

$ref = new ReflectionFunction(<<A1>> <<A1>> function () {});

foreach ($ref->getAttributes(A1::class) as $a) {
    var_dump($a->getName(), $a->getTarget(), $a->isRepeated(), $a->newInstance());
}

These are the extensions to existing APIs:

final class Attribute
{
    public const TARGET_CLASS = 1;
    public const TARGET_FUNCTION = (1 << 1);
    public const TARGET_METHOD = (1 << 2);
    public const TARGET_PROPERTY = (1 << 3);
    public const TARGET_CLASS_CONST = (1 << 4);
    public const TARGET_PARAMETER = (1 << 5);
    public const TARGET_ALL = ((1 << 6) - 1);
    public const IS_REPEATABLE = (1 << 6);

    public int $flags;

    public function __construct(int $flags = self::TARGET_ALL) { }
}

final class ReflectionAttribute
{
    ...
    public function getTarget(): int { }
    public function isRepeated(): bool {}
}

@nikic
Copy link
Member

nikic commented Jun 23, 2020

To avoid unnecessary back and forth in the AST representation, let's drop the grouped attribute syntax, which will dropped anyway once https://wiki.php.net/rfc/shorter_attribute_syntax finishes voting.

@kooldev
Copy link
Contributor Author

kooldev commented Jun 23, 2020

Attribute grouping has been removed from the PR.

@kooldev
Copy link
Contributor Author

kooldev commented Jun 28, 2020

@nikic All review comments have been addressed. I rebased the PR on latest master and squashed all commits into a single one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants