|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Aztech\Sniffs\Formatting; |
| 4 | + |
| 5 | +use Aztech\Sniffs\TokenIterator; |
| 6 | + |
| 7 | +/** |
| 8 | + * Sniff to detect that there are no consecutive blank lines in code. |
| 9 | + * |
| 10 | + * @author thibaud |
| 11 | + */ |
| 12 | +class ScopeFormattingSniff implements \PHP_CodeSniffer_Sniff |
| 13 | +{ |
| 14 | + |
| 15 | + public function register() |
| 16 | + { |
| 17 | + return [ |
| 18 | + T_FUNCTION |
| 19 | + ]; |
| 20 | + } |
| 21 | + |
| 22 | + public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 23 | + { |
| 24 | + $scope = $phpcsFile->getDeclarationName($stackPtr); |
| 25 | + $class = $phpcsFile->getDeclarationName($phpcsFile->findPrevious([ T_CLASS, T_INTERFACE], $stackPtr)); |
| 26 | + |
| 27 | + $tokens = $phpcsFile->getTokens(); |
| 28 | + $function = $tokens[$stackPtr]; |
| 29 | + |
| 30 | + $scopeTypes = [ |
| 31 | + T_IF, |
| 32 | + T_FOR, |
| 33 | + T_FOREACH, |
| 34 | + T_SWITCH |
| 35 | + ]; |
| 36 | + |
| 37 | + $functionStartPtr = $function['scope_opener']; |
| 38 | + $functionEndPtr = $function['scope_closer']; |
| 39 | + $ptr = $functionStartPtr; |
| 40 | + |
| 41 | + while ($ptr = $phpcsFile->findNext($scopeTypes, $ptr + 1, $functionEndPtr)) { |
| 42 | + $this->getBlankLineCountBefore($phpcsFile, $tokens, $functionStartPtr, $ptr); |
| 43 | + $this->getBlankLineCountAfter($phpcsFile, $tokens, $tokens[$ptr]['scope_closer'], $functionEndPtr); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private function getBlankLineCountBefore(\PHP_CodeSniffer_File $phpcsFile, $tokens, $startPtr, $endPtr) |
| 48 | + { |
| 49 | + $it = new TokenIterator($tokens, $startPtr, $endPtr); |
| 50 | + $brokenBy = null; |
| 51 | + $newLines = 0; |
| 52 | + |
| 53 | + foreach ($it->reverse() as $ptr => $token) { |
| 54 | + if ($token['code'] == T_SEMICOLON || $token['code'] == T_OPEN_CURLY_BRACKET || $token['code'] == T_CLOSE_CURLY_BRACKET) { |
| 55 | + $brokenBy = $token; |
| 56 | + |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + if ($token['code'] == T_WHITESPACE) { |
| 61 | + $content = str_replace(' ', '', $token['content']); |
| 62 | + $newLines += substr_count($content, PHP_EOL); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if ($newLines != 2 && $brokenBy['code'] != T_OPEN_CURLY_BRACKET) { |
| 67 | + $error = 'Control blocks should be preceded by exactly one blank line (' . max(0, $newLines - 1) . ' found)'; |
| 68 | + $phpcsFile->addError($error, $endPtr, 'NotExactlyOneBlankLine'); |
| 69 | + } |
| 70 | + |
| 71 | + elseif ($newLines > 1 && $brokenBy['code'] == T_OPEN_CURLY_BRACKET) { |
| 72 | + $error = 'Immediately nested control blocks should not be preceded by blank lines (' . max(0, $newLines - 1) . ' found)'; |
| 73 | + $phpcsFile->addError($error, $ptr + 2, 'ExtraBlankLines'); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private function getBlankLineCountAfter(\PHP_CodeSniffer_File $phpcsFile, $tokens, $startPtr, $endPtr) |
| 78 | + { |
| 79 | + $it = new TokenIterator($tokens, $startPtr, $endPtr); |
| 80 | + $brokenBy = null; |
| 81 | + $newLines = 0; |
| 82 | + |
| 83 | + foreach ($it as $ptr => $token) { |
| 84 | + if ($token['code'] == T_SEMICOLON || $token['code'] == T_OPEN_CURLY_BRACKET || $token['code'] == T_CLOSE_CURLY_BRACKET) { |
| 85 | + $brokenBy = $token; |
| 86 | + |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + if ($token['code'] == T_WHITESPACE) { |
| 91 | + $content = str_replace(' ', '', $token['content']); |
| 92 | + $newLines += substr_count($content, PHP_EOL); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + if ($newLines != 2 && $brokenBy['code'] != T_CLOSE_CURLY_BRACKET) { |
| 98 | + $error = 'Control blocks should be followed by exactly one blank line (' . max(0, $newLines - 1) . ' found)'; |
| 99 | + $phpcsFile->addError($error, $endPtr, 'NotExactlyOneBlankLine'); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments