|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SymfonyCustom\Sniffs\Commenting; |
| 4 | + |
| 5 | +use PHP_CodeSniffer\Files\File; |
| 6 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 7 | + |
| 8 | +/** |
| 9 | + * Ensures doc blocks follow basic formatting. |
| 10 | + */ |
| 11 | +class DocCommentSniff implements Sniff |
| 12 | +{ |
| 13 | + /** |
| 14 | + * A list of tokenizers this sniff supports. |
| 15 | + * |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + public $supportedTokenizers = [ |
| 19 | + 'PHP', |
| 20 | + 'JS', |
| 21 | + ]; |
| 22 | + |
| 23 | + /** |
| 24 | + * Returns an array of tokens this test wants to listen for. |
| 25 | + * |
| 26 | + * @return array |
| 27 | + */ |
| 28 | + public function register() |
| 29 | + { |
| 30 | + return [T_DOC_COMMENT_OPEN_TAG]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Processes this test, when one of its tokens is encountered. |
| 35 | + * |
| 36 | + * @param File $phpcsFile The file being scanned. |
| 37 | + * @param int $stackPtr The position of the current token in the stack passed in $tokens. |
| 38 | + * |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + public function process(File $phpcsFile, $stackPtr) |
| 42 | + { |
| 43 | + $tokens = $phpcsFile->getTokens(); |
| 44 | + |
| 45 | + if (false === isset($tokens[$stackPtr]['comment_closer']) |
| 46 | + || ('' === $tokens[$tokens[$stackPtr]['comment_closer']]['content'] |
| 47 | + && ($phpcsFile->numTokens - 1) === $tokens[$stackPtr]['comment_closer']) |
| 48 | + ) { |
| 49 | + // Don't process an unfinished comment during live coding. |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $commentEnd = $tokens[$stackPtr]['comment_closer']; |
| 54 | + |
| 55 | + $empty = [ |
| 56 | + T_DOC_COMMENT_WHITESPACE, |
| 57 | + T_DOC_COMMENT_STAR, |
| 58 | + ]; |
| 59 | + |
| 60 | + $short = $phpcsFile->findNext($empty, ($stackPtr + 1), $commentEnd, true); |
| 61 | + if (false === $short) { |
| 62 | + // No content at all. |
| 63 | + $error = 'Doc comment is empty'; |
| 64 | + $phpcsFile->addError($error, $stackPtr, 'Empty'); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $isSingleLine = $tokens[$stackPtr]['line'] === $tokens[$commentEnd]['line']; |
| 70 | + |
| 71 | + // The first line of the comment should just be the /** code. |
| 72 | + if (!$isSingleLine && $tokens[$short]['line'] === $tokens[$stackPtr]['line']) { |
| 73 | + $error = 'The open comment tag must be the only content on the line'; |
| 74 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentAfterOpen'); |
| 75 | + if (true === $fix) { |
| 76 | + $phpcsFile->fixer->beginChangeset(); |
| 77 | + for ($i = ($stackPtr + 1); $i < $short; $i++) { |
| 78 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 79 | + } |
| 80 | + $phpcsFile->fixer->addNewline($stackPtr); |
| 81 | + $phpcsFile->fixer->replaceToken( |
| 82 | + $short, |
| 83 | + ltrim($tokens[$short]['content']) |
| 84 | + ); |
| 85 | + $phpcsFile->fixer->addContentBefore( |
| 86 | + $short, |
| 87 | + str_repeat(' ', $tokens[$stackPtr]['column']).'* ' |
| 88 | + ); |
| 89 | + $phpcsFile->fixer->endChangeset(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Check for additional blank lines at the beginning of the comment. |
| 94 | + if ($tokens[$stackPtr]['line'] < ($tokens[$short]['line'] - 1)) { |
| 95 | + $error = 'Additional blank lines found at beginning of doc comment'; |
| 96 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore'); |
| 97 | + if (true === $fix) { |
| 98 | + $phpcsFile->fixer->beginChangeset(); |
| 99 | + for ($i = ($stackPtr + 1); $i < $short; $i++) { |
| 100 | + if ($tokens[($i + 1)]['line'] === $tokens[$short]['line']) { |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 105 | + } |
| 106 | + |
| 107 | + $phpcsFile->fixer->endChangeset(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // The last line of the comment should just be the */ code. |
| 112 | + $prev = $phpcsFile->findPrevious($empty, ($commentEnd - 1), $stackPtr, true); |
| 113 | + if (!$isSingleLine && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) { |
| 114 | + $error = 'The close comment tag must be the only content on the line'; |
| 115 | + $fix = $phpcsFile->addFixableError($error, $commentEnd, 'ContentBeforeClose'); |
| 116 | + if (true === $fix) { |
| 117 | + $phpcsFile->fixer->beginChangeset(); |
| 118 | + for ($i = ($prev + 1); $i < $commentEnd; $i++) { |
| 119 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 120 | + } |
| 121 | + $phpcsFile->fixer->replaceToken( |
| 122 | + $commentEnd - 1, |
| 123 | + rtrim($tokens[$commentEnd - 1]['content']) |
| 124 | + ); |
| 125 | + $phpcsFile->fixer->addContentBefore( |
| 126 | + $commentEnd, |
| 127 | + str_repeat(' ', $tokens[$stackPtr]['column']) |
| 128 | + ); |
| 129 | + $phpcsFile->fixer->addNewlineBefore($commentEnd); |
| 130 | + $phpcsFile->fixer->endChangeset(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Check for additional blank lines at the end of the comment. |
| 135 | + if ($tokens[$prev]['line'] < ($tokens[$commentEnd]['line'] - 1)) { |
| 136 | + $error = 'Additional blank lines found at end of doc comment'; |
| 137 | + $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter'); |
| 138 | + if (true === $fix) { |
| 139 | + $phpcsFile->fixer->beginChangeset(); |
| 140 | + for ($i = ($prev + 1); $i < $commentEnd; $i++) { |
| 141 | + if ($tokens[($i + 1)]['line'] === $tokens[$commentEnd]['line']) { |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 146 | + } |
| 147 | + |
| 148 | + $phpcsFile->fixer->endChangeset(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments