-
Notifications
You must be signed in to change notification settings - Fork 1
Whitespace around control structures #15
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
Merged
indigoxela
merged 2 commits into
backdrop-ops:main
from
yorkshire-pudding:issue-14-spaces-control-structures
Jul 27, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
297 changes: 297 additions & 0 deletions
297
Backdrop/Sniffs/ControlStructures/ControlSignatureSniff.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| <?php | ||
| /** | ||
| * Verifies that control statements conform to their coding standards. | ||
| * | ||
| * Forked from https://github.com/squizlabs/PHP_CodeSniffer/. | ||
| * | ||
| * @author Greg Sherwood <gsherwood@squiz.net> | ||
| * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) | ||
| * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
| */ | ||
|
|
||
| namespace Backdrop\Sniffs\ControlStructures; | ||
|
|
||
| use PHP_CodeSniffer\Files\File; | ||
| use PHP_CodeSniffer\Sniffs\Sniff; | ||
| use PHP_CodeSniffer\Util\Tokens; | ||
|
|
||
| class ControlSignatureSniff implements Sniff | ||
| { | ||
|
|
||
| /** | ||
| * How many spaces should precede the colon if using alternative syntax. | ||
| * | ||
| * @var integer | ||
| */ | ||
| public $requiredSpacesBeforeColon = 1; | ||
|
|
||
| /** | ||
| * A list of tokenizers this sniff supports. | ||
| * | ||
| * @var array | ||
| */ | ||
| public $supportedTokenizers = [ | ||
| 'PHP', | ||
| 'JS', | ||
| ]; | ||
|
|
||
|
|
||
| /** | ||
| * Returns an array of tokens this test wants to listen for. | ||
| * | ||
| * @return int[] | ||
| */ | ||
| public function register() | ||
| { | ||
| return [ | ||
| T_TRY, | ||
| T_CATCH, | ||
| T_FINALLY, | ||
| T_DO, | ||
| T_WHILE, | ||
| T_FOR, | ||
| T_IF, | ||
| T_FOREACH, | ||
| T_ELSE, | ||
| T_ELSEIF, | ||
| T_SWITCH, | ||
| T_MATCH, | ||
| ]; | ||
|
|
||
| }//end register() | ||
|
|
||
|
|
||
| /** | ||
| * Processes this test, when one of its tokens is encountered. | ||
| * | ||
| * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
| * @param int $stackPtr The position of the current token in the | ||
| * stack passed in $tokens. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function process(File $phpcsFile, $stackPtr) | ||
| { | ||
| $tokens = $phpcsFile->getTokens(); | ||
|
|
||
| $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); | ||
| if ($nextNonEmpty === false) { | ||
| return; | ||
| } | ||
|
|
||
| $isAlternative = false; | ||
| if (isset($tokens[$stackPtr]['scope_opener']) === true | ||
| && $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON | ||
| ) { | ||
| $isAlternative = true; | ||
| } | ||
|
|
||
| // Single space after the keyword. | ||
| $expected = 1; | ||
| if (isset($tokens[$stackPtr]['parenthesis_closer']) === false && $isAlternative === true) { | ||
| // Catching cases like: | ||
| // if (condition) : ... else: ... endif | ||
| // where there is no condition. | ||
| $expected = (int) $this->requiredSpacesBeforeColon; | ||
| } | ||
|
|
||
| $found = 1; | ||
| if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { | ||
| $found = 0; | ||
| } else if ($tokens[($stackPtr + 1)]['content'] !== ' ') { | ||
| if (strpos($tokens[($stackPtr + 1)]['content'], $phpcsFile->eolChar) !== false) { | ||
| $found = 'newline'; | ||
| } else { | ||
| $found = $tokens[($stackPtr + 1)]['length']; | ||
| } | ||
| } | ||
|
|
||
| if ($found !== $expected) { | ||
| $error = 'Expected %s space(s) after %s keyword; %s found'; | ||
| $data = [ | ||
| $expected, | ||
| strtoupper($tokens[$stackPtr]['content']), | ||
| $found, | ||
| ]; | ||
|
|
||
| $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterKeyword', $data); | ||
| if ($fix === true) { | ||
| if ($found === 0) { | ||
| $phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $expected)); | ||
| } else { | ||
| $phpcsFile->fixer->replaceToken(($stackPtr + 1), str_repeat(' ', $expected)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Single space after closing parenthesis. | ||
| if (isset($tokens[$stackPtr]['parenthesis_closer']) === true | ||
| && isset($tokens[$stackPtr]['scope_opener']) === true | ||
| ) { | ||
| $expected = 1; | ||
| if ($isAlternative === true) { | ||
| $expected = (int) $this->requiredSpacesBeforeColon; | ||
| } | ||
|
|
||
| $closer = $tokens[$stackPtr]['parenthesis_closer']; | ||
| $opener = $tokens[$stackPtr]['scope_opener']; | ||
| $content = $phpcsFile->getTokensAsString(($closer + 1), ($opener - $closer - 1)); | ||
|
|
||
| if (trim($content) === '') { | ||
| if (strpos($content, $phpcsFile->eolChar) !== false) { | ||
| $found = 'newline'; | ||
| } else { | ||
| $found = strlen($content); | ||
| } | ||
| } else { | ||
| $found = '"'.str_replace($phpcsFile->eolChar, '\n', $content).'"'; | ||
| } | ||
|
|
||
| if ($found !== $expected) { | ||
| $error = 'Expected %s space(s) after closing parenthesis; found %s'; | ||
| $data = [ | ||
| $expected, | ||
| $found, | ||
| ]; | ||
|
|
||
| $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceAfterCloseParenthesis', $data); | ||
| if ($fix === true) { | ||
| $padding = str_repeat(' ', $expected); | ||
| if ($closer === ($opener - 1)) { | ||
| $phpcsFile->fixer->addContent($closer, $padding); | ||
| } else { | ||
| $phpcsFile->fixer->beginChangeset(); | ||
| if (trim($content) === '') { | ||
| $phpcsFile->fixer->addContent($closer, $padding); | ||
| if ($found !== 0) { | ||
| for ($i = ($closer + 1); $i < $opener; $i++) { | ||
| $phpcsFile->fixer->replaceToken($i, ''); | ||
| } | ||
| } | ||
| } else { | ||
| $phpcsFile->fixer->addContent($closer, $padding.$tokens[$opener]['content']); | ||
| $phpcsFile->fixer->replaceToken($opener, ''); | ||
|
|
||
| if ($tokens[$opener]['line'] !== $tokens[$closer]['line']) { | ||
| $next = $phpcsFile->findNext(T_WHITESPACE, ($opener + 1), null, true); | ||
| if ($tokens[$next]['line'] !== $tokens[$opener]['line']) { | ||
| for ($i = ($opener + 1); $i < $next; $i++) { | ||
| $phpcsFile->fixer->replaceToken($i, ''); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $phpcsFile->fixer->endChangeset(); | ||
| }//end if | ||
| }//end if | ||
| }//end if | ||
| }//end if | ||
|
|
||
| // Single newline after opening brace. | ||
| if (isset($tokens[$stackPtr]['scope_opener']) === true) { | ||
| $opener = $tokens[$stackPtr]['scope_opener']; | ||
| for ($next = ($opener + 1); $next < $phpcsFile->numTokens; $next++) { | ||
| $code = $tokens[$next]['code']; | ||
|
|
||
| if ($code === T_WHITESPACE | ||
| || ($code === T_INLINE_HTML | ||
| && trim($tokens[$next]['content']) === '') | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip all empty tokens on the same line as the opener. | ||
| if ($tokens[$next]['line'] === $tokens[$opener]['line'] | ||
| && (isset(Tokens::$emptyTokens[$code]) === true | ||
| || $code === T_CLOSE_TAG) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| // We found the first bit of a code, or a comment on the | ||
| // following line. | ||
| break; | ||
| }//end for | ||
|
|
||
| if ($tokens[$next]['line'] === $tokens[$opener]['line']) { | ||
| $error = 'Newline required after opening brace'; | ||
| $fix = $phpcsFile->addFixableError($error, $opener, 'NewlineAfterOpenBrace'); | ||
| if ($fix === true) { | ||
| $phpcsFile->fixer->beginChangeset(); | ||
| for ($i = ($opener + 1); $i < $next; $i++) { | ||
| if (trim($tokens[$i]['content']) !== '') { | ||
| break; | ||
| } | ||
|
|
||
| // Remove whitespace. | ||
| $phpcsFile->fixer->replaceToken($i, ''); | ||
| } | ||
|
|
||
| $phpcsFile->fixer->addContent($opener, $phpcsFile->eolChar); | ||
| $phpcsFile->fixer->endChangeset(); | ||
| } | ||
| }//end if | ||
| } else if ($tokens[$stackPtr]['code'] === T_WHILE) { | ||
| // Zero spaces after parenthesis closer, but only if followed by a semicolon. | ||
| $closer = $tokens[$stackPtr]['parenthesis_closer']; | ||
| $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($closer + 1), null, true); | ||
| if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_SEMICOLON) { | ||
| $found = 0; | ||
| if ($tokens[($closer + 1)]['code'] === T_WHITESPACE) { | ||
| if (strpos($tokens[($closer + 1)]['content'], $phpcsFile->eolChar) !== false) { | ||
| $found = 'newline'; | ||
| } else { | ||
| $found = $tokens[($closer + 1)]['length']; | ||
| } | ||
| } | ||
|
|
||
| if ($found !== 0) { | ||
| $error = 'Expected 0 spaces before semicolon; %s found'; | ||
| $data = [$found]; | ||
| $fix = $phpcsFile->addFixableError($error, $closer, 'SpaceBeforeSemicolon', $data); | ||
| if ($fix === true) { | ||
| $phpcsFile->fixer->replaceToken(($closer + 1), ''); | ||
| } | ||
| } | ||
| } | ||
| }//end if | ||
|
|
||
| // Only want to check multi-keyword structures from here on. | ||
| if ($tokens[$stackPtr]['code'] === T_WHILE) { | ||
| if (isset($tokens[$stackPtr]['scope_closer']) !== false) { | ||
| return; | ||
| } | ||
|
|
||
| $closer = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); | ||
| if ($closer === false | ||
| || $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET | ||
| || $tokens[$tokens[$closer]['scope_condition']]['code'] !== T_DO | ||
| ) { | ||
| return; | ||
| } | ||
| } else if ($tokens[$stackPtr]['code'] === T_ELSE | ||
| || $tokens[$stackPtr]['code'] === T_ELSEIF | ||
| || $tokens[$stackPtr]['code'] === T_CATCH | ||
| || $tokens[$stackPtr]['code'] === T_FINALLY | ||
| ) { | ||
| if (isset($tokens[$stackPtr]['scope_opener']) === true | ||
| && $tokens[$tokens[$stackPtr]['scope_opener']]['code'] === T_COLON | ||
| ) { | ||
| // Special case for alternate syntax, where this token is actually | ||
| // the closer for the previous block, so there is no spacing to check. | ||
| return; | ||
| } | ||
|
|
||
| $closer = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); | ||
| if ($closer === false || $tokens[$closer]['code'] !== T_CLOSE_CURLY_BRACKET) { | ||
| return; | ||
| } | ||
| } else { | ||
| return; | ||
| }//end if | ||
|
|
||
| }//end process() | ||
|
|
||
|
|
||
| }//end class | ||
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.
Uh oh!
There was an error while loading. Please reload this page.