New Utils\ControlStructures class#70
Merged
Merged
Conversation
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.
This file contains hidden or 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
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.
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);andwhile (++$i < 10) { /* do something */ }.isElseIf()- to check whether anT_IForT_ELSEtoken is part of anelse if. Returns boolean.getDeclareScopeOpenClose()- to retrieve the scope open/close pointers fordeclarestatements. 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:
declare(ticks=1);;In that last case, PHPCS does not assign the scope open/close indexes in the
$tokensarray. 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\Collectionsclass.$alternativeControlStructureSyntaxTokens- tokens which can use the alternative syntax for control structures.$controlStructureTokens- control structure tokens.Includes extensive dedicated unit tests.