1<?php
2/**
3 * Ensures that object indexes are written in dot notation.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer_MySource
9 * @author    Sertan Danis <sdanis@squiz.net>
10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Ensures that object indexes are written in dot notation.
17 *
18 * @category  PHP
19 * @package   PHP_CodeSniffer
20 * @author    Sertan Danis <sdanis@squiz.net>
21 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
22 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23 * @version   Release: @package_version@
24 * @link      http://pear.php.net/package/PHP_CodeSniffer
25 */
26class Squiz_Sniffs_Objects_DisallowObjectStringIndexSniff implements PHP_CodeSniffer_Sniff
27{
28
29    /**
30     * A list of tokenizers this sniff supports.
31     *
32     * @var array
33     */
34    public $supportedTokenizers = array('JS');
35
36
37    /**
38     * Returns an array of tokens this test wants to listen for.
39     *
40     * @return array
41     */
42    public function register()
43    {
44        return array(T_OPEN_SQUARE_BRACKET);
45
46    }//end register()
47
48
49    /**
50     * Processes this test, when one of its tokens is encountered.
51     *
52     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
53     * @param int                  $stackPtr  The position of the current token
54     *                                        in the 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        // Check if the next non whitespace token is a string.
63        $index = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
64        if ($tokens[$index]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
65            return;
66        }
67
68        // Make sure it is the only thing in the square brackets.
69        $next = $phpcsFile->findNext(T_WHITESPACE, ($index + 1), null, true);
70        if ($tokens[$next]['code'] !== T_CLOSE_SQUARE_BRACKET) {
71            return;
72        }
73
74        // Allow indexes that have dots in them because we can't write
75        // them in dot notation.
76        $content = trim($tokens[$index]['content'], '"\' ');
77        if (strpos($content, '.') !== false) {
78            return;
79        }
80
81        // Also ignore reserved words.
82        if ($content === 'super') {
83            return;
84        }
85
86        // Token before the opening square bracket cannot be a var name.
87        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
88        if ($tokens[$prev]['code'] === T_STRING) {
89            $error = 'Object indexes must be written in dot notation';
90            $phpcsFile->addError($error, $prev, 'Found');
91        }
92
93    }//end process()
94
95
96}//end class
97