1<?php
2/**
3 * Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @author    Marc McIntyre <mmcintyre@squiz.net>
11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13 * @link      http://pear.php.net/package/PHP_CodeSniffer
14 */
15
16/**
17 * Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
18 *
19 * Ensure that there are no spaces around square brackets.
20 *
21 * @category  PHP
22 * @package   PHP_CodeSniffer
23 * @author    Greg Sherwood <gsherwood@squiz.net>
24 * @author    Marc McIntyre <mmcintyre@squiz.net>
25 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27 * @version   Release: @package_version@
28 * @link      http://pear.php.net/package/PHP_CodeSniffer
29 */
30class Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff implements PHP_CodeSniffer_Sniff
31{
32
33
34    /**
35     * Returns an array of tokens this test wants to listen for.
36     *
37     * @return array
38     */
39    public function register()
40    {
41        return array(
42                T_OPEN_SQUARE_BRACKET,
43                T_CLOSE_SQUARE_BRACKET,
44               );
45
46    }//end register()
47
48
49    /**
50     * Processes this sniff, when one of its tokens is encountered.
51     *
52     * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
53     * @param int                  $stackPtr  The position of the current token in the
54     *                                        stack passed in $tokens.
55     *
56     * @return void
57     */
58    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
59    {
60        $tokens = $phpcsFile->getTokens();
61
62        // PHP 5.4 introduced a shorthand array declaration syntax, so we need
63        // to ignore the these type of array declarations because this sniff is
64        // only dealing with array usage.
65        if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) {
66            $openBracket = $stackPtr;
67        } else {
68            if (isset($tokens[$stackPtr]['bracket_opener']) === false) {
69                return;
70            }
71
72            $openBracket = $tokens[$stackPtr]['bracket_opener'];
73        }
74
75        $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket - 1), null, true);
76        if ($tokens[$prev]['code'] === T_EQUAL) {
77            return;
78        }
79
80        // Square brackets can not have a space before them.
81        $prevType = $tokens[($stackPtr - 1)]['code'];
82        if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$prevType]) === true) {
83            $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true);
84            $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content'];
85            $found    = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content'];
86            $error    = 'Space found before square bracket; expected "%s" but found "%s"';
87            $data     = array(
88                         $expected,
89                         $found,
90                        );
91            $fix      = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeBracket', $data);
92            if ($fix === true) {
93                $phpcsFile->fixer->replaceToken(($stackPtr - 1), '');
94            }
95        }
96
97        // Open square brackets can't ever have spaces after them.
98        if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) {
99            $nextType = $tokens[($stackPtr + 1)]['code'];
100            if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$nextType]) === true) {
101                $nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 2), null, true);
102                $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content'];
103                $found    = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1));
104                $error    = 'Space found after square bracket; expected "%s" but found "%s"';
105                $data     = array(
106                             $expected,
107                             $found,
108                            );
109                $fix      = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterBracket', $data);
110                if ($fix === true) {
111                    $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
112                }
113            }
114        }
115
116    }//end process()
117
118
119}//end class
120