|
12 | 12 |
|
13 | 13 | use PHP_CodeSniffer\Files\File;
|
14 | 14 | use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as Sniff;
|
| 15 | +use PHP_CodeSniffer\Util\Tokens; |
15 | 16 |
|
16 | 17 | /**
|
17 | 18 | * Symfony standard customization to PEARs FunctionCommentSniff.
|
|
25 | 26 | */
|
26 | 27 | class FunctionCommentSniff extends Sniff
|
27 | 28 | {
|
| 29 | + /** |
| 30 | + * @inheritDoc |
| 31 | + */ |
| 32 | + public function process(File $phpcsFile, $stackPtr) |
| 33 | + { |
| 34 | + $tokens = $phpcsFile->getTokens(); |
| 35 | + $find = Tokens::$methodPrefixes; |
| 36 | + $find[] = T_WHITESPACE; |
| 37 | + |
| 38 | + $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
| 39 | + if ($tokens[$commentEnd]['code'] === T_COMMENT) { |
| 40 | + // Inline comments might just be closing comments for |
| 41 | + // control structures or functions instead of function comments |
| 42 | + // using the wrong comment type. If there is other code on the line, |
| 43 | + // assume they relate to that code. |
| 44 | + $prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true); |
| 45 | + if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
| 46 | + $commentEnd = $prev; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + $properties = $phpcsFile->getMethodProperties($stackPtr); |
| 51 | + |
| 52 | + if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
| 53 | + && $tokens[$commentEnd]['code'] !== T_COMMENT |
| 54 | + ) { |
| 55 | + $hasComment = false; |
| 56 | + $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no'); |
| 57 | + if ($properties['scope'] !== 'private') { |
| 58 | + $function = $phpcsFile->getDeclarationName($stackPtr); |
| 59 | + $phpcsFile->addError( |
| 60 | + 'Missing doc comment for function %s()', |
| 61 | + $stackPtr, |
| 62 | + 'Missing', |
| 63 | + [$function] |
| 64 | + ); |
| 65 | + return; |
| 66 | + } |
| 67 | + } else { |
| 68 | + $hasComment = true; |
| 69 | + $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes'); |
| 70 | + } |
| 71 | + |
| 72 | + if ($hasComment && $tokens[$commentEnd]['code'] === T_COMMENT) { |
| 73 | + $phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle'); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if ($hasComment && $tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { |
| 78 | + $error = 'There must be no blank lines after the function comment'; |
| 79 | + $phpcsFile->addError($error, $commentEnd, 'SpacingAfter'); |
| 80 | + } |
| 81 | + |
| 82 | + if (!$hasComment) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
| 87 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
| 88 | + if ($tokens[$tag]['content'] === '@see') { |
| 89 | + // Make sure the tag isn't empty. |
| 90 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); |
| 91 | + if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { |
| 92 | + $error = 'Content missing for @see tag in function comment'; |
| 93 | + $phpcsFile->addError($error, $tag, 'EmptySees'); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + $this->processReturn($phpcsFile, $stackPtr, $commentStart); |
| 99 | + $this->processThrows($phpcsFile, $stackPtr, $commentStart); |
| 100 | + $this->processParams($phpcsFile, $stackPtr, $commentStart); |
| 101 | + } |
| 102 | + |
28 | 103 | /**
|
29 | 104 | * Process the return comment of this function comment.
|
30 | 105 | *
|
|
0 commit comments