From 59b435642a5bb91d184da209d8f41234188300d6 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Thu, 28 Aug 2025 03:09:29 +0200 Subject: [PATCH] Do not remove true/false phpdoc if type hint is bool Fix https://github.com/slevomat/coding-standard/issues/1777 --- .../Helpers/AnnotationHelper.php | 2 +- .../Sniffs/TypeHints/ReturnTypeHintSniff.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/SlevomatCodingStandard/Helpers/AnnotationHelper.php b/SlevomatCodingStandard/Helpers/AnnotationHelper.php index ebce8e72b..afc7d5603 100644 --- a/SlevomatCodingStandard/Helpers/AnnotationHelper.php +++ b/SlevomatCodingStandard/Helpers/AnnotationHelper.php @@ -286,7 +286,7 @@ public static function isAnnotationUseless( ['true', 'false', 'null'], true, )) { - return $enableStandaloneNullTrueFalseTypeHints; + return $typeHint->getTypeHint() === strtolower($annotationType->name) ? $enableStandaloneNullTrueFalseTypeHints : false; } if (TypeHintHelper::isSimpleUnofficialTypeHints( diff --git a/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php b/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php index 802e4b54d..fc4f4e6ea 100644 --- a/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php +++ b/SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php @@ -183,6 +183,31 @@ private function checkFunctionTypeHint( } } + if ( + $this->enableStandaloneNullTrueFalseTypeHints + && $returnTypeHint->getTypeHint() === 'bool' + && $returnTypeNode instanceof IdentifierTypeNode + && in_array(strtolower($returnTypeNode->name), ['true', 'false'], true) + ) { + $fix = $phpcsFile->addFixableError( + sprintf( + '%s %s() has return type hint "bool" but it should be possible to use "%s" based on @return annotation "%s".', + FunctionHelper::getTypeLabel($phpcsFile, $functionPointer), + FunctionHelper::getFullyQualifiedName($phpcsFile, $functionPointer), + strtolower($returnTypeNode->name), + AnnotationTypeHelper::print($returnTypeNode), + ), + $functionPointer, + self::CODE_LESS_SPECIFIC_NATIVE_TYPE_HINT, + ); + + if ($fix) { + $phpcsFile->fixer->beginChangeset(); + FixerHelper::replace($phpcsFile, $returnTypeHint->getStartPointer(), strtolower($returnTypeNode->name)); + $phpcsFile->fixer->endChangeset(); + } + } + return; }