1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Thomas Ernest <[email protected]> |
10
|
|
|
* @copyright 2010 Thomas Ernest |
11
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidClassNameSniff. |
17
|
|
|
* |
18
|
|
|
* Ensures that class and interface names have their first letter uppercase |
19
|
|
|
* and that words are separated with an underscore, and not CamelCased. |
20
|
|
|
* |
21
|
|
|
* @todo Try to avoid overly long and verbose names in using property rule and |
22
|
|
|
* configuration variable to set limits. Have a look at |
23
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff. |
24
|
|
|
* |
25
|
|
|
* @category PHP |
26
|
|
|
* @package PHP_CodeSniffer |
27
|
|
|
* @author Thomas Ernest <[email protected]> |
28
|
|
|
* @copyright 2010 Thomas Ernest |
29
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
30
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace CodeIgniter\Sniffs\NamingConventions; |
34
|
|
|
|
35
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
36
|
|
|
use PHP_CodeSniffer\Files\File; |
37
|
|
|
|
38
|
|
|
class ValidClassNameSniff implements Sniff |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns an array of tokens this test wants to listen for. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function register() |
48
|
|
|
{ |
49
|
|
|
return array( |
50
|
|
|
T_CLASS, |
51
|
|
|
T_INTERFACE, |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
}//end register() |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Processes this test, when one of its tokens is encountered. |
58
|
|
|
* |
59
|
|
|
* @param File $phpcsFile The current file being processed. |
60
|
|
|
* @param int $stackPtr The position of the current token |
61
|
|
|
* in the stack passed in $tokens. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function process(File $phpcsFile, $stackPtr) |
66
|
|
|
{ |
67
|
|
|
// get the class name |
68
|
|
|
$className = trim($phpcsFile->getDeclarationName($stackPtr)); |
69
|
|
|
// compute the expected class name |
70
|
|
|
// [^_] means "something different from _", but not "nothing or something different from _" |
|
|
|
|
71
|
|
|
$lcClassNameChunk = preg_replace('/([^_])([A-Z])/', '${1}_${2}', $className); |
72
|
|
|
$expectedClassName |
73
|
|
|
= strtoupper($className[0]) . strtolower(substr($lcClassNameChunk,1)); |
74
|
|
|
// ensures that the current class name |
75
|
|
|
// and the expected class name are identical |
76
|
|
|
if (0 !== strcmp($className, $expectedClassName)) { |
77
|
|
|
$error = 'Class names should always have their first letter uppercase. Multiple words should be separated with an underscore, and not CamelCased. Please consider ' . $expectedClassName . ' instead of ' . $className . '.'; |
|
|
|
|
78
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
}//end process() |
81
|
|
|
|
82
|
|
|
}//end class |
83
|
|
|
|
84
|
|
|
?> |
|
|
|
|
85
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.