1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Don’t throw an exceptin when parsing invalid type

This commit is contained in:
Matt Brown 2021-02-16 20:24:16 -05:00
parent 196f24aac3
commit 686f9fcaaa
2 changed files with 25 additions and 1 deletions

View File

@ -185,7 +185,11 @@ class AssertionReconciler extends \Psalm\Type\Reconciler
$assertion = 'class-string';
}
$new_type = Type::parseString($assertion, null, $template_type_map);
try {
$new_type = Type::parseString($assertion, null, $template_type_map);
} catch (\Psalm\Exception\TypeParseTreeException $e) {
$new_type = Type::getMixed();
}
}
if ($existing_var_type->hasMixed()) {

View File

@ -986,6 +986,26 @@ class FunctionTemplateAssertTest extends TestCase
}',
'error_message' => 'string, string',
],
'noCrashWhenOnUnparseableTemplatedAssertion' => [
'<?php
/**
* @template TCandidateKey as array-key
* @param array $arr
* @param TCandidateKey $key
* @psalm-assert has-array-key<TCandidateKey> $arr
*/
function keyExists(array $arr, $key) : void {
if (!array_key_exists($key, $arr)) {
throw new \Exception("bad");
}
}
function fromArray(array $data) : void {
keyExists($data, "id");
if (is_string($data["id"])) {}
}',
'error_message' => 'InvalidDocblock',
],
];
}
}