Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check-type when using builtin types from within a namespace #10648

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
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;
Expand Down Expand Up @@ -678,11 +680,23 @@ private static function analyzeStatement(
} else {
try {
$checked_type = $context->vars_in_scope[$checked_var_id];
$fq_check_type_string = Type::getFQCLNFromString(

$path = $statements_analyzer->getRootFilePath();
$file_storage = $codebase->file_storage_provider->get($path);

$check_tokens = TypeTokenizer::getFullyQualifiedTokens(
$check_type_string,
$statements_analyzer->getAliases(),
$statements_analyzer->getTemplateTypeMap(),
$file_storage->type_aliases,
);
$check_type = TypeParser::parseTokens(
$check_tokens,
null,
$statements_analyzer->getTemplateTypeMap() ?? [],
$file_storage->type_aliases,
true,
);
$check_type = Type::parseString($fq_check_type_string);
/** @psalm-suppress InaccessibleProperty We just created this type */
$check_type->possibly_undefined = $possibly_undefined;

Expand Down
31 changes: 30 additions & 1 deletion tests/AssertAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,36 @@ function takesSomeIntFromEnum(int $foo): IntEnum
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
Expand Down
9 changes: 9 additions & 0 deletions tests/CheckTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ final class A {}
$_a = new stdClass();
/** @psalm-check-type-exact $_a = \stdClass */',
];
yield 'allowType' => [
'code' => '<?php
namespace X;

/** @psalm-type A = int|string */

$_a = 1;
/** @psalm-check-type $_a = A */',
];
}

public function providerInvalidCodeParse(): iterable
Expand Down
Loading