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

Support user defined types for psalm-check-type

This commit is contained in:
robchett 2024-02-03 18:26:20 +00:00
parent 526013e77e
commit 9d4fd4011d
2 changed files with 15 additions and 1 deletions

View File

@ -680,16 +680,21 @@ final class StatementsAnalyzer extends SourceAnalyzer
} else {
try {
$checked_type = $context->vars_in_scope[$checked_var_id];
$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,
);
/** @psalm-suppress InaccessibleProperty We just created this type */

View File

@ -38,6 +38,15 @@ class CheckTypeTest extends TestCase
$_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