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

Fixed #9496 #9497

Merged
merged 5 commits into from
Mar 16, 2023
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 @@ -1905,9 +1905,12 @@ private static function getTypeAliasesFromCommentLines(
}

$type_string = str_replace("\n", '', implode('', $var_line_parts));

// Strip any remaining characters after the last grouping character >, } or )
$type_string = preg_replace('/(?<=[>})])[^>})]*$/', '', $type_string, 1);
try {
$type_string = CommentAnalyzer::splitDocLine($type_string)[0];
} catch (DocblockParseException $e) {
throw new DocblockParseException($type_string . ' is not a valid type: '.$e->getMessage());
}
$type_string = CommentAnalyzer::sanitizeDocblockType($type_string);

try {
$type_tokens = TypeTokenizer::getFullyQualifiedTokens(
Expand Down
110 changes: 110 additions & 0 deletions tests/TypeAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,88 @@ public function doStuff(): array {
'$output===' => 'list<1|2>',
],
],
'callableWithReturnTypeTypeAliasWithinBackets' => [
'code' => '<?php
/** @psalm-type TCallback (callable():int) */
class Foo {
/** @psalm-var TCallback */
public static callable $callback;
}
$output = Foo::$callback;
',
'assertions' => [
'$output===' => 'callable():int',
],
],
'callableWithReturnTypeTypeAlias' => [
'code' => '<?php
/** @psalm-type TCallback callable():int */
class Foo {
/** @psalm-var TCallback */
public static callable $callback;
}
$output = Foo::$callback;
',
'assertions' => [
'$output===' => 'callable():int',
],
],
'unionOfStringsContainingBraceChar' => [
'code' => '<?php
/** @psalm-type T \'{\'|\'}\' */
class Foo {
/** @psalm-var T */
public static string $t;
}
$t = Foo::$t;
',
'assertions' => [
'$t===' => '\'{\'|\'}\'',
],
],
'unionOfStringsContainingGTChar' => [
'code' => '<?php
/** @psalm-type T \'<\'|\'>\' */
class Foo {
/** @psalm-var T */
public static string $t;
}
$t = Foo::$t;
',
'assertions' => [
'$t===' => '\'<\'|\'>\'',
],
],
'unionOfStringsContainingBracketChar' => [
'code' => '<?php
/** @psalm-type T \'(\'|\')\' */
class Foo {
/** @psalm-var T */
public static string $t;
}
$t = Foo::$t;
',
'assertions' => [
'$t===' => '\'(\'|\')\'',
],
],
'bareWordsCommentAfterType' => [
'code' => '<?php
/**
* @psalm-type T string
*
* Lorem ipsum
*/
class Foo {
/** @psalm-var T */
public static string $t;
}
$t = Foo::$t;
',
'assertions' => [
'$t===' => 'string',
],
],
'handlesTypeWhichEndsWithRoundBracket' => [
'code' => '<?php
/**
Expand All @@ -661,6 +743,34 @@ public function doStuff(): array {
class A {}
',
],
'commentAfterType' => [
'code' => '<?php
/**
* @psalm-type TTest string
*
* This is a test class.
*/
class Test {}',
],
'multilineTypeWithComment' => [
'code' => '<?php
/**
* @psalm-type PhoneType = array{
* phone: string
* }
*
* Bar
*/
class Foo {
/** @var PhoneType */
public static $phone;
}
$output = Foo::$phone;
',
'assertions' => [
'$output===' => 'array{phone: string}',
],
],
];
}

Expand Down