3
* Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
* @version CVS: $Id: ArrayBracketSpacingSniff.php,v 1.2 2007/10/23 06:05:14 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
20
* Ensure that there are no spaces around square brackets.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @author Marc McIntyre <mmcintyre@squiz.net>
26
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
* @version Release: 1.0.1
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff implements PHP_CodeSniffer_Sniff
36
* Returns an array of tokens this test wants to listen for.
40
public function register()
43
T_OPEN_SQUARE_BRACKET,
44
T_CLOSE_SQUARE_BRACKET,
51
* Processes this sniff, when one of its tokens is encountered.
53
* @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
54
* @param int $stackPtr The position of the current token in the
55
* stack passed in $tokens.
59
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61
$tokens = $phpcsFile->getTokens();
63
// No bracket can have space before them.
64
$prevType = $tokens[($stackPtr - 1)]['code'];
65
if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
66
$nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true);
67
$expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content'];
68
$found = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content'];
69
$error = "Space found before square bracket; expected \"$expected\" but found \"$found\"";
70
$phpcsFile->addError($error, $stackPtr);
73
if ($tokens[$stackPtr]['type'] === 'T_OPEN_SQUARE_BRACKET') {
74
// Open brackets can't have spaces on after them either.
75
$nextType = $tokens[($stackPtr + 1)]['code'];
76
if (in_array($nextType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
77
$nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 2), null, true);
78
$expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content'];
79
$found = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1));
80
$error = "Space found after square bracket; expected \"$expected\" but found \"$found\"";
81
$phpcsFile->addError($error, $stackPtr);