5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Reports errors if the same class or interface name is used in multiple files.
|
|
|
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: DuplicateClassNameSniff.php,v 1.1 2008/07/25 04:24:10 squiz Exp $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Reports errors if the same class or interface name is used in multiple files.
|
|
|
18 |
*
|
|
|
19 |
* @category PHP
|
|
|
20 |
* @package PHP_CodeSniffer
|
|
|
21 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
22 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
23 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
24 |
* @version Release: 1.2.0RC1
|
|
|
25 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
26 |
*/
|
|
|
27 |
class Generic_Sniffs_Classes_DuplicateClassNameSniff implements PHP_CodeSniffer_MultiFileSniff
|
|
|
28 |
{
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Called once per script run to allow for processing of this sniff.
|
|
|
33 |
*
|
|
|
34 |
* @param array(PHP_CodeSniffer_File) $files The PHP_CodeSniffer files processed
|
|
|
35 |
* during the script run.
|
|
|
36 |
*
|
|
|
37 |
* @return void
|
|
|
38 |
*/
|
|
|
39 |
public function process(array $files)
|
|
|
40 |
{
|
|
|
41 |
$foundClasses = array();
|
|
|
42 |
|
|
|
43 |
foreach ($files as $phpcsFile) {
|
|
|
44 |
$tokens = $phpcsFile->getTokens();
|
|
|
45 |
|
|
|
46 |
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), 0);
|
|
|
47 |
while ($stackPtr !== false) {
|
|
|
48 |
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr);
|
|
|
49 |
$name = $tokens[$nameToken]['content'];
|
|
|
50 |
$compareName = strtolower($name);
|
|
|
51 |
if (isset($foundClasses[$compareName]) === true) {
|
|
|
52 |
$type = strtolower($tokens[$stackPtr]['content']);
|
|
|
53 |
$file = $foundClasses[$compareName]['file'];
|
|
|
54 |
$line = $foundClasses[$compareName]['line'];
|
|
|
55 |
$error = "Duplicate $type name \"$name\" found; first defined in $file on line $line";
|
|
|
56 |
$phpcsFile->addWarning($error, $stackPtr);
|
|
|
57 |
} else {
|
|
|
58 |
$foundClasses[$compareName] = array(
|
|
|
59 |
'file' => $phpcsFile->getFilename(),
|
|
|
60 |
'line' => $tokens[$stackPtr]['line'],
|
|
|
61 |
);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1));
|
|
|
65 |
}//end while
|
|
|
66 |
|
|
|
67 |
}//end foreach
|
|
|
68 |
|
|
|
69 |
}//end process()
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
}//end class
|
|
|
73 |
|
|
|
74 |
?>
|