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

Improve parsing of @psalm-type #10713

Merged
merged 2 commits into from
Feb 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
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;
Expand Down Expand Up @@ -1932,7 +1933,7 @@ private static function getTypeAliasesFromCommentLines(
continue;
}

if ($var_line_parts[0] === ' ') {
while (isset($var_line_parts[0]) && $var_line_parts[0] === ' ') {
array_shift($var_line_parts);
}

Expand All @@ -1948,11 +1949,12 @@ private static function getTypeAliasesFromCommentLines(
continue;
}

if ($var_line_parts[0] === ' ') {
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 {
$type_string = CommentAnalyzer::splitDocLine($type_string)[0];
} catch (DocblockParseException $e) {
Expand Down
22 changes: 22 additions & 0 deletions tests/AnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,28 @@ function example(string $_x) : void {}',
*/
class A {}',
],
'multipeLineGenericArray2' => [
'code' => '<?php
/**
* @psalm-type TRelAlternate =
* list<
* array{
* href: string,
* lang: string
* }
* >
*/
class A {
/** @return TRelAlternate */
public function ret(): array { return []; }
}

$_ = (new A)->ret();
',
'assertions' => [
'$_===' => 'list<array{href: string, lang: string}>',
],
],
'builtInClassInAShape' => [
'code' => '<?php
/**
Expand Down
16 changes: 16 additions & 0 deletions tests/TypeAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,22 @@ public function bar(array $foo): void {}
}
PHP,
],
'typeWithMultipleSpaces' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-type Foo = string
* @psalm-type Bar int
*/
class A {
/**
* @psalm-param Foo $foo
* @psalm-param Bar $bar
*/
public function bar(string $foo, int $bar): void {}
}
PHP,
],
];
}

Expand Down