|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Formidable_Sniffs_WhiteSpace_NoBlankLineAfterLoopOpenSniff |
| 4 | + * |
| 5 | + * Ensures there is no blank line immediately after the opening brace of a loop. |
| 6 | + * |
| 7 | + * @package Formidable\Sniffs |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Formidable\Sniffs\WhiteSpace; |
| 11 | + |
| 12 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | + |
| 15 | +/** |
| 16 | + * Ensures there is no blank line immediately after the opening brace of a loop. |
| 17 | + * |
| 18 | + * Bad: |
| 19 | + * foreach ( $items as $item ) { |
| 20 | + * |
| 21 | + * do_something( $item ); |
| 22 | + * } |
| 23 | + * |
| 24 | + * Good: |
| 25 | + * foreach ( $items as $item ) { |
| 26 | + * do_something( $item ); |
| 27 | + * } |
| 28 | + */ |
| 29 | +class NoBlankLineAfterLoopOpenSniff implements Sniff { |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns an array of tokens this test wants to listen for. |
| 33 | + * |
| 34 | + * @return array |
| 35 | + */ |
| 36 | + public function register() { |
| 37 | + return array( T_FOREACH, T_FOR, T_WHILE, T_DO ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Processes this test, when one of its tokens is encountered. |
| 42 | + * |
| 43 | + * @param File $phpcsFile The file being scanned. |
| 44 | + * @param int $stackPtr The position of the current token in the stack passed in $tokens. |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function process( File $phpcsFile, $stackPtr ) { |
| 49 | + $tokens = $phpcsFile->getTokens(); |
| 50 | + |
| 51 | + // Make sure this loop has a scope (curly braces). |
| 52 | + if ( ! isset( $tokens[ $stackPtr ]['scope_opener'] ) ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $scopeOpener = $tokens[ $stackPtr ]['scope_opener']; |
| 57 | + $scopeCloser = $tokens[ $stackPtr ]['scope_closer']; |
| 58 | + |
| 59 | + // Find the first non-whitespace token after the opening brace. |
| 60 | + $firstContent = $phpcsFile->findNext( T_WHITESPACE, $scopeOpener + 1, $scopeCloser, true ); |
| 61 | + |
| 62 | + if ( false === $firstContent ) { |
| 63 | + // Empty loop body. |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $openerLine = $tokens[ $scopeOpener ]['line']; |
| 68 | + $contentLine = $tokens[ $firstContent ]['line']; |
| 69 | + |
| 70 | + // Check if there's a blank line between the opener and first content. |
| 71 | + // A blank line means the content is more than 1 line after the opener. |
| 72 | + if ( $contentLine <= $openerLine + 1 ) { |
| 73 | + // No blank line, content is on the next line or same line. |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $fix = $phpcsFile->addFixableError( |
| 78 | + 'No blank line should follow the opening brace of a loop.', |
| 79 | + $scopeOpener, |
| 80 | + 'BlankLineAfterLoopOpen' |
| 81 | + ); |
| 82 | + |
| 83 | + if ( true === $fix ) { |
| 84 | + $phpcsFile->fixer->beginChangeset(); |
| 85 | + |
| 86 | + // Find the whitespace token right after the opener that contains the blank line. |
| 87 | + $nextToken = $scopeOpener + 1; |
| 88 | + |
| 89 | + if ( $tokens[ $nextToken ]['code'] === T_WHITESPACE ) { |
| 90 | + $content = $tokens[ $nextToken ]['content']; |
| 91 | + $newlineCount = substr_count( $content, "\n" ); |
| 92 | + |
| 93 | + if ( $newlineCount >= 2 ) { |
| 94 | + // Multiple newlines - reduce to single newline + indentation. |
| 95 | + $lastNewline = strrpos( $content, "\n" ); |
| 96 | + $indent = substr( $content, $lastNewline + 1 ); |
| 97 | + $phpcsFile->fixer->replaceToken( $nextToken, "\n" . $indent ); |
| 98 | + } elseif ( $newlineCount === 1 ) { |
| 99 | + // Single newline here, but there might be another whitespace token creating the blank. |
| 100 | + // Check if the next token is also whitespace with a newline. |
| 101 | + $afterNext = $nextToken + 1; |
| 102 | + |
| 103 | + if ( $afterNext < $firstContent && $tokens[ $afterNext ]['code'] === T_WHITESPACE ) { |
| 104 | + // Remove this extra whitespace token. |
| 105 | + $phpcsFile->fixer->replaceToken( $afterNext, '' ); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $phpcsFile->fixer->endChangeset(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments