|
| 1 | +<?php |
| 2 | + |
| 3 | +if (class_exists('PHP_CodeSniffer_Standards_AbstractVariableSniff', true) === false) { |
| 4 | + throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractVariableSniff not found'); |
| 5 | +} |
| 6 | + |
| 7 | +/** |
| 8 | + * Parses and verifies the variable doc comment. |
| 9 | + */ |
| 10 | +class Symfony3Custom_Sniffs_Commenting_VariableCommentSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Called to process class member vars. |
| 14 | + * |
| 15 | + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
| 16 | + * @param int $stackPtr The position of the current token |
| 17 | + * in the stack passed in $tokens. |
| 18 | + * |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 22 | + { |
| 23 | + $tokens = $phpcsFile->getTokens(); |
| 24 | + $ignore = array( |
| 25 | + T_PUBLIC, |
| 26 | + T_PRIVATE, |
| 27 | + T_PROTECTED, |
| 28 | + T_VAR, |
| 29 | + T_STATIC, |
| 30 | + T_WHITESPACE, |
| 31 | + ); |
| 32 | + |
| 33 | + $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true); |
| 34 | + if ($commentEnd === false |
| 35 | + || ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
| 36 | + && $tokens[$commentEnd]['code'] !== T_COMMENT) |
| 37 | + ) { |
| 38 | + $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing'); |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
| 44 | + $phpcsFile->addError( |
| 45 | + 'You must use "/**" style comments for a member variable comment', |
| 46 | + $stackPtr, |
| 47 | + 'WrongStyle' |
| 48 | + ); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
| 54 | + |
| 55 | + $foundVar = null; |
| 56 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
| 57 | + if ($tokens[$tag]['content'] === '@var') { |
| 58 | + if ($foundVar !== null) { |
| 59 | + $error = 'Only one @var tag is allowed in a member variable comment'; |
| 60 | + $phpcsFile->addError($error, $tag, 'DuplicateVar'); |
| 61 | + } else { |
| 62 | + $foundVar = $tag; |
| 63 | + } |
| 64 | + } elseif ($tokens[$tag]['content'] === '@see') { |
| 65 | + // Make sure the tag isn't empty. |
| 66 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
| 67 | + if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
| 68 | + $error = 'Content missing for @see tag in member variable comment'; |
| 69 | + $phpcsFile->addError($error, $tag, 'EmptySees'); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // The @var tag is the only one we require. |
| 75 | + if ($foundVar === null) { |
| 76 | + $error = 'Missing @var tag in member variable comment'; |
| 77 | + $phpcsFile->addError($error, $commentEnd, 'MissingVar'); |
| 78 | + |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $firstTag = $tokens[$commentStart]['comment_tags'][0]; |
| 83 | + if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') { |
| 84 | + $error = 'The @var tag must be the first tag in a member variable comment'; |
| 85 | + $phpcsFile->addError($error, $foundVar, 'VarOrder'); |
| 86 | + } |
| 87 | + |
| 88 | + // Make sure the tag isn't empty and has the correct padding. |
| 89 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd); |
| 90 | + if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) { |
| 91 | + $error = 'Content missing for @var tag in member variable comment'; |
| 92 | + $phpcsFile->addError($error, $foundVar, 'EmptyVar'); |
| 93 | + |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + $this->processWhitespace($phpcsFile, $commentStart); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param PHP_CodeSniffer_File $phpcsFile |
| 102 | + * @param int $commentStart |
| 103 | + */ |
| 104 | + protected function processWhitespace( |
| 105 | + PHP_CodeSniffer_File $phpcsFile, |
| 106 | + $commentStart |
| 107 | + ) { |
| 108 | + $tokens = $phpcsFile->getTokens(); |
| 109 | + $before = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true); |
| 110 | + |
| 111 | + $startLine = $tokens[$commentStart]['line']; |
| 112 | + $prevLine = $tokens[$before]['line']; |
| 113 | + |
| 114 | + $found = $startLine - $prevLine - 1; |
| 115 | + |
| 116 | + // Skip for class opening |
| 117 | + if ($found < 1 && !($found === 0 && $tokens[$before]['type'] === 'T_OPEN_CURLY_BRACKET')) { |
| 118 | + if ($found < 0) { |
| 119 | + $found = 0; |
| 120 | + } |
| 121 | + |
| 122 | + $error = 'Expected 1 blank line before docblock; %s found'; |
| 123 | + $rule = 'SpacingBeforeDocblock'; |
| 124 | + |
| 125 | + $data = array($found); |
| 126 | + $fix = $phpcsFile->addFixableError($error, $commentStart, $rule, $data); |
| 127 | + |
| 128 | + if ($fix === true) { |
| 129 | + if ($found > 1) { |
| 130 | + $phpcsFile->fixer->beginChangeset(); |
| 131 | + |
| 132 | + for ($i = ($before + 1); $i < ($commentStart - 1); $i++) { |
| 133 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 134 | + } |
| 135 | + |
| 136 | + $phpcsFile->fixer->endChangeset(); |
| 137 | + } else { |
| 138 | + // Try and maintain indentation. |
| 139 | + if ($tokens[($commentStart - 1)]['code'] === T_WHITESPACE) { |
| 140 | + $phpcsFile->fixer->addNewlineBefore($commentStart - 1); |
| 141 | + } else { |
| 142 | + $phpcsFile->fixer->addNewlineBefore($commentStart); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Called to process a normal variable. |
| 151 | + * |
| 152 | + * Not required for this sniff. |
| 153 | + * |
| 154 | + * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found. |
| 155 | + * @param int $stackPtr The position where the double quoted |
| 156 | + * string was found. |
| 157 | + * |
| 158 | + * @return void |
| 159 | + */ |
| 160 | + protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 161 | + { |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Called to process variables found in double quoted strings. |
| 166 | + * |
| 167 | + * Not required for this sniff. |
| 168 | + * |
| 169 | + * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found. |
| 170 | + * @param int $stackPtr The position where the double quoted |
| 171 | + * string was found. |
| 172 | + * |
| 173 | + * @return void |
| 174 | + */ |
| 175 | + protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
| 176 | + { |
| 177 | + } |
| 178 | +} |
0 commit comments