Skip to content

Commit

Permalink
Use CommentAnalyzer::sanitizeDocblockType consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Oct 26, 2023
1 parent 147505c commit b1c5340
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ public static function sanitizeDocblockType(string $docblock_type): string
{
$docblock_type = (string) preg_replace('@^[ \t]*\*@m', '', $docblock_type);
$docblock_type = (string) preg_replace('/,\n\s+}/', '}', $docblock_type);
$docblock_type = (string) preg_replace('/[ \t]+/', ' ', $docblock_type);

return str_replace("\n", '', $docblock_type);
return trim(str_replace("\n", '', $docblock_type));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static function run(array $argv): void

self::emitMacPcreWarning($options, $threads);

self::restart($options, $threads, $progress);
//self::restart($options, $threads, $progress);

if (isset($options['debug-emitted-issues'])) {
$config->debug_emitted_issues = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static function parse(
$templates = [];
if (isset($parsed_docblock->combined_tags['template'])) {
foreach ($parsed_docblock->combined_tags['template'] as $offset => $template_line) {
$template_type = preg_split('/[\s]+/', (string) preg_replace('@^[ \t]*\*@m', '', $template_line));
$template_type = preg_split('/[\s]+/', CommentAnalyzer::sanitizeDocblockType($template_line));
if ($template_type === false) {
throw new IncorrectDocblockException('Invalid @ŧemplate tag: '.preg_last_error_msg());
throw new IncorrectDocblockException('Invalid @template tag: '.preg_last_error_msg());
}

$template_name = array_shift($template_type);
Expand Down Expand Up @@ -111,7 +111,7 @@ public static function parse(

if (isset($parsed_docblock->combined_tags['template-covariant'])) {
foreach ($parsed_docblock->combined_tags['template-covariant'] as $offset => $template_line) {
$template_type = preg_split('/[\s]+/', (string) preg_replace('@^[ \t]*\*@m', '', $template_line));
$template_type = preg_split('/[\s]+/', CommentAnalyzer::sanitizeDocblockType($template_line));
if ($template_type === false) {
throw new IncorrectDocblockException('Invalid @template-covariant tag: '.preg_last_error_msg());
}
Expand Down Expand Up @@ -171,20 +171,12 @@ public static function parse(

if (isset($parsed_docblock->tags['psalm-require-extends'])
&& count($extension_requirements = $parsed_docblock->tags['psalm-require-extends']) > 0) {
$info->extension_requirement = trim((string) preg_replace(
'@^[ \t]*\*@m',
'',
$extension_requirements[array_key_first($extension_requirements)],
));
$info->extension_requirement = CommentAnalyzer::sanitizeDocblockType($extension_requirements[array_key_first($extension_requirements)]);
}

if (isset($parsed_docblock->tags['psalm-require-implements'])) {
foreach ($parsed_docblock->tags['psalm-require-implements'] as $implementation_requirement) {
$info->implementation_requirements[] = trim((string) preg_replace(
'@^[ \t]*\*@m',
'',
$implementation_requirement,
));
$info->implementation_requirements[] = trim(CommentAnalyzer::sanitizeDocblockType($implementation_requirement));
}
}

Expand All @@ -199,7 +191,7 @@ public static function parse(
if (isset($parsed_docblock->tags['psalm-yield'])) {
$yield = (string) reset($parsed_docblock->tags['psalm-yield']);

$info->yield = trim((string) preg_replace('@^[ \t]*\*@m', '', $yield));
$info->yield = trim(CommentAnalyzer::sanitizeDocblockType($yield));
}

if (isset($parsed_docblock->tags['deprecated'])) {
Expand Down Expand Up @@ -552,7 +544,7 @@ protected static function addMagicPropertyToInfo(

$end = $offset + strlen($line_parts[0]);

$line_parts[0] = str_replace("\n", '', (string) preg_replace('@^[ \t]*\*@m', '', $line_parts[0]));
$line_parts[0] = CommentAnalyzer::sanitizeDocblockType($line_parts[0]);

if ($line_parts[0] === ''
|| ($line_parts[0][0] === '$'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ public function handleTraitUse(PhpParser\Node\Stmt\TraitUse $node): void
$this->useTemplatedType(
$storage,
$node,
trim((string) preg_replace('@^[ \t]*\*@m', '', $template_line)),
trim(CommentAnalyzer::sanitizeDocblockType($template_line)),
);
}
}
Expand Down Expand Up @@ -1912,10 +1912,7 @@ private static function getTypeAliasesFromCommentLines(
continue;
}

$var_line = (string) preg_replace('/[ \t]+/', ' ', (string) preg_replace('@^[ \t]*\*@m', '', $var_line));
$var_line = (string) preg_replace('/,\n\s+\}/', '}', $var_line);
$var_line = str_replace("\n", '', $var_line);

$var_line = CommentAnalyzer::sanitizeDocblockType($var_line);
$var_line_parts = preg_split('/( |=)/', $var_line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

if (!$var_line_parts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ public static function parse(
$line_parts[1] = substr($line_parts[1], 1);
}

$line_parts[0] = str_replace(
"\n",
'',
(string) preg_replace('@^[ \t]*\*@m', '', $line_parts[0]),
);
$line_parts[0] = CommentAnalyzer::sanitizeDocblockType($line_parts[0]);

if ($line_parts[0] === ''
|| ($line_parts[0][0] === '$'
Expand Down Expand Up @@ -194,14 +190,10 @@ public static function parse(
$line_parts = CommentAnalyzer::splitDocLine($param);

if (count($line_parts) > 0) {
$line_parts[0] = str_replace(
"\n",
'',
(string) preg_replace('@^[ \t]*\*@m', '', $line_parts[0]),
);
$line_parts[0] = CommentAnalyzer::sanitizeDocblockType($line_parts[0]);

$info->self_out = [
'type' => str_replace("\n", '', $line_parts[0]),
'type' => $line_parts[0],
'line_number' => $comment->getStartLine() + substr_count(
$comment_text,
"\n",
Expand All @@ -225,10 +217,10 @@ public static function parse(
foreach ($parsed_docblock->tags['psalm-if-this-is'] as $offset => $param) {
$line_parts = CommentAnalyzer::splitDocLine($param);

$line_parts[0] = str_replace("\n", '', (string) preg_replace('@^[ \t]*\*@m', '', $line_parts[0]));
$line_parts[0] = CommentAnalyzer::sanitizeDocblockType($line_parts[0]);

$info->if_this_is = [
'type' => str_replace("\n", '', $line_parts[0]),
'type' => $line_parts[0],
'line_number' => $comment->getStartLine() + substr_count(
$comment->getText(),
"\n",
Expand Down Expand Up @@ -454,7 +446,7 @@ public static function parse(
$templates = [];
if (isset($parsed_docblock->combined_tags['template'])) {
foreach ($parsed_docblock->combined_tags['template'] as $offset => $template_line) {
$template_type = preg_split('/[\s]+/', (string) preg_replace('@^[ \t]*\*@m', '', $template_line));
$template_type = preg_split('/[\s]+/', CommentAnalyzer::sanitizeDocblockType($template_line));
if ($template_type === false) {
throw new AssertionError(preg_last_error_msg());
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Template/TraitTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,32 @@ class B {
use T;
}',
],
'multilineTemplateUse' => [
'code' => '<?php
/**
* @template T1
* @template T2
* @template T3
*/
trait MyTrait {}
class Foo {
/**
* @template-use MyTrait<int, int, array{
* foo: mixed,
* bar: mixed,
* }>
*/
use MyTrait;
}
class Bar {
/**
* @template-use MyTrait<int, string, bar>
*/
use MyTrait;
}'
],
'allowTraitExtendAndImplementWithExplicitParamType' => [
'code' => '<?php
/**
Expand Down

0 comments on commit b1c5340

Please sign in to comment.