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

Tokenizer does not support alternative syntax for declare statements #2843

Merged

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Jan 30, 2020

The declare language construct can also use the alternative control structure syntax, but in that case would not get assigned scope_opener or scope_closer indexes.

This fixes that.

Includes unit test via the PSR1.Files.SideEffects sniff, which also needed as small tweak to disregard the semicolon after the enddeclare.

The `declare` language construct can also use the alternative control structure syntax, but in that case would not get assigned `scope_opener` or `scope_closer` indexes.

This fixed that.

Includes unit test via the `PSR1.Files.SideEffects` sniff, which also needed as small tweaks to disregard the semicolon after the `enddeclare`.
@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Jan 30, 2020
@gsherwood gsherwood added this to the 3.5.4 milestone Jan 30, 2020
jrfnl added a commit to PHPCSStandards/PHPCSUtils that referenced this pull request 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](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 added a commit to PHPCSStandards/PHPCSUtils that referenced this pull request 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](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.
@gsherwood gsherwood changed the title Tokenizer/PHP: allow for alternative syntax for declare Tokenizer does not support alternative syntax for declare statements Jan 30, 2020
gsherwood added a commit that referenced this pull request Jan 30, 2020
@gsherwood gsherwood merged commit b80a2b4 into squizlabs:master Jan 30, 2020
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Jan 30, 2020
@gsherwood
Copy link
Member

Thanks a lot for this.

@jrfnl jrfnl deleted the feature/tokenizer-declare-alternative-syntax branch January 30, 2020 21:56
@jrfnl
Copy link
Contributor Author

jrfnl commented Jan 30, 2020

You're welcome ;-)

tobias-trozowski pushed a commit to tobias-trozowski/PHP_CodeSniffer that referenced this pull request Feb 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

None yet

2 participants