1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix check-type when using reserved types from within a namespace

This commit is contained in:
robchett 2024-02-03 18:09:23 +00:00
parent 192f7b99a1
commit 526013e77e
2 changed files with 41 additions and 3 deletions

View File

@ -45,6 +45,8 @@ use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Internal\ReferenceConstraint;
use Psalm\Internal\Scanner\ParsedDocblock;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Type\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Issue\CheckType;
use Psalm\Issue\ComplexFunction;
use Psalm\Issue\ComplexMethod;
@ -678,11 +680,18 @@ final class StatementsAnalyzer extends SourceAnalyzer
} else {
try {
$checked_type = $context->vars_in_scope[$checked_var_id];
$fq_check_type_string = Type::getFQCLNFromString(
$check_tokens = TypeTokenizer::getFullyQualifiedTokens(
$check_type_string,
$statements_analyzer->getAliases(),
$statements_analyzer->getTemplateTypeMap(),
);
$check_type = TypeParser::parseTokens(
$check_tokens,
null,
$statements_analyzer->getTemplateTypeMap() ?? [],
[],
true,
);
$check_type = Type::parseString($fq_check_type_string);
/** @psalm-suppress InaccessibleProperty We just created this type */
$check_type->possibly_undefined = $possibly_undefined;

View File

@ -2255,7 +2255,36 @@ class AssertAnnotationTest extends TestCase
function isNonEmptyString($_str): bool
{
return true;
}',
}
',
],
'assertStringIsNonEmptyStringInNamespace' => [
'code' => '<?php
namespace X;
/** @var string $str */;
/** @var string|int $stringOrInt */;
if (isNonEmptyString($str)) {
/** @psalm-check-type-exact $str = non-empty-string */;
} else {
/** @psalm-check-type-exact $str = string */;
}
if (isNonEmptyString($stringOrInt)) {
/** @psalm-check-type-exact $stringOrInt = non-empty-string */;
} else {
/** @psalm-check-type-exact $stringOrInt = string|int */;
}
/**
* @param mixed $_str
* @psalm-assert-if-true non-empty-string $_str
*/
function isNonEmptyString($_str): bool
{
return true;
}
',
],
'assertObjectWithClosedInheritance' => [
'code' => '<?php