Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Verifies that properties are declared correctly.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Laregely copied from PSR2_Sniffs_Classes_PropertyDeclarationSniff to have a fixer
|
Chris@0
|
12 * for the var keyword.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @category PHP
|
Chris@0
|
15 * @package PHP_CodeSniffer
|
Chris@0
|
16 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
17 */
|
Chris@0
|
18 class Drupal_Sniffs_Classes_PropertyDeclarationSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff
|
Chris@0
|
19 {
|
Chris@0
|
20
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Processes the function tokens within the class.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
|
Chris@0
|
26 * @param int $stackPtr The position where the token was found.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @return void
|
Chris@0
|
29 */
|
Chris@0
|
30 protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
31 {
|
Chris@0
|
32 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
33
|
Chris@0
|
34 if ($tokens[$stackPtr]['content'][1] === '_') {
|
Chris@0
|
35 $error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility';
|
Chris@0
|
36 $data = array($tokens[$stackPtr]['content']);
|
Chris@0
|
37 $phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data);
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 // Detect multiple properties defined at the same time. Throw an error
|
Chris@0
|
41 // for this, but also only process the first property in the list so we don't
|
Chris@0
|
42 // repeat errors.
|
Chris@0
|
43 $find = PHP_CodeSniffer_Tokens::$scopeModifiers;
|
Chris@0
|
44 $find = array_merge($find, array(T_VARIABLE, T_VAR, T_SEMICOLON));
|
Chris@0
|
45 $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1));
|
Chris@0
|
46 if ($tokens[$prev]['code'] === T_VARIABLE) {
|
Chris@0
|
47 return;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 if ($tokens[$prev]['code'] === T_VAR) {
|
Chris@0
|
51 $error = 'The var keyword must not be used to declare a property';
|
Chris@0
|
52 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'VarUsed');
|
Chris@0
|
53 if ($fix === true) {
|
Chris@0
|
54 $phpcsFile->fixer->replaceToken($prev, 'public');
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 $next = $phpcsFile->findNext(array(T_VARIABLE, T_SEMICOLON), ($stackPtr + 1));
|
Chris@0
|
59 if ($tokens[$next]['code'] === T_VARIABLE) {
|
Chris@0
|
60 $error = 'There must not be more than one property declared per statement';
|
Chris@0
|
61 $phpcsFile->addError($error, $stackPtr, 'Multiple');
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr);
|
Chris@0
|
65 if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) {
|
Chris@0
|
66 $error = 'Visibility must be declared on property "%s"';
|
Chris@0
|
67 $data = array($tokens[$stackPtr]['content']);
|
Chris@0
|
68 $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 }//end processMemberVar()
|
Chris@0
|
72
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Processes normal variables.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
|
Chris@0
|
78 * @param int $stackPtr The position where the token was found.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @return void
|
Chris@0
|
81 */
|
Chris@0
|
82 protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
83 {
|
Chris@0
|
84 /*
|
Chris@0
|
85 We don't care about normal variables.
|
Chris@0
|
86 */
|
Chris@0
|
87
|
Chris@0
|
88 }//end processVariable()
|
Chris@0
|
89
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Processes variables in double quoted strings.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
|
Chris@0
|
95 * @param int $stackPtr The position where the token was found.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return void
|
Chris@0
|
98 */
|
Chris@0
|
99 protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
Chris@0
|
100 {
|
Chris@0
|
101 /*
|
Chris@0
|
102 We don't care about normal variables.
|
Chris@0
|
103 */
|
Chris@0
|
104
|
Chris@0
|
105 }//end processVariableInString()
|
Chris@0
|
106
|
Chris@0
|
107
|
Chris@0
|
108 }//end class
|