Skip to content

Attribute classsynopsis: simplified #[\Attribute] vs full targets #5849

Description

@lacatoire

Affected page

All attribute class synopsis pages:

Current issue

All attribute class pages show #[\Attribute] in their classsynopsis (added in #5067). This is the shorthand form, which in PHP compiles to #[\Attribute(Attribute::TARGET_ALL)], meaning the attribute can be applied anywhere.

The actual declarations in the PHP stubs are more restrictive:

Class Stubs
Attribute #[Attribute(Attribute::TARGET_CLASS)]
AllowDynamicProperties #[Attribute(Attribute::TARGET_CLASS)]
Override #[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_PROPERTY|Attribute::TARGET_CLASS_CONSTANT)]
SensitiveParameter #[Attribute(Attribute::TARGET_PARAMETER)]
Deprecated #[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION|Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_CONSTANT|Attribute::TARGET_CLASS)]
NoDiscard #[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION)]
DelayedTargetValidation #[Attribute(Attribute::TARGET_ALL)]

A reader who treats the classsynopsis as valid PHP will conclude that #[Override] can be placed on a class or function, which is wrong.

Suggested improvement

Two options:

  • Full form: replace #[\Attribute] with the accurate target list from the stubs in each classsynopsis. Mirrors the source exactly, but requires updating doc-en each time PHP adds a target (e.g. Override gained TARGET_PROPERTY in PHP 8.5).
  • Prose note: keep #[\Attribute] in the classsynopsis and add a sentence in the description listing the valid targets. Lower maintenance, same accuracy.

Additional context

Related: #3897 (superseded by #5067, which adopted the simplified form without discussion of this tradeoff).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions