Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for type aliases in @psalm-assert(-*) annotations #8705

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
37 changes: 37 additions & 0 deletions tests/TypeAnnotationTest.php
Expand Up @@ -593,6 +593,43 @@ class C extends A {}
'$output' => 'string',
],
],
'importedTypeUsedInAssertion' => [
'code' => '<?php
/** @psalm-type Foo = string */
class A {}
annervisser marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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