1<?php
2/**
3 * Squiz_Sniffs_Classes_DuplicatePropertySniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@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 * Squiz_Sniffs_Classes_DuplicatePropertySniff.
17 *
18 * Ensures JS classes don't contain duplicate property names.
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version   Release: @package_version@
26 * @link      http://pear.php.net/package/PHP_CodeSniffer
27 */
28class Squiz_Sniffs_Classes_DuplicatePropertySniff implements PHP_CodeSniffer_Sniff
29{
30
31    /**
32     * A list of tokenizers this sniff supports.
33     *
34     * @var array
35     */
36    public $supportedTokenizers = array('JS');
37
38
39    /**
40     * Returns an array of tokens this test wants to listen for.
41     *
42     * @return array
43     */
44    public function register()
45    {
46        return array(T_OBJECT);
47
48    }//end register()
49
50
51    /**
52     * Processes this test, when one of its tokens is encountered.
53     *
54     * @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
55     * @param int                  $stackPtr  The position of the current token in the
56     *                                        stack passed in $tokens.
57     *
58     * @return void
59     */
60    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61    {
62        $tokens = $phpcsFile->getTokens();
63
64        $properties   = array();
65        $wantedTokens = array(
66                         T_PROPERTY,
67                         T_OBJECT,
68                        );
69
70        $next = $phpcsFile->findNext($wantedTokens, ($stackPtr + 1), $tokens[$stackPtr]['bracket_closer']);
71        while ($next !== false && $next < $tokens[$stackPtr]['bracket_closer']) {
72            if ($tokens[$next]['code'] === T_OBJECT) {
73                // Skip nested objects.
74                $next = $tokens[$next]['bracket_closer'];
75            } else {
76                $propName = $tokens[$next]['content'];
77                if (isset($properties[$propName]) === true) {
78                    $error = 'Duplicate property definition found for "%s"; previously defined on line %s';
79                    $data  = array(
80                              $propName,
81                              $tokens[$properties[$propName]]['line'],
82                             );
83                    $phpcsFile->addError($error, $next, 'Found', $data);
84                }
85
86                $properties[$propName] = $next;
87            }//end if
88
89            $next = $phpcsFile->findNext($wantedTokens, ($next + 1), $tokens[$stackPtr]['bracket_closer']);
90        }//end while
91
92    }//end process()
93
94
95}//end class
96