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