diff --git a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php index dbffad76d88..25e68730d7e 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeAnalyzer.php @@ -241,9 +241,8 @@ public static function verifyReturnType( } $inferred_return_type_parts_with_never = $inferred_return_type_parts; - $number_of_types = count($inferred_return_type_parts); // we filter TNever that have no bearing on the return type - if ($number_of_types > 1) { + if (count($inferred_return_type_parts) > 1) { $inferred_return_type_parts = array_filter( $inferred_return_type_parts, static fn(Union $union_type): bool => !$union_type->isNever() @@ -260,7 +259,7 @@ public static function verifyReturnType( } // void + never = null, so we need to check this separately - if ($number_of_types > 1 + if (count($inferred_return_type_parts_with_never) > 1 && !$function_always_exits && $inferred_return_type_parts_with_never !== $inferred_return_type_parts) { $inferred_return_type_with_never = Type::combineUnionTypeArray( @@ -614,8 +613,7 @@ public static function verifyReturnType( return false; } } - } elseif ( - ($declared_return_type->explicit_never === false || !$declared_return_type->isNull()) + } elseif (($declared_return_type->explicit_never === false || !$declared_return_type->isNull()) && ( !$declared_return_type->isNullable() || ($parent_class === null && $self_fq_class_name === $source->getFQCLN())