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