~ubuntu-branches/ubuntu/saucy/php-codesniffer/saucy

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jan Wagner
  • Date: 2008-03-21 23:29:33 UTC
  • Revision ID: james.westby@ubuntu.com-20080321232933-za8kvi1bgvrvud6z
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

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 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
 
15
 */
 
16
 
 
17
/**
 
18
 * Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff.
 
19
 *
 
20
 * Ensure that there are no spaces around square brackets.
 
21
 *
 
22
 * @category  PHP
 
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
 
30
 */
 
31
class Squiz_Sniffs_Arrays_ArrayBracketSpacingSniff implements PHP_CodeSniffer_Sniff
 
32
{
 
33
 
 
34
 
 
35
    /**
 
36
     * Returns an array of tokens this test wants to listen for.
 
37
     *
 
38
     * @return array
 
39
     */
 
40
    public function register()
 
41
    {
 
42
        return array(
 
43
                T_OPEN_SQUARE_BRACKET,
 
44
                T_CLOSE_SQUARE_BRACKET,
 
45
               );
 
46
 
 
47
    }//end register()
 
48
 
 
49
 
 
50
    /**
 
51
     * Processes this sniff, when one of its tokens is encountered.
 
52
     *
 
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.
 
56
     *
 
57
     * @return void
 
58
     */
 
59
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
60
    {
 
61
        $tokens = $phpcsFile->getTokens();
 
62
 
 
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);
 
71
        }
 
72
 
 
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);
 
82
            }
 
83
        }
 
84
 
 
85
    }//end process()
 
86
 
 
87
 
 
88
}//end class
 
89
 
 
90
?>