Skip to content

Commit d9cab8c

Browse files
First try
1 parent c58e5a0 commit d9cab8c

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
class FunctionCommentSniff implements Sniff
2525
{
2626

27+
/**
28+
* @var array<string>
29+
*/
30+
public $commentProhibitedFunctions = [];
31+
2732
/**
2833
* A map of invalid data types to valid ones for param and return documentation.
2934
*
@@ -93,14 +98,29 @@ public function process(File $phpcsFile, $stackPtr)
9398

9499
break;
95100
}
101+
if (isset($tokens[$commentEnd]['comment_opener'])) {
102+
$commentStart = $tokens[$commentEnd]['comment_opener'];
103+
}
96104

97-
// Constructor methods are exempt from requiring a docblock.
98-
// @see https://www.drupal.org/project/coder/issues/3400560.
99105
$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
102107
&& $tokens[$commentEnd]['code'] !== T_COMMENT
103108
) {
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+
104124
return;
105125
}
106126

@@ -139,7 +159,6 @@ public function process(File $phpcsFile, $stackPtr)
139159
return;
140160
}
141161

142-
$commentStart = $tokens[$commentEnd]['comment_opener'];
143162
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
144163
// This is a file comment, not a function comment.
145164
if ($tokens[$tag]['content'] === '@file') {

0 commit comments

Comments
 (0)