mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
allow scalar values in return to be accepted, even when the branch has no inferred types (#5749)
This commit is contained in:
parent
ab1732de66
commit
1a59e81808
@ -20,6 +20,8 @@ class ReturnTypeCollector
|
||||
* @param list<Type\Union> $yield_types
|
||||
*
|
||||
* @return list<Type\Union> a list of return types
|
||||
*
|
||||
* @psalm-suppress ComplexMethod to be refactored
|
||||
*/
|
||||
public static function getReturnTypes(
|
||||
Codebase $codebase,
|
||||
@ -38,6 +40,18 @@ class ReturnTypeCollector
|
||||
$return_types[] = $stmt_type;
|
||||
|
||||
$yield_types = array_merge($yield_types, self::getYieldTypeFromExpression($stmt->expr, $nodes));
|
||||
} elseif ($stmt->expr instanceof PhpParser\Node\Scalar\String_) {
|
||||
$return_types[] = Type::getString();
|
||||
} elseif ($stmt->expr instanceof PhpParser\Node\Scalar\LNumber) {
|
||||
$return_types[] = Type::getString();
|
||||
} elseif ($stmt->expr instanceof PhpParser\Node\Expr\ConstFetch) {
|
||||
if ((string)$stmt->expr->name === 'true') {
|
||||
$return_types[] = Type::getTrue();
|
||||
} elseif ((string)$stmt->expr->name === 'false') {
|
||||
$return_types[] = Type::getFalse();
|
||||
} elseif ((string)$stmt->expr->name === 'null') {
|
||||
$return_types[] = Type::getNull();
|
||||
}
|
||||
} else {
|
||||
$return_types[] = Type::getMixed();
|
||||
}
|
||||
|
@ -919,6 +919,20 @@ class ReturnTypeTest extends TestCase
|
||||
throw new RuntimeException;
|
||||
}
|
||||
'
|
||||
],
|
||||
'scalarLiteralsInferredAfterUndefinedClass' => [
|
||||
'<?php
|
||||
/** @param object $arg */
|
||||
function test($arg): ?string
|
||||
{
|
||||
/** @psalm-suppress UndefinedClass */
|
||||
if ($arg instanceof SomeClassThatDoesNotExist) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return "b";
|
||||
}
|
||||
'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user