Skip to content

Commit

Permalink
Support user defined types for psalm-check-type
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Feb 3, 2024
1 parent 526013e commit 9d4fd40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,21 @@ private static function analyzeStatement(
} 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 */
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 9d4fd40

Please sign in to comment.