Skip to content

Commit b648acf

Browse files
committed
UselessVariableSniff: Variables with @phpstan-var or @psalm-var annotation are not reported as useless
1 parent 85e3329 commit b648acf

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private function hasVariableVarAnnotation(File $phpcsFile, int $variablePointer)
293293
}
294294

295295
$docCommentContent = TokenHelper::getContent($phpcsFile, $tokens[$pointerBeforeVariable]['comment_opener'], $pointerBeforeVariable);
296-
return preg_match('~@var\\s+.+\\s+' . preg_quote($tokens[$variablePointer]['content'], '~') . '(?:\\s|$)~', $docCommentContent) !== 0;
296+
return preg_match('~@(?:(?:phpstan|psalm)-)?var\\s+.+\\s+' . preg_quote($tokens[$variablePointer]['content'], '~') . '(?:\\s|$)~', $docCommentContent) !== 0;
297297
}
298298

299299
private function hasAnotherAssigmentBefore(File $phpcsFile, int $variablePointer, string $variableName): bool

tests/Sniffs/Variables/data/uselessVariableNoErrors.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ function withGenericDocComment() {
6666
return $hh;
6767
}
6868

69+
function withPhpsanDocComment() {
70+
/** @phpstan-var string $hhh */
71+
$hhh = [];
72+
return $hhh;
73+
}
74+
75+
function withPsalmDocComment() {
76+
/** @psalm-var string $hhhh */
77+
$hhhh = [];
78+
return $hhhh;
79+
}
80+
6981
function moreAssigments() {
7082
$i = 'i';
7183
$i .= 'ii';

0 commit comments

Comments
 (0)