Skip to content

Commit

Permalink
Factor out a helper for a stricter check that the comment is ONLY {@i…
Browse files Browse the repository at this point in the history
…nheritdoc} (and nothing else).
  • Loading branch information
xjm committed Aug 13, 2020
1 parent 201f663 commit 826f7e8
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
$return = null;

if ($this->skipIfInheritdoc === true) {
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
$trimmedContent = strtolower(trim($tokens[$i]['content']));
if ($trimmedContent === '{@inheritdoc}') {
return;
}
if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
return;
}
}

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

if ($this->skipIfInheritdoc === true) {
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
$trimmedContent = strtolower(trim($tokens[$i]['content']));
if ($trimmedContent === '{@inheritdoc}') {
return;
}
if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
return;
}
}

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

if ($this->skipIfInheritdoc === true) {
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
$trimmedContent = strtolower(trim($tokens[$i]['content']));
if ($trimmedContent === '{@inheritdoc}') {
return;
}
if ($this->checkInheritdoc($phpcsFile, $stackPtr, $commentStart) === true) {
return;
}
}

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


/**
* Determines whether the whole comment is an inheritdoc comment.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
protected function checkInheritdoc(File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();

$allowedTokens = [
T_DOC_COMMENT_OPEN_TAG,
T_DOC_COMMENT_WHITESPACE,
T_DOC_COMMENT_STAR,
];
for ($i = $commentStart; $i <= $tokens[$commentStart]['comment_closer']; $i++) {
if (in_array($tokens[$i]['code'], $allowedTokens) === false) {
$trimmedContent = strtolower(trim($tokens[$i]['content']));

if ($trimmedContent === '{@inheritdoc}') {
return true;
} else {
return false;
}
}
}

}//end checkInheritdoc()


}//end class

0 comments on commit 826f7e8

Please sign in to comment.