|
24 | 24 | class FunctionCommentSniff implements Sniff
|
25 | 25 | {
|
26 | 26 |
|
| 27 | + /** |
| 28 | + * @var array<string> |
| 29 | + */ |
| 30 | + public $commentProhibitedFunctions = []; |
| 31 | + |
27 | 32 | /**
|
28 | 33 | * A map of invalid data types to valid ones for param and return documentation.
|
29 | 34 | *
|
@@ -93,14 +98,29 @@ public function process(File $phpcsFile, $stackPtr)
|
93 | 98 |
|
94 | 99 | break;
|
95 | 100 | }
|
| 101 | + if (isset($tokens[$commentEnd]['comment_opener'])) { |
| 102 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
| 103 | + } |
96 | 104 |
|
97 |
| - // Constructor methods are exempt from requiring a docblock. |
98 |
| - // @see https://www.drupal.org/project/coder/issues/3400560. |
99 | 105 | $methodName = $phpcsFile->getDeclarationName($stackPtr);
|
100 |
| - if ($methodName === '__construct' |
101 |
| - && $tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
| 106 | + if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
102 | 107 | && $tokens[$commentEnd]['code'] !== T_COMMENT
|
103 | 108 | ) {
|
| 109 | + if ($methodName === '__construct') { |
| 110 | + // Constructor methods are exempt from requiring a docblock. |
| 111 | + // @see https://www.drupal.org/project/coder/issues/3400560. |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + elseif (in_array($methodName, $this->commentProhibitedFunctions, true)) { |
| 116 | + // Method prohibited to have docblock. |
| 117 | + $fix = $phpcsFile->addFixableError("It's forbidden to document $methodName function", $stackPtr, 'DocProhibited'); |
| 118 | + if ($fix === true) { |
| 119 | + for ($i = $commentStart; $i <= $commentEnd; $i++) { |
| 120 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 121 | + } |
| 122 | + } |
| 123 | + |
104 | 124 | return;
|
105 | 125 | }
|
106 | 126 |
|
@@ -139,7 +159,6 @@ public function process(File $phpcsFile, $stackPtr)
|
139 | 159 | return;
|
140 | 160 | }
|
141 | 161 |
|
142 |
| - $commentStart = $tokens[$commentEnd]['comment_opener']; |
143 | 162 | foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
|
144 | 163 | // This is a file comment, not a function comment.
|
145 | 164 | if ($tokens[$tag]['content'] === '@file') {
|
|
0 commit comments