From 242a38c18445fbb666e798412aa9fe1c67114fb4 Mon Sep 17 00:00:00 2001 From: rhertogh Date: Thu, 6 Jul 2023 00:01:00 +0200 Subject: [PATCH 1/5] Allow space before array shape opening brace and added unit tests for `\Psalm\Internal\Analyzer\CommentAnalyzer::splitDocLine()` --- .../Internal/Analyzer/CommentAnalyzer.php | 5 ++ tests/CommentAnalyzerTest.php | 65 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/Psalm/Internal/Analyzer/CommentAnalyzer.php b/src/Psalm/Internal/Analyzer/CommentAnalyzer.php index b2d34db5199..15e93908ba4 100644 --- a/src/Psalm/Internal/Analyzer/CommentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/CommentAnalyzer.php @@ -346,6 +346,11 @@ public static function splitDocLine(string $return_block): array continue; } + if ($next_char === '{') { + $type .= ' '; + continue; + } + if ($next_char === '|' || $next_char === '&') { $nexter_char = $i < $l - 2 ? $return_block[$i + 2] : null; diff --git a/tests/CommentAnalyzerTest.php b/tests/CommentAnalyzerTest.php index 63c22f8ecaf..cbc5c51a7cc 100644 --- a/tests/CommentAnalyzerTest.php +++ b/tests/CommentAnalyzerTest.php @@ -75,4 +75,69 @@ public function testDocblockDescriptionWithVarDescription(): void $comment_docblock = CommentAnalyzer::getTypeFromComment($php_parser_doc, new FileScanner('somefile.php', 'somefile.php', false), new Aliases); $this->assertSame('Use a string', $comment_docblock[0]->description); } + + /** + * @dataProvider providerSplitDocLine + * @param string $doc_line + * @param string[] $expected + */ + public function testSplitDocLine(string $doc_line, array $expected): void + { + $this->assertSame($expected, CommentAnalyzer::splitDocLine($doc_line)); + } + + public function providerSplitDocLine(): iterable + { + return [ + 'typeWithVar' => [ + 'doc_line' => + 'TArray $array', + 'expected' => [ + 'TArray', + '$array', + ], + ], + 'arrayShape' => [ + 'doc_line' => + 'array{ + * a: int, + * b: string, + * }', + 'expected' => [ + 'array{ + * a: int, + * b: string, + * }', + ] + ], + 'arrayShapeWithSpace' => [ + 'doc_line' => + 'array { + * a: int, + * b: string, + * }', + 'expected' => [ + 'array { + * a: int, + * b: string, + * }', + ] + ], + 'func_num_args' => [ + 'doc_line' => + '( + * func_num_args() is 1 + * ? array{dirname: string, basename: string, extension?: string, filename: string} + * : string + * )', + 'expected' => [ + '( + * func_num_args() is 1 + * ? array{dirname: string, basename: string, extension?: string, filename: string} + * : string + * )' + ] + ], + ]; + } } From fc85ecb1f60ceb3dd954905c8ea9aa768618d052 Mon Sep 17 00:00:00 2001 From: rhertogh Date: Thu, 6 Jul 2023 18:31:26 +0200 Subject: [PATCH 2/5] Code style fixes for \Psalm\Tests\CommentAnalyzerTest (https://app.circleci.com/pipelines/github/vimeo/psalm/11905/workflows/353387bb-1dcc-4527-9569-c83ebe43864c/jobs/39822) --- tests/CommentAnalyzerTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/CommentAnalyzerTest.php b/tests/CommentAnalyzerTest.php index cbc5c51a7cc..bda9f6df4d4 100644 --- a/tests/CommentAnalyzerTest.php +++ b/tests/CommentAnalyzerTest.php @@ -78,7 +78,6 @@ public function testDocblockDescriptionWithVarDescription(): void /** * @dataProvider providerSplitDocLine - * @param string $doc_line * @param string[] $expected */ public function testSplitDocLine(string $doc_line, array $expected): void @@ -108,7 +107,7 @@ public function providerSplitDocLine(): iterable * a: int, * b: string, * }', - ] + ], ], 'arrayShapeWithSpace' => [ 'doc_line' => @@ -121,7 +120,7 @@ public function providerSplitDocLine(): iterable * a: int, * b: string, * }', - ] + ], ], 'func_num_args' => [ 'doc_line' => @@ -135,8 +134,8 @@ public function providerSplitDocLine(): iterable * func_num_args() is 1 * ? array{dirname: string, basename: string, extension?: string, filename: string} * : string - * )' - ] + * )', + ], ], ]; } From a69b0caa239cf565f49a3ec157519e6d7679ee59 Mon Sep 17 00:00:00 2001 From: rhertogh Date: Fri, 7 Jul 2023 16:25:10 +0200 Subject: [PATCH 3/5] Added return type specification for `\Psalm\Tests\CommentAnalyzerTest::providerSplitDocLine()` (https://github.com/vimeo/psalm/pull/10000#issuecomment-1624354237) --- tests/CommentAnalyzerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/CommentAnalyzerTest.php b/tests/CommentAnalyzerTest.php index bda9f6df4d4..a570ab5a63b 100644 --- a/tests/CommentAnalyzerTest.php +++ b/tests/CommentAnalyzerTest.php @@ -85,6 +85,9 @@ public function testSplitDocLine(string $doc_line, array $expected): void $this->assertSame($expected, CommentAnalyzer::splitDocLine($doc_line)); } + /** + * @return iterable> + */ public function providerSplitDocLine(): iterable { return [ From d2ec23fc7175401cf676f4f8bb97fd4691dfe862 Mon Sep 17 00:00:00 2001 From: rhertogh Date: Sat, 8 Jul 2023 23:01:35 +0200 Subject: [PATCH 4/5] 2nd attempt to fix `CommentAnalyzerTest::providerSplitDocLine()` docblock (https://github.com/vimeo/psalm/pull/10000#issuecomment-1627430145) --- tests/CommentAnalyzerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CommentAnalyzerTest.php b/tests/CommentAnalyzerTest.php index a570ab5a63b..d0e164a68bf 100644 --- a/tests/CommentAnalyzerTest.php +++ b/tests/CommentAnalyzerTest.php @@ -86,7 +86,7 @@ public function testSplitDocLine(string $doc_line, array $expected): void } /** - * @return iterable> + * @return iterable> */ public function providerSplitDocLine(): iterable { From 5a1bb56fc2ae117e840d8c5bb6fe41a478711077 Mon Sep 17 00:00:00 2001 From: rhertogh Date: Sun, 9 Jul 2023 14:26:58 +0200 Subject: [PATCH 5/5] 3nd attempt to fix `CommentAnalyzerTest::providerSplitDocLine()` docblock --- tests/CommentAnalyzerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CommentAnalyzerTest.php b/tests/CommentAnalyzerTest.php index d0e164a68bf..b034627fa64 100644 --- a/tests/CommentAnalyzerTest.php +++ b/tests/CommentAnalyzerTest.php @@ -86,7 +86,7 @@ public function testSplitDocLine(string $doc_line, array $expected): void } /** - * @return iterable> + * @return iterable */ public function providerSplitDocLine(): iterable {