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
gsherwood
merged 1 commit into
squizlabs:master
from
jrfnl:feature/tokenizer-declare-alternative-syntax
Jan 30, 2020
Merged
Tokenizer does not support alternative syntax for declare statements #2843
gsherwood
merged 1 commit into
squizlabs:master
from
jrfnl:feature/tokenizer-declare-alternative-syntax
Jan 30, 2020
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`.
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
changed the title
Tokenizer/PHP: allow for alternative syntax for declare
Tokenizer does not support alternative syntax for declare statements
Jan 30, 2020
Thanks a lot for this. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
declare
language construct can also use the alternative control structure syntax, but in that case would not get assignedscope_opener
orscope_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 theenddeclare
.