Skip to content

Commit 44678bf

Browse files
committed
Fix inline docblock var return.
1 parent 361aef8 commit 44678bf

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

PSR2R/Sniffs/Commenting/InlineDocBlockSniff.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PSR2R\Sniffs\Commenting;
44

55
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Util\Tokens;
67
use PSR2R\Tools\AbstractSniff;
78

89
/**
@@ -192,7 +193,11 @@ protected function findErrors(File $phpCsFile, $contentIndex) {
192193

193194
preg_match('|^(.+?)(\s+)(.+?)\s*$|', $comment, $contentMatches);
194195
if (!$contentMatches || !$contentMatches[1] || !$contentMatches[3]) {
195-
$phpCsFile->addError('Invalid Inline Doc Block content', $contentIndex, 'ContentInvalid');
196+
if ($this->hasReturnAsFollowingToken($phpCsFile, $contentIndex)) {
197+
return [];
198+
}
199+
200+
$phpCsFile->addError('Invalid Inline Doc Block content, expected `{Type} ${var}` style', $contentIndex, 'ContentInvalid');
196201
return [];
197202
}
198203

@@ -209,4 +214,21 @@ protected function findErrors(File $phpCsFile, $contentIndex) {
209214
return $errors;
210215
}
211216

217+
/**
218+
* @param \PHP_CodeSniffer\Files\File $phpCsFile
219+
* @param int $contentIndex
220+
*
221+
* @return bool
222+
*/
223+
protected function hasReturnAsFollowingToken(File $phpCsFile, $contentIndex) {
224+
$nextIndex = $phpCsFile->findNext(Tokens::$emptyTokens, $contentIndex + 1, null, true);
225+
if (!$nextIndex) {
226+
return false;
227+
}
228+
229+
$tokens = $phpCsFile->getTokens();
230+
231+
return $tokens[$nextIndex]['code'] === T_RETURN;
232+
}
233+
212234
}

0 commit comments

Comments
 (0)