comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php @ 17:129ea1e6d783

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