Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Squiz_Sniffs_CSS_DuplicateClassDefinitionSniff.
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: DuplicateClassDefinitionSniff.php 288184 2009-09-09 05:00:01Z squiz $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Squiz_Sniffs_CSS_DuplicateClassDefinitionSniff.
18
 *
19
 * Check for duplicate class definitions that can be merged into one.
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_CSS_DuplicateClassDefinitionSniff implements PHP_CodeSniffer_Sniff
30
{
31
 
32
    /**
33
     * A list of tokenizers this sniff supports.
34
     *
35
     * @var array
36
     */
37
    public $supportedTokenizers = array('CSS');
38
 
39
 
40
    /**
41
     * Returns the token types that this sniff is interested in.
42
     *
43
     * @return array(int)
44
     */
45
    public function register()
46
    {
47
        return array(T_OPEN_TAG);
48
 
49
    }//end register()
50
 
51
 
52
    /**
53
     * Processes the tokens that this sniff is interested in.
54
     *
55
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
56
     * @param int                  $stackPtr  The position in the stack where
57
     *                                        the token was found.
58
     *
59
     * @return void
60
     */
61
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
62
    {
63
        $tokens = $phpcsFile->getTokens();
64
 
65
        // Find the content of each class definition name.
66
        $classNames = array();
67
        $next = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1));
68
        if ($next === false) {
69
            // No class definitions in the file.
70
            return;
71
        }
72
 
73
        $find = array(
74
                 T_CLOSE_CURLY_BRACKET,
75
                 T_COMMENT,
76
                 T_OPEN_TAG,
77
                );
78
 
79
        while ($next !== false) {
80
            $prev = $phpcsFile->findPrevious($find, ($next - 1));
81
 
82
            // Create a sorted name for the class so we can compare classes
83
            // even when the individual names are all over the place.
84
            $name = '';
85
            for ($i = ($prev + 1); $i < $next; $i++) {
86
                $name .= $tokens[$i]['content'];
87
            }
88
 
89
            $name = trim($name);
90
            $name = str_replace("\n", ' ', $name);
91
            $name = preg_replace('|[\s]+|', ' ', $name);
92
            $name = str_replace(', ', ',', $name);
93
 
94
            $names = explode(',', $name);
95
            sort($names);
96
            $name = implode(',', $names);
97
 
98
            if (isset($classNames[$name]) === true) {
99
                $first = $classNames[$name];
100
                $line  = $tokens[$first]['line'];
101
                $error = "Duplicate class definition found; first defined on line $line";
102
                $phpcsFile->addError($error, $next);
103
            } else {
104
                $classNames[$name] = $next;
105
            }
106
 
107
            $next = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($next + 1));
108
        }//end while
109
 
110
    }//end process()
111
 
112
 
113
}//end class
114
?>