Skip to content

Commit

Permalink
Add support for type aliases in @psalm-assert(-*) annotations
Browse files Browse the repository at this point in the history
Fixes #8263
  • Loading branch information
annervisser committed Nov 14, 2022
1 parent 56bc854 commit 5bafc95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Expand Up @@ -615,7 +615,8 @@ private static function getAssertionParts(
true
),
null,
$template_types
$template_types,
$type_aliases
);
} catch (TypeParseTreeException $e) {
$storage->docblock_issues[] = new InvalidDocblock(
Expand Down
41 changes: 41 additions & 0 deletions tests/TypeAnnotationTest.php
Expand Up @@ -593,6 +593,47 @@ class C extends A {}
'$output' => 'string',
],
],
'importedTypeUsedInAssertion' => [
'code' => '<?php
/**
* @psalm-type Foo = string
*/
class A
{
}
/**
* @psalm-immutable
* @psalm-import-type Foo from A as FooAlias
*/
class B {
/**
* @param mixed $input
* @psalm-return FooAlias
*/
public function convertToFoo($input) {
$this->assertFoo($input);
return $input;
}
/**
* @param mixed $value
* @psalm-assert FooAlias $value
*/
private function assertFoo($value): void {
if(!is_string($value)) {
throw new \InvalidArgumentException();
}
}
}
$instance = new B();
$output = $instance->convertToFoo("hallo");
',
'assertions' => [
'$output' => 'string',
]
]
];
}

Expand Down

0 comments on commit 5bafc95

Please sign in to comment.