|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ModernTribe_Sniffs_Arrays_ArrayBracketSpacingSniff. |
| 4 | + * |
| 5 | + * This is a shameless copy of the work done by Squizlabs, specifically |
| 6 | + * Greg Sherwood <gsherwood@squiz.net> and Marc McIntyre <mmcintyre@squiz.net>, |
| 7 | + * but modified to match ModernTribe standards. |
| 8 | + * |
| 9 | + * PHP version 5 |
| 10 | + * |
| 11 | + * @category PHP |
| 12 | + * @package PHP_CodeSniffer |
| 13 | + * @author Matthew Batchelder <borkweb@gmail.com> |
| 14 | + * @author Zachary Tirrell <zbtirrell@gmail.com> |
| 15 | + * @copyright 2012 ModernTribe |
| 16 | + * @license https://github.com/ModernTribe/ModernTribe-codesniffer/blob/master/licence.txt BSD Licence |
| 17 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff. |
| 22 | + * |
| 23 | + * Ensure that there are no spaces around square brackets. |
| 24 | + * |
| 25 | + * @category PHP |
| 26 | + * @package PHP_CodeSniffer |
| 27 | + * @author Matthew Batchelder <borkweb@gmail.com> |
| 28 | + * @author Zachary Tirrell <zbtirrell@gmail.com> |
| 29 | + * @copyright 2012 ModernTribe |
| 30 | + * @license https://github.com/ModernTribe/ModernTribe-codesniffer/blob/master/licence.txt BSD Licence |
| 31 | + * @version Release: 1.4.0 |
| 32 | + * @link http://pear.php.net/package/PHP_CodeSniffer |
| 33 | + */ |
| 34 | +class ModernTribe_Sniffs_Arrays_ArrayBracketSpacingSniff implements PHP_CodeSniffer_Sniff |
| 35 | +{ |
| 36 | + /** |
| 37 | + * Returns an array of tokens this test wants to listen for. |
| 38 | + * |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + public function register() |
| 42 | + { |
| 43 | + return array( |
| 44 | + T_OPEN_SQUARE_BRACKET, |
| 45 | + T_CLOSE_SQUARE_BRACKET, |
| 46 | + ); |
| 47 | + |
| 48 | + }//end register() |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Processes this sniff, when one of its tokens is encountered. |
| 53 | + * |
| 54 | + * @param PHP_CodeSniffer_File $phpcsFile The current file being checked. |
| 55 | + * @param int $stackPtr The position of the current token in the |
| 56 | + * stack passed in $tokens. |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 61 | + { |
| 62 | + $tokens = $phpcsFile->getTokens(); |
| 63 | + |
| 64 | + // PHP 5.4 introduced a shorthand array declaration syntax, so we need |
| 65 | + // to ignore the these type of array declarations because this sniff is |
| 66 | + // only dealing with array usage. |
| 67 | + if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) { |
| 68 | + $openBracket = $stackPtr; |
| 69 | + } else { |
| 70 | + $openBracket = $tokens[$stackPtr]['bracket_opener']; |
| 71 | + } |
| 72 | + |
| 73 | + $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket - 1), null, true); |
| 74 | + if ($tokens[$prev]['code'] === T_EQUAL) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Square brackets can not have a space before them. |
| 79 | + $prevType = $tokens[($stackPtr - 1)]['code']; |
| 80 | + if ('[' == $tokens[$stackPtr]['content'] && in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) { |
| 81 | + $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true); |
| 82 | + $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content']; |
| 83 | + $found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content']; |
| 84 | + $error = 'Space found before square bracket; expected "%s" but found "%s"'; |
| 85 | + $data = array( |
| 86 | + $expected, |
| 87 | + $found, |
| 88 | + ); |
| 89 | + $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeBracket', $data); |
| 90 | + } |
| 91 | + }//end process() |
| 92 | +}//end class |
0 commit comments