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