Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Drupal_Sniffs_NamingConventions_ValidClassNameSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@0
|
8 * @author Marc McIntyre <mmcintyre@squiz.net>
|
Chris@0
|
9 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@0
|
10 * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
Chris@0
|
11 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
12 */
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Drupal_Sniffs_NamingConventions_ValidClassNameSniff.
|
Chris@0
|
16 *
|
Chris@0
|
17 * Ensures class and interface names start with a capital letter
|
Chris@0
|
18 * and do not use _ separators.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @category PHP
|
Chris@0
|
21 * @package PHP_CodeSniffer
|
Chris@0
|
22 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@0
|
23 * @author Marc McIntyre <mmcintyre@squiz.net>
|
Chris@0
|
24 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@0
|
25 * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
Chris@0
|
26 * @version Release: 1.2.0RC3
|
Chris@0
|
27 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
28 */
|
Chris@0
|
29 class Drupal_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
|
Chris@0
|
30 {
|
Chris@0
|
31
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @return array
|
Chris@0
|
37 */
|
Chris@0
|
38 public function register()
|
Chris@0
|
39 {
|
Chris@0
|
40 return array(
|
Chris@0
|
41 T_CLASS,
|
Chris@0
|
42 T_INTERFACE,
|
Chris@0
|
43 );
|
Chris@0
|
44
|
Chris@0
|
45 }//end register()
|
Chris@0
|
46
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
|
Chris@0
|
52 * @param int $stackPtr The position of the current token
|
Chris@0
|
53 * in the stack passed in $tokens.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @return void
|
Chris@0
|
56 */
|
Chris@0
|
57 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
58 {
|
Chris@0
|
59 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
60
|
Chris@0
|
61 $className = $phpcsFile->findNext(T_STRING, $stackPtr);
|
Chris@0
|
62 $name = trim($tokens[$className]['content']);
|
Chris@0
|
63 $errorData = array(ucfirst($tokens[$stackPtr]['content']));
|
Chris@0
|
64
|
Chris@0
|
65 // Make sure the first letter is a capital.
|
Chris@0
|
66 if (preg_match('|^[A-Z]|', $name) === 0) {
|
Chris@0
|
67 $error = '%s name must begin with a capital letter';
|
Chris@0
|
68 $phpcsFile->addError($error, $stackPtr, 'StartWithCaptial', $errorData);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 // Search for underscores.
|
Chris@0
|
72 if (strpos($name, '_') !== false) {
|
Chris@0
|
73 $error = '%s name must use UpperCamel naming without underscores';
|
Chris@0
|
74 $phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 }//end process()
|
Chris@0
|
78
|
Chris@0
|
79
|
Chris@0
|
80 }//end class
|