Skip to content

Commit dbb802b

Browse files
committed
Silence sniff for simple cases.
1 parent 94afcf8 commit dbb802b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

PhpCollective/Sniffs/Commenting/DocBlockReturnVoidSniff.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@ public function process(File $phpcsFile, $stackPtr): void
7171
$docBlockReturnIndex = $this->findDocBlockReturn($phpcsFile, $docBlockStartIndex, $docBlockEndIndex);
7272

7373
$hasInheritDoc = $this->hasInheritDoc($phpcsFile, $docBlockStartIndex, $docBlockEndIndex);
74+
$hasReturnType = $this->hasReturnType($phpcsFile, $stackPtr);
7475

7576
// If interface we will at least report it
7677
if (empty($tokens[$stackPtr]['scope_opener']) || empty($tokens[$stackPtr]['scope_closer'])) {
77-
if (!$docBlockReturnIndex && !$hasInheritDoc) {
78+
if (!$docBlockReturnIndex && !$hasInheritDoc && !$hasReturnType) {
7879
$phpcsFile->addError('Method does not have a return statement in doc block: ' . $tokens[$nextIndex]['content'], $nextIndex, 'ReturnMissingInInterface');
7980
}
8081

8182
return;
8283
}
8384

8485
// If inheritDoc is present assume the parent contains it
85-
if (!$docBlockReturnIndex && $hasInheritDoc) {
86+
if (!$docBlockReturnIndex && ($hasInheritDoc || $this->hasReturnType($phpcsFile, $stackPtr))) {
8687
return;
8788
}
8889

@@ -378,4 +379,30 @@ protected function documentedReturnType(array $tokens, int $docBlockReturnIndex)
378379

379380
return $documentedReturnType;
380381
}
382+
383+
/**
384+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
385+
* @param int $stackPtr
386+
*
387+
* @return bool
388+
*/
389+
protected function hasReturnType(File $phpcsFile, int $stackPtr): bool
390+
{
391+
$tokens = $phpcsFile->getTokens();
392+
393+
$parenthesisCloserIndex = $tokens[$stackPtr]['parenthesis_closer'];
394+
$scopeOpenerIndex = $tokens[$stackPtr]['scope_opener'];
395+
$nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $parenthesisCloserIndex + 1, $scopeOpenerIndex, true);
396+
397+
if ($tokens[$nextIndex]['code'] !== T_COLON) {
398+
return false;
399+
}
400+
401+
$typeHintIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $nextIndex + 1, $scopeOpenerIndex, true);
402+
if (!$typeHintIndex) {
403+
return false;
404+
}
405+
406+
return true;
407+
}
381408
}

0 commit comments

Comments
 (0)