Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Marc McIntyre <mmcintyre@squiz.net>
11
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
 * @version   CVS: $Id: ValidClassNameSniff.php,v 1.10 2008/04/28 01:02:28 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
19
 *
20
 * Ensures class and interface names start with a capital letter
21
 * and use _ separators.
22
 *
23
 * @category  PHP
24
 * @package   PHP_CodeSniffer
25
 * @author    Greg Sherwood <gsherwood@squiz.net>
26
 * @author    Marc McIntyre <mmcintyre@squiz.net>
27
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
28
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
29
 * @version   Release: 1.2.0RC1
30
 * @link      http://pear.php.net/package/PHP_CodeSniffer
31
 */
32
class PEAR_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
33
{
34
 
35
 
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        return array(
44
                T_CLASS,
45
                T_INTERFACE,
46
               );
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
56
     *                                        in the 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
        $className = $phpcsFile->findNext(T_STRING, $stackPtr);
65
        $name      = trim($tokens[$className]['content']);
66
 
67
        // Make sure the first letter is a capital.
68
        if (preg_match('|^[A-Z]|', $name) === 0) {
69
            $error = ucfirst($tokens[$stackPtr]['content']).' name must begin with a capital letter';
70
            $phpcsFile->addError($error, $stackPtr);
71
        }
72
 
73
        // Check that each new word starts with a capital as well, but don't
74
        // check the first word, as it is checked above.
75
        $validName = true;
76
        $nameBits  = explode('_', $name);
77
        $firstBit  = array_shift($nameBits);
78
        foreach ($nameBits as $bit) {
79
            if ($bit === '' || $bit{0} !== strtoupper($bit{0})) {
80
                $validName = false;
81
                break;
82
            }
83
        }
84
 
85
        if ($validName !== true) {
86
            // Strip underscores because they cause the suggested name
87
            // to be incorrect.
88
            $nameBits = explode('_', trim($name, '_'));
89
            $firstBit = array_shift($nameBits);
90
            if ($firstBit === '') {
91
                $error = ucfirst($tokens[$stackPtr]['content']).' name is not valid';
92
            } else {
93
                $newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
94
                foreach ($nameBits as $bit) {
95
                    if ($bit !== '') {
96
                        $newName .= strtoupper($bit{0}).substr($bit, 1).'_';
97
                    }
98
                }
99
 
100
                $newName = rtrim($newName, '_');
101
                $error   = ucfirst($tokens[$stackPtr]['content'])." name is not valid; consider $newName instead";
102
            }
103
 
104
            $phpcsFile->addError($error, $stackPtr);
105
        }//end if
106
 
107
    }//end process()
108
 
109
 
110
}//end class
111
 
112
 
113
?>