Skip to content
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

New Utils\ControlStructures class #70

Merged
merged 1 commit into from
Jan 30, 2020

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 30, 2020

This adds three new utility methods:

  • hasBody() - to check whether a control structure has a body and optionally whether that body is non-empty. Returns boolean.
    This is a helper function to distinguish between structures like while (++$i < 10); and while (++$i < 10) { /* do something */ }.
  • isElseIf() - to check whether an T_IF or T_ELSE token is part of an else if. Returns boolean.
  • getDeclareScopeOpenClose() - to retrieve the scope open/close pointers for declare statements. Returns an array with the stack pointers or false if a file based statement or if the scope open/close pointers could not be determined.
    Declare statements come in three flavours:
    • file based, without body: declare(ticks=1);;
    • scoped using curly braces;
    • scoped using the alternative control structure syntax.
      In that last case, PHPCS does not assign the scope open/close indexes in the $tokens array. A PR is open to fix this, but for any sniff which needs to support versions of PHPCS prior to the version in which that PR will be merged, this helper method can retrieve the scope open/close indexes.

This commit also adds two convenience token arrays for working with control structures to the PHPCSUtils\Tokens\Collections class.

  • $alternativeControlStructureSyntaxTokens - tokens which can use the alternative syntax for control structures.
  • $controlStructureTokens - control structure tokens.

Includes extensive dedicated unit tests.

This adds three new utility methods:
* `hasBody()` - to check whether a control structure has a body and optionally whether that body is non-empty. Returns boolean.
    This is a helper function to distinguish between structures like `while (++$i < 10);` and `while (++$i < 10) { /* do something */ }`.
* `isElseIf()` - to check whether an `T_IF` or `T_ELSE` token is part of an `else if`. Returns boolean.
* `getDeclareScopeOpenClose()` - to retrieve the scope open/close pointers for `declare` statements. Returns an array with the stack pointers or false if a file based statement or if the scope open/close pointers could not be determined.
    Declare statements come in three flavours:
    - file based, without body: `declare(ticks=1);`;
    - scoped using curly braces;
    - scoped using the alternative control structure syntax.
    In that last case, PHPCS does not assign the scope open/close indexes in the `$tokens` array. A [PR is open to fix this](squizlabs/PHP_CodeSniffer#2843), but for any sniff which needs to support versions of PHPCS prior to the version in which that PR will be merged, this helper method can retrieve the scope open/close indexes.

This commit also adds two convenience token arrays for working with control structures to the `PHPCSUtils\Tokens\Collections` class.
* `$alternativeControlStructureSyntaxTokens` - tokens which can use the alternative syntax for control structures.
* `$controlStructureTokens` - control structure tokens.

Includes extensive dedicated unit tests.
@jrfnl jrfnl added this to the 1.0.0 milestone Jan 30, 2020
@jrfnl jrfnl merged commit 8d9cf7b into develop Jan 30, 2020
@jrfnl jrfnl deleted the feature/new-controlstructures-class branch January 30, 2020 19:31
@jrfnl jrfnl modified the milestones: 1.0.0, 1.0.0-alpha2 May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant