From 893b60ed0d123a19d600d5132b94adbce3957a37 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 15 Feb 2024 13:34:28 +0100 Subject: [PATCH 1/2] Improve parsing of psalm-type --- .../Reflector/ClassLikeNodeScanner.php | 6 ++--- tests/AnnotationTest.php | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php index 999f95df5..f43fc71f6 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php @@ -78,6 +78,7 @@ use function get_class; use function implode; use function is_int; use function is_string; +use function ltrim; use function preg_match; use function preg_replace; use function preg_split; @@ -1948,11 +1949,8 @@ final class ClassLikeNodeScanner continue; } - if ($var_line_parts[0] === ' ') { - array_shift($var_line_parts); - } - $type_string = implode('', $var_line_parts); + $type_string = ltrim($type_string, "* \n\r"); try { $type_string = CommentAnalyzer::splitDocLine($type_string)[0]; } catch (DocblockParseException $e) { diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index 64ebf674b..4252e60b8 100644 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -513,6 +513,28 @@ class AnnotationTest extends TestCase */ class A {}', ], + 'multipeLineGenericArray2' => [ + 'code' => ' + */ + class A { + /** @return TRelAlternate */ + public function ret(): array { return []; } + } + + $_ = (new A)->ret(); + ', + 'assertions' => [ + '$_===' => 'list', + ], + ], 'builtInClassInAShape' => [ 'code' => ' Date: Thu, 15 Feb 2024 23:23:40 +0100 Subject: [PATCH 2/2] Allow multiple spaces between type name and type definition --- .../Reflector/ClassLikeNodeScanner.php | 6 +++++- tests/TypeAnnotationTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php index f43fc71f6..828012fa7 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/ClassLikeNodeScanner.php @@ -1933,7 +1933,7 @@ final class ClassLikeNodeScanner continue; } - if ($var_line_parts[0] === ' ') { + while (isset($var_line_parts[0]) && $var_line_parts[0] === ' ') { array_shift($var_line_parts); } @@ -1949,6 +1949,10 @@ final class ClassLikeNodeScanner continue; } + while (isset($var_line_parts[0]) && $var_line_parts[0] === ' ') { + array_shift($var_line_parts); + } + $type_string = implode('', $var_line_parts); $type_string = ltrim($type_string, "* \n\r"); try { diff --git a/tests/TypeAnnotationTest.php b/tests/TypeAnnotationTest.php index 0e101137c..89cc4f999 100644 --- a/tests/TypeAnnotationTest.php +++ b/tests/TypeAnnotationTest.php @@ -916,6 +916,22 @@ class TypeAnnotationTest extends TestCase } PHP, ], + 'typeWithMultipleSpaces' => [ + 'code' => <<<'PHP' +