From 5bafc9512c1e83658bc21f9e04cdcdc13358bcaf Mon Sep 17 00:00:00 2001 From: Anner Visser Date: Mon, 14 Nov 2022 16:03:35 +0100 Subject: [PATCH 1/2] Add support for type aliases in @psalm-assert(-*) annotations Fixes #8263 --- .../Reflector/FunctionLikeDocblockScanner.php | 3 +- tests/TypeAnnotationTest.php | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php index 5452d899e6b..14eddc5d5a3 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php @@ -615,7 +615,8 @@ private static function getAssertionParts( true ), null, - $template_types + $template_types, + $type_aliases ); } catch (TypeParseTreeException $e) { $storage->docblock_issues[] = new InvalidDocblock( diff --git a/tests/TypeAnnotationTest.php b/tests/TypeAnnotationTest.php index ee512e26bcc..4d15c612e05 100644 --- a/tests/TypeAnnotationTest.php +++ b/tests/TypeAnnotationTest.php @@ -593,6 +593,47 @@ class C extends A {} '$output' => 'string', ], ], + 'importedTypeUsedInAssertion' => [ + 'code' => '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', + ] + ] ]; } From dd253b755b25e38a3943116261cce66c94679fa2 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 14 Nov 2022 14:54:55 -0400 Subject: [PATCH 2/2] Conserve some vertical space --- tests/TypeAnnotationTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/TypeAnnotationTest.php b/tests/TypeAnnotationTest.php index 4d15c612e05..c55a5911221 100644 --- a/tests/TypeAnnotationTest.php +++ b/tests/TypeAnnotationTest.php @@ -595,12 +595,8 @@ class C extends A {} ], 'importedTypeUsedInAssertion' => [ 'code' => '