1
0
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:
orklah 2021-05-13 00:47:05 +02:00 committed by GitHub
parent ab1732de66
commit 1a59e81808
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -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();
}

View File

@ -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";
}
'
]
];
}