Skip to content

Commit 59b4356

Browse files
committed
Do not remove true/false phpdoc if type hint is bool
Fix #1777
1 parent 03face2 commit 59b4356

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

SlevomatCodingStandard/Helpers/AnnotationHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public static function isAnnotationUseless(
286286
['true', 'false', 'null'],
287287
true,
288288
)) {
289-
return $enableStandaloneNullTrueFalseTypeHints;
289+
return $typeHint->getTypeHint() === strtolower($annotationType->name) ? $enableStandaloneNullTrueFalseTypeHints : false;
290290
}
291291

292292
if (TypeHintHelper::isSimpleUnofficialTypeHints(

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,31 @@ private function checkFunctionTypeHint(
183183
}
184184
}
185185

186+
if (
187+
$this->enableStandaloneNullTrueFalseTypeHints
188+
&& $returnTypeHint->getTypeHint() === 'bool'
189+
&& $returnTypeNode instanceof IdentifierTypeNode
190+
&& in_array(strtolower($returnTypeNode->name), ['true', 'false'], true)
191+
) {
192+
$fix = $phpcsFile->addFixableError(
193+
sprintf(
194+
'%s %s() has return type hint "bool" but it should be possible to use "%s" based on @return annotation "%s".',
195+
FunctionHelper::getTypeLabel($phpcsFile, $functionPointer),
196+
FunctionHelper::getFullyQualifiedName($phpcsFile, $functionPointer),
197+
strtolower($returnTypeNode->name),
198+
AnnotationTypeHelper::print($returnTypeNode),
199+
),
200+
$functionPointer,
201+
self::CODE_LESS_SPECIFIC_NATIVE_TYPE_HINT,
202+
);
203+
204+
if ($fix) {
205+
$phpcsFile->fixer->beginChangeset();
206+
FixerHelper::replace($phpcsFile, $returnTypeHint->getStartPointer(), strtolower($returnTypeNode->name));
207+
$phpcsFile->fixer->endChangeset();
208+
}
209+
}
210+
186211
return;
187212
}
188213

0 commit comments

Comments
 (0)