Skip to content

Commit

Permalink
Merge pull request #9509 from ptomulik/issue-9506
Browse files Browse the repository at this point in the history
Fixed #9506
  • Loading branch information
orklah committed Mar 28, 2023
2 parents c6f66bf + ed1d095 commit e2abc3e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
Expand Up @@ -143,6 +143,7 @@ public function __construct(

/**
* @return false|null
* @psalm-suppress ComplexMethod
*/
public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool
{
Expand Down Expand Up @@ -420,10 +421,17 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool

if ($template_map[1] !== null && $template_map[2] !== null) {
if (trim($template_map[2])) {
$type_string = $template_map[2];
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 {
$template_type = TypeParser::parseTokens(
TypeTokenizer::getFullyQualifiedTokens(
$template_map[2],
$type_string,
$this->aliases,
$storage->template_types,
$this->type_aliases,
Expand Down
Expand Up @@ -9,8 +9,10 @@
use Psalm\CodeLocation\DocblockTypeLocation;
use Psalm\Codebase;
use Psalm\Config;
use Psalm\Exception\DocblockParseException;
use Psalm\Exception\InvalidMethodOverrideException;
use Psalm\Exception\TypeParseTreeException;
use Psalm\Internal\Analyzer\CommentAnalyzer;
use Psalm\Internal\Analyzer\NamespaceAnalyzer;
use Psalm\Internal\Scanner\FileScanner;
use Psalm\Internal\Scanner\FunctionDocblockComment;
Expand Down Expand Up @@ -1440,10 +1442,17 @@ private static function handleTemplates(

if ($template_map[1] !== null && $template_map[2] !== null) {
if (trim($template_map[2])) {
$type_string = $template_map[2];
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 {
$template_type = TypeParser::parseTokens(
TypeTokenizer::getFullyQualifiedTokens(
$template_map[2],
$type_string,
$aliases,
$storage->template_types + ($template_types ?: []),
$type_aliases,
Expand Down
58 changes: 58 additions & 0 deletions tests/Template/ClassTemplateTest.php
Expand Up @@ -4051,6 +4051,64 @@ public function __construct(
'ignored_issues' => [],
'php_version' => '8.0',
],
'template of simple type with additional comment without dot' => [
'code' => '<?php
/**
* @psalm-template T of string
*
* lorem ipsum
*/
class Foo {
/** @psalm-var T */
public string $t;
/** @psalm-param T $t */
public function __construct(string $t) {
$this->t = $t;
}
/**
* @psalm-return T
*/
public function t(): string {
return $this->t;
}
}
$t = (new Foo(\'\'))->t();
',
'assertions' => [
'$t===' => '\'\'',
],
],
'template of simple type with additional comment with dot' => [
'code' => '<?php
/**
* @psalm-template T of string
*
* lorem ipsum.
*/
class Foo {
/** @psalm-var T */
public string $t;
/** @psalm-param T $t */
public function __construct(string $t) {
$this->t = $t;
}
/**
* @psalm-return T
*/
public function t(): string {
return $this->t;
}
}
$t = (new Foo(\'\'))->t();
',
'assertions' => [
'$t===' => '\'\'',
],
],
];
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Template/FunctionTemplateTest.php
Expand Up @@ -1659,6 +1659,20 @@ function normalizeField(mixed $value, Norm $n): void
'ignored_issues' => [],
'php_version' => '8.0',
],
'templateWithCommentAfterSimpleType' => [
'code' => '<?php
/**
* @template T of string
*
* lorem ipsumm
*
* @param T $t
*/
function foo(string $t): string
{
return $t;
}',
],
];
}

Expand Down

0 comments on commit e2abc3e

Please sign in to comment.