Skip to content

Commit

Permalink
Merge pull request #9497 from ptomulik/issue-9496
Browse files Browse the repository at this point in the history
Fixed #9496
  • Loading branch information
orklah committed Mar 16, 2023
2 parents c4f6b0c + 24ddf9e commit 531eec6
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
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
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

0 comments on commit 531eec6

Please sign in to comment.