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_IF
orT_ELSE
token is part of anelse if
. Returns boolean.getDeclareScopeOpenClose()
- to retrieve the scope open/close pointers fordeclare
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:
declare(ticks=1);
;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.