~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.0RC2/CodeSniffer/Standards/Squiz/Sniffs/Arrays/ArrayBracketSpacingSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-07-12 15:16:25 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130712151625-4autdc0twzbueha9
Tags: 1.5.0~rc2-1
* New upstream release.
* Refreshed patch.
* Standards-Version is now 3.9.4.
* Canonical VCS URLs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2012 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-2012 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: 1.5.0RC2
 
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
29
 */
 
30
class 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
            $openBracket = $tokens[$stackPtr]['bracket_opener'];
 
69
        }
 
70
 
 
71
        $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($openBracket - 1), null, true);
 
72
        if ($tokens[$prev]['code'] === T_EQUAL) {
 
73
            return;
 
74
        }
 
75
 
 
76
        // Square brackets can not have a space before them.
 
77
        $prevType = $tokens[($stackPtr - 1)]['code'];
 
78
        if (in_array($prevType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
 
79
            $nonSpace = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 2), null, true);
 
80
            $expected = $tokens[$nonSpace]['content'].$tokens[$stackPtr]['content'];
 
81
            $found    = $phpcsFile->getTokensAsString($nonSpace, ($stackPtr - $nonSpace)).$tokens[$stackPtr]['content'];
 
82
            $error    = 'Space found before square bracket; expected "%s" but found "%s"';
 
83
            $data     = array(
 
84
                         $expected,
 
85
                         $found,
 
86
                        );
 
87
            $phpcsFile->addError($error, $stackPtr, 'SpaceBeforeBracket', $data);
 
88
        }
 
89
 
 
90
        // Open square brackets can't ever have spaces after them.
 
91
        if ($tokens[$stackPtr]['code'] === T_OPEN_SQUARE_BRACKET) {
 
92
            $nextType = $tokens[($stackPtr + 1)]['code'];
 
93
            if (in_array($nextType, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
 
94
                $nonSpace = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 2), null, true);
 
95
                $expected = $tokens[$stackPtr]['content'].$tokens[$nonSpace]['content'];
 
96
                $found    = $phpcsFile->getTokensAsString($stackPtr, ($nonSpace - $stackPtr + 1));
 
97
                $error    = 'Space found after square bracket; expected "%s" but found "%s"';
 
98
                $data     = array(
 
99
                             $expected,
 
100
                             $found,
 
101
                            );
 
102
                $phpcsFile->addError($error, $stackPtr, 'SpaceAfterBracket', $data);
 
103
            }
 
104
        }
 
105
 
 
106
    }//end process()
 
107
 
 
108
 
 
109
}//end class
 
110
 
 
111
?>