|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Symfony2-coding-standard (phpcs standard) |
| 5 | + * |
| 6 | + * PHP version 5 |
| 7 | + * |
| 8 | + * @category PHP |
| 9 | + * @package PHP_CodeSniffer-Symfony2 |
| 10 | + * @author Symfony2-phpcs-authors <[email protected]> |
| 11 | + * @license http://spdx.org/licenses/MIT MIT License |
| 12 | + * @version GIT: master |
| 13 | + * @link https://github.com/opensky/Symfony2-coding-standard |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Symfony2_Sniffs_Instaclick_BlankLineBeforeIfSniff. |
| 18 | + * |
| 19 | + * Throws errors if there's no blank line before if statements. Symfony |
| 20 | + * coding standard specifies: "Add a blank line before if statements, |
| 21 | + * unless the if is alone inside a statement-group (like an if statement);" |
| 22 | + * |
| 23 | + * @category PHP |
| 24 | + * @package PHP_CodeSniffer-Symfony2 |
| 25 | + * @author Dave Hauenstein <[email protected]> |
| 26 | + * @license http://spdx.org/licenses/MIT MIT License |
| 27 | + * @link https://github.com/opensky/Symfony2-coding-standard |
| 28 | + */ |
| 29 | +class Symfony2_Sniffs_Instaclick_BlankLineBeforeIfSniff implements PHP_CodeSniffer_Sniff |
| 30 | +{ |
| 31 | + /** |
| 32 | + * A list of tokenizers this sniff supports. |
| 33 | + * |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + public $supportedTokenizers = array( |
| 37 | + 'PHP', |
| 38 | + 'JS', |
| 39 | + ); |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns an array of tokens this test wants to listen for. |
| 43 | + * |
| 44 | + * @return array |
| 45 | + */ |
| 46 | + public function register() |
| 47 | + { |
| 48 | + return array(T_IF); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Processes this test, when one of its tokens is encountered. |
| 53 | + * |
| 54 | + * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
| 55 | + * @param int $stackPtr The position of the current token in |
| 56 | + * the stack passed in $tokens. |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 61 | + { |
| 62 | + $tokens = $phpcsFile->getTokens(); |
| 63 | + $current = $stackPtr; |
| 64 | + $previousLine = $tokens[$stackPtr]['line'] - 1; |
| 65 | + $prevLineTokens = array(); |
| 66 | + |
| 67 | + while ($current >= 0 && $tokens[$current]['line'] >= $previousLine) { |
| 68 | + if ($tokens[$current]['line'] == $previousLine |
| 69 | + && $tokens[$current]['type'] !== 'T_WHITESPACE' |
| 70 | + && $tokens[$current]['type'] !== 'T_COMMENT' |
| 71 | + ) { |
| 72 | + $prevLineTokens[] = $tokens[$current]['type']; |
| 73 | + } |
| 74 | + $current--; |
| 75 | + } |
| 76 | + |
| 77 | + if (isset($prevLineTokens[0]) |
| 78 | + && ($prevLineTokens[0] === 'T_OPEN_CURLY_BRACKET' |
| 79 | + || $prevLineTokens[0] === 'T_COLON') |
| 80 | + ) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + if (count($prevLineTokens) > 0) { |
| 85 | + $phpcsFile->addError( |
| 86 | + 'Missing blank line before if statement', |
| 87 | + $stackPtr |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return; |
| 92 | + } |
| 93 | +} |
0 commit comments