3
* Verifies that properties are declared correctly.
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
15
if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) {
16
throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found');
20
* Verifies that properties are declared correctly.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27
* @version Release: 1.5.4
28
* @link http://pear.php.net/package/PHP_CodeSniffer
30
class PSR2_Sniffs_Classes_PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
35
* Processes the function tokens within the class.
37
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
38
* @param int $stackPtr The position where the token was found.
42
protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
44
$tokens = $phpcsFile->getTokens();
46
if ($tokens[$stackPtr]['content'][1] === '_') {
47
$error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
48
$data = array($tokens[$stackPtr]['content']);
49
$phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
52
// Detect multiple properties defined at the same time. Throw an error
53
// for this, but also only process the first property in the list so we don't
55
$find = PHP_CodeSniffer_Tokens::$scopeModifiers;
56
$find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
57
$prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
58
if ($tokens[$prev]['code'] === T_VARIABLE) {
62
if ($tokens[$prev]['code'] === T_VAR) {
63
$error = 'The var keyword must not be used to declare a property';
64
$phpcsFile->addError($error, $stackPtr, 'VarUsed');
67
$next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), ($stackPtr + 1));
68
if ($tokens[$next]['code'] === T_VARIABLE) {
69
$error = 'There must not be more than one property declared per statement';
70
$phpcsFile->addError($error, $stackPtr, 'Multiple');
73
$modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
74
if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
75
$error = 'Visibility must be declared on property "%s"';
76
$data = array($tokens[$stackPtr]['content']);
77
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
80
}//end processMemberVar()
84
* Processes normal variables.
86
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
87
* @param int $stackPtr The position where the token was found.
91
protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
93
// We don't care about normal variables.
95
}//end processVariable()
99
* Processes variables in double quoted strings.
101
* @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
102
* @param int $stackPtr The position where the token was found.
106
protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
108
// We don't care about normal variables.
110
}//end processVariableInString()