Source for file ValidClassNameSniff.php
Documentation is available at ValidClassNameSniff.php
* Ensures class and interface names start with a capital letter and use _ separators.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
class ValidClassNameSniff implements Sniff
* Returns an array of tokens this test wants to listen for.
public function register ()
* Processes this test, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$className = $phpcsFile->findNext (T_STRING , $stackPtr);
$name = trim($tokens[$className]['content']);
$errorData = array (ucfirst($tokens[$stackPtr]['content']));
// Make sure the first letter is a capital.
$error = '%s name must begin with a capital letter';
$phpcsFile->addError ($error, $stackPtr, 'StartWithCapital', $errorData);
// Check that each new word starts with a capital as well, but don't
// check the first word, as it is checked above.
foreach ($nameBits as $bit) {
if ($bit === '' || $bit{0 } !== strtoupper($bit{0 })) {
if ($validName === false ) {
// Strip underscores because they cause the suggested name
$error = '%s name is not valid';
$phpcsFile->addError ($error, $stackPtr, 'Invalid', $errorData);
foreach ($nameBits as $bit) {
$newName = rtrim($newName, '_');
$error = '%s name is not valid; consider %s instead';
$phpcsFile->addError ($error, $stackPtr, 'Invalid', $data);
Documentation generated on Mon, 11 Mar 2019 15:27:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|