Skip to content

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Sep 22, 2025

... to contain utility methods for analysing attribute blocks, where an attribute block is defined as being an attribute opener, an attribute closer and everything between. I.e. #[MyAttribute(1, 2), AnotherAttribute] is one attribute block.

Initially, the class comes with the following methods:

  • getAttributes(File $phpcsFile, $stackPtr): array to retrieve an array of information about each attribute referenced in an attribute block. The returned array will contain the following information on each attribute in the block:
    • name (string): the complete name of the attribute as found in the attribute block.
    • name_token (int): stack pointer to the last token in the name (which can be passed to the methods in the PassedParameters class to retrieve any passed arguments).
    • start (int): the stack pointer to the first token in the attribute instantiation. Mind: this may be a whitespace (or comment token), like for AnotherAttribute in the example above.
    • end (int): the stack pointer to the last token in the attribute instantiation. Again, mind: this may be a whitespace/comment token.
    • comma_token (int|false): the stack pointer to the comma after the attribute instantiation or false if this is the last attribute and there is no comma.
  • countAttributes(File $phpcsFile, $stackPtr): int: convenience method to count the number of attribute instantiations in an attribute block.

These methods expect to be passed a T_ATTRIBUTE (attribute block opener) token as the $stackPtr.

Includes extensive unit tests.

Related to #616

@jrfnl
Copy link
Member Author

jrfnl commented Sep 28, 2025

Added one more test (ParseError3).

... to contain utility methods for analysing attribute blocks, where an attribute block is defined as being an attribute opener, an attribute closer and everything between.
I.e. `#[MyAttribute(1, 2), AnotherAttribute]` is one attribute block.

Initially, the class comes with the following methods:
* `getAttributes(File $phpcsFile, $stackPtr): array` to retrieve an array of information about each attribute referenced in an attribute block.
    The returned array will contain the following information on each attribute in the block:
    - `name` (`string`): the complete name of the attribute as found in the attribute block.
    - `name_token` (`int`): stack pointer to the last token in the name (which can be passed to the methods in the `PassedParameters` class to retrieve any passed arguments).
    - `start` (`int`): the stack pointer to the first token in the attribute instantiation. Mind: this may be a whitespace (or comment token), like for `AnotherAttribute` in the example above.
    - `end` (`int`): the stack pointer to the last token in the attribute instantiation. Again, mind: this may be a whitespace/comment token.
    - `comma_token` (`int|false`): the stack pointer to the comma after the attribute instantiation or `false` if this is the last attribute and there is no comma.
* `countAttributes(File $phpcsFile, $stackPtr): int`: convenience method to count the number of attribute instantiations in an attribute block.

These methods expect to be passed a `T_ATTRIBUTE` (attribute block opener) token as the `$stackPtr`.

Includes extensive unit tests.

Related to #616
@jrfnl jrfnl force-pushed the feature/616-new-attribute-block-utility branch from 1b4153a to fcdaaad Compare November 10, 2025 19:28
@jrfnl jrfnl merged commit f660eca into develop Nov 10, 2025
82 checks passed
@jrfnl jrfnl deleted the feature/616-new-attribute-block-utility branch November 10, 2025 19:42
@jrfnl jrfnl mentioned this pull request Nov 11, 2025
29 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants