3
* PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
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
18
* PEAR_Sniffs_NamingConventions_ValidClassNameSniff.
20
* Ensures class and interface names start with a capital letter
21
* and use _ separators.
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.1.0
30
* @link http://pear.php.net/package/PHP_CodeSniffer
32
class PEAR_Sniffs_NamingConventions_ValidClassNameSniff implements PHP_CodeSniffer_Sniff
37
* Returns an array of tokens this test wants to listen for.
41
public function register()
52
* Processes this test, when one of its tokens is encountered.
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.
60
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
62
$tokens = $phpcsFile->getTokens();
64
$className = $phpcsFile->findNext(T_STRING, $stackPtr);
65
$name = trim($tokens[$className]['content']);
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);
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.
76
$nameBits = explode('_', $name);
77
$firstBit = array_shift($nameBits);
78
foreach ($nameBits as $bit) {
79
if ($bit === '' || $bit{0} !== strtoupper($bit{0})) {
85
if ($validName !== true) {
86
// Strip underscores because they cause the suggested name
88
$nameBits = explode('_', trim($name, '_'));
89
$firstBit = array_shift($nameBits);
90
if ($firstBit === '') {
91
$error = ucfirst($tokens[$stackPtr]['content']).' name is not valid';
93
$newName = strtoupper($firstBit{0}).substr($firstBit, 1).'_';
94
foreach ($nameBits as $bit) {
96
$newName .= strtoupper($bit{0}).substr($bit, 1).'_';
100
$newName = rtrim($newName, '_');
101
$error = ucfirst($tokens[$stackPtr]['content'])." name is not valid; consider $newName instead";
104
$phpcsFile->addError($error, $stackPtr);