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

Allow space before array shape opening brace and added unit tests #10000

Merged
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Expand Up @@ -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;

Expand Down
67 changes: 67 additions & 0 deletions tests/CommentAnalyzerTest.php
Expand Up @@ -75,4 +75,71 @@ 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[] $expected
*/
public function testSplitDocLine(string $doc_line, array $expected): void
{
$this->assertSame($expected, CommentAnalyzer::splitDocLine($doc_line));
}

/**
* @return iterable<array-key, array{doc_line: string, expected: string[]}>
*/
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
* )',
],
],
];
}
}