diff --git a/coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php b/coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php index 67d63e88..d67a119e 100644 --- a/coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Classes/PropertyDeclarationSniff.php @@ -1,6 +1,6 @@ */ - protected function processMemberVar(File $phpcsFile, $stackPtr) + public function register() { - $tokens = $phpcsFile->getTokens(); - - if ($tokens[$stackPtr]['content'][1] === '_') { - $error = 'Property name "%s" should not be prefixed with an underscore to indicate visibility'; - $data = [$tokens[$stackPtr]['content']]; - $phpcsFile->addWarning($error, $stackPtr, 'Underscore', $data); - } - - // Detect multiple properties defined at the same time. Throw an error - // for this, but also only process the first property in the list so we don't - // repeat errors. - $find = Tokens::$scopeModifiers; - $find = array_merge($find, [T_VARIABLE, T_VAR, T_SEMICOLON]); - $prev = $phpcsFile->findPrevious($find, ($stackPtr - 1)); - if ($tokens[$prev]['code'] === T_VARIABLE) { - return; - } - - if ($tokens[$prev]['code'] === T_VAR) { - $error = 'The var keyword must not be used to declare a property'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'VarUsed'); - if ($fix === true) { - $phpcsFile->fixer->replaceToken($prev, 'public'); - } - } + return [T_VAR]; - $next = $phpcsFile->findNext([T_VARIABLE, T_SEMICOLON], ($stackPtr + 1)); - if ($tokens[$next]['code'] === T_VARIABLE) { - $error = 'There must not be more than one property declared per statement'; - $phpcsFile->addError($error, $stackPtr, 'Multiple'); - } - - $modifier = $phpcsFile->findPrevious(Tokens::$scopeModifiers, $stackPtr); - if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) { - $error = 'Visibility must be declared on property "%s"'; - $data = [$tokens[$stackPtr]['content']]; - $phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data); - } - - }//end processMemberVar() + }//end register() /** - * Processes normal variables. + * Processes this test, when one of its tokens is encountered. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. - * @param int $stackPtr The position where the token was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in + * the stack passed in $tokens. * * @return void */ - protected function processVariable(File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { - /* - We don't care about normal variables. - */ - - }//end processVariable() - - - /** - * Processes variables in double quoted strings. - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found. - * @param int $stackPtr The position where the token was found. - * - * @return void - */ - protected function processVariableInString(File $phpcsFile, $stackPtr) - { - /* - We don't care about normal variables. - */ + $error = 'The var keyword must not be used to declare a property'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'VarUsed'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($stackPtr, 'public'); + } - }//end processVariableInString() + }//end process() }//end class diff --git a/coder_sniffer/Drupal/ruleset.xml b/coder_sniffer/Drupal/ruleset.xml index fd3a42b1..1a9fcbab 100644 --- a/coder_sniffer/Drupal/ruleset.xml +++ b/coder_sniffer/Drupal/ruleset.xml @@ -68,48 +68,52 @@ + + - - 0 - 0 - + 0 0 - + + 0 + - - + 0 - + 0 - - + 0 - + 0 - + 0 - + + 0 - + 0 - + + + + + diff --git a/tests/Drupal/Classes/PropertyDeclarationUnitTest.php b/tests/Drupal/Classes/PropertyDeclarationUnitTest.php index dc2d7bfb..23474f58 100644 --- a/tests/Drupal/Classes/PropertyDeclarationUnitTest.php +++ b/tests/Drupal/Classes/PropertyDeclarationUnitTest.php @@ -20,7 +20,7 @@ class PropertyDeclarationUnitTest extends CoderSniffUnitTest */ protected function getErrorList(string $testFile): array { - return [16 => 2]; + return [16 => 1]; }//end getErrorList()