Skip to content

Commit 826f7e8

Browse files
committed
Factor out a helper for a stricter check that the comment is ONLY {@inheritdoc} (and nothing else).
1 parent 201f663 commit 826f7e8

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
4848
$return = null;
4949

5050
if ($this->skipIfInheritdoc === true) {
51-
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
52-
$trimmedContent = strtolower(trim($tokens[$i]['content']));
53-
if ($trimmedContent === '{@inheritdoc}') {
54-
return;
55-
}
51+
if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
52+
return;
5653
}
5754
}
5855

@@ -206,11 +203,8 @@ protected function processThrows(File $phpcsFile, $stackPtr, $commentStart)
206203
$tokens = $phpcsFile->getTokens();
207204

208205
if ($this->skipIfInheritdoc === true) {
209-
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
210-
$trimmedContent = strtolower(trim($tokens[$i]['content']));
211-
if ($trimmedContent === '{@inheritdoc}') {
212-
return;
213-
}
206+
if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
207+
return;
214208
}
215209
}
216210

@@ -290,11 +284,8 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
290284
$tokens = $phpcsFile->getTokens();
291285

292286
if ($this->skipIfInheritdoc === true) {
293-
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
294-
$trimmedContent = strtolower(trim($tokens[$i]['content']));
295-
if ($trimmedContent === '{@inheritdoc}') {
296-
return;
297-
}
287+
if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
288+
return;
298289
}
299290
}
300291

@@ -729,4 +720,38 @@ protected function checkSpacingAfterParamName(File $phpcsFile, $param, $maxVar,
729720
}//end checkSpacingAfterParamName()
730721

731722

723+
/**
724+
* Determines whether the whole comment is an inheritdoc comment.
725+
*
726+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
727+
* @param int $stackPtr The position of the current token
728+
* in the stack passed in $tokens.
729+
* @param int $commentStart The position in the stack where the comment started.
730+
*
731+
* @return void
732+
*/
733+
protected function checkInheritdoc(File $phpcsFile, $stackPtr, $commentStart)
734+
{
735+
$tokens = $phpcsFile->getTokens();
736+
737+
$allowedTokens = [
738+
T_DOC_COMMENT_OPEN_TAG,
739+
T_DOC_COMMENT_WHITESPACE,
740+
T_DOC_COMMENT_STAR,
741+
];
742+
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
743+
if (in_array($tokens[$i]['code'], $allowedTokens) === false) {
744+
$trimmedContent = strtolower(trim($tokens[$i]['content']));
745+
746+
if ($trimmedContent === '{@inheritdoc}') {
747+
return true;
748+
} else {
749+
return false;
750+
}
751+
}
752+
}
753+
754+
}//end checkInheritdoc()
755+
756+
732757
}//end class

0 commit comments

Comments
 (0)