1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Use TypeAlias object to allow future extension

This commit is contained in:
Brown 2020-05-13 19:29:59 -04:00
parent 0fc3398631
commit 190c9ce27e
5 changed files with 47 additions and 21 deletions

View File

@ -12,6 +12,7 @@ use Psalm\Internal\Scanner\ClassLikeDocblockComment;
use Psalm\Internal\Scanner\FunctionDocblockComment;
use Psalm\Internal\Scanner\VarDocblockComment;
use Psalm\Internal\Type\ParseTree;
use Psalm\Internal\Type\TypeAlias;
use Psalm\Internal\Type\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Type;
@ -46,7 +47,7 @@ class CommentAnalyzer
/**
* @param array<string, array<string, array{Type\Union}>>|null $template_type_map
* @param array<string, array<int, array{0: string, 1: int}>> $type_aliases
* @param array<string, TypeAlias> $type_aliases
*
* @throws DocblockParseException if there was a problem parsing the docblock
*
@ -73,7 +74,7 @@ class CommentAnalyzer
/**
* @param array<string, array<string, array{Type\Union}>>|null $template_type_map
* @param array<string, array<int, array{0: string, 1: int}>> $type_aliases
* @param array<string, TypeAlias> $type_aliases
* @param array{description:string, specials:array<string, array<int, string>>} $parsed_docblock
*
* @return VarDocblockComment[]
@ -240,11 +241,11 @@ class CommentAnalyzer
/**
* @param Aliases $aliases
* @param array<string, array<int, array{0: string, 1: int}>> $type_aliases
* @param array<string, TypeAlias> $type_aliases
*
* @throws DocblockParseException if there was a problem parsing the docblock
*
* @return array<string, list<array{0: string, 1: int}>>
* @return array<string, TypeAlias>
*/
public static function getTypeAliasesFromComment(
PhpParser\Comment\Doc $comment,
@ -267,11 +268,11 @@ class CommentAnalyzer
/**
* @param array<string> $type_alias_comment_lines
* @param Aliases $aliases
* @param array<string, array<int, array{0: string, 1: int}>> $type_aliases
* @param array<string, TypeAlias> $type_aliases
*
* @throws DocblockParseException if there was a problem parsing the docblock
*
* @return array<string, list<array{0: string, 1: int}>>
* @return array<string, TypeAlias>
*/
private static function getTypeAliasesFromCommentLines(
array $type_alias_comment_lines,
@ -335,7 +336,7 @@ class CommentAnalyzer
throw new DocblockParseException($type_string . ' is not a valid type');
}
$type_alias_tokens[$type_alias] = $type_tokens;
$type_alias_tokens[$type_alias] = new TypeAlias($type_tokens);
}
return $type_alias_tokens;

View File

@ -42,6 +42,7 @@ use Psalm\Internal\Scanner\FileScanner;
use Psalm\Internal\Scanner\PhpStormMetaScanner;
use Psalm\Internal\Scanner\UnresolvedConstant;
use Psalm\Internal\Scanner\UnresolvedConstantComponent;
use Psalm\Internal\Type\TypeAlias;
use Psalm\Internal\Type\TypeParser;
use Psalm\Internal\Type\TypeTokenizer;
use Psalm\Issue\DuplicateClass;
@ -127,7 +128,7 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
private $skip_if_descendants = null;
/**
* @var array<string, array<int, array{0: string, 1: int}>>
* @var array<string, TypeAlias>
*/
private $type_aliases = [];
@ -164,9 +165,11 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
$this->type_aliases
);
foreach ($type_alias_tokens as $type_tokens) {
// finds issues, if there are any
TypeParser::parseTokens($type_tokens);
foreach ($type_alias_tokens as $type_alias) {
if ($type_alias->replacement_tokens) {
// finds issues, if there are any
TypeParser::parseTokens($type_alias->replacement_tokens);
}
}
$this->type_aliases += $type_alias_tokens;

View File

@ -0,0 +1,18 @@
<?php
namespace Psalm\Internal\Type;
class TypeAlias
{
/**
* @var list<array{0: string, 1: int}>|null
*/
public $replacement_tokens = null;
/**
* @param list<array{0: string, 1: int}>|null $replacement_tokens
*/
public function __construct(?array $replacement_tokens)
{
$this->replacement_tokens = $replacement_tokens;
}
}

View File

@ -322,8 +322,8 @@ class TypeTokenizer
}
/**
* @param array<string, mixed>|null $template_type_map
* @param array<string, array<int, array{0: string, 1: int}>>|null $type_aliases
* @param array<string, mixed>|null $template_type_map
* @param array<string, TypeAlias>|null $type_aliases
*
* @return list<array{0: string, 1: int}>
*/
@ -443,17 +443,21 @@ class TypeTokenizer
}
if (isset($type_aliases[$string_type_token[0]])) {
$replacement_tokens = $type_aliases[$string_type_token[0]];
$type_alias = $type_aliases[$string_type_token[0]];
array_unshift($replacement_tokens, ['(', $i]);
array_push($replacement_tokens, [')', $i]);
if ($type_alias->replacement_tokens) {
$replacement_tokens = $type_alias->replacement_tokens;
$diff = count($replacement_tokens) - 1;
array_unshift($replacement_tokens, ['(', $i]);
array_push($replacement_tokens, [')', $i]);
array_splice($type_tokens, $i, 1, $replacement_tokens);
$diff = count($replacement_tokens) - 1;
$i += $diff;
$l += $diff;
array_splice($type_tokens, $i, 1, $replacement_tokens);
$i += $diff;
$l += $diff;
}
} else {
$type_tokens[$i][0] = \Psalm\Type::getFQCLNFromString(
$string_type_token[0],

View File

@ -82,7 +82,7 @@ class FileStorage
public $docblock_issues = [];
/**
* @var array<string, array<int, array{0: string, 1: int}>>
* @var array<string, \Psalm\Internal\Type\TypeAlias>
*/
public $type_aliases = [];