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 #9506 #9509

Merged
merged 6 commits into from Mar 28, 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
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