3
* Class Declaration Test.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Class Declaration Test.
18
* Checks the declaration of the class is correct.
21
* @package PHP_CodeSniffer
22
* @author Greg Sherwood <gsherwood@squiz.net>
23
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
* @version Release: 1.5.3
26
* @link http://pear.php.net/package/PHP_CodeSniffer
28
class PSR1_Sniffs_Classes_ClassDeclarationSniff implements PHP_CodeSniffer_Sniff
33
* Returns an array of tokens this test wants to listen for.
37
public function register()
49
* Processes this test, when one of its tokens is encountered.
51
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
* @param integer $stackPtr The position of the current token in
57
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
59
$tokens = $phpcsFile->getTokens();
60
$errorData = array(strtolower($tokens[$stackPtr]['content']));
62
$nextClass = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE, T_TRAIT), ($stackPtr + 1));
63
if ($nextClass !== false) {
64
$error = 'Each %s must be in a file by itself';
65
$phpcsFile->addError($error, $nextClass, 'MultipleClasses', $errorData);
68
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
69
$namespace = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
70
if ($namespace === false) {
71
$error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
72
$phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $errorData);