Skip to content

Commit

Permalink
Merge pull request #10648 from robchett/check_type_namespace_allow_re…
Browse files Browse the repository at this point in the history
…served_words
  • Loading branch information
weirdan authored Feb 3, 2024
2 parents 74314eb + 9d4fd40 commit 080c8f6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
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

0 comments on commit 080c8f6

Please sign in to comment.