Skip to content

Commit

Permalink
Fix parsing of class-string-map<T of Foo, T>
Browse files Browse the repository at this point in the history
  • Loading branch information
robchett committed Oct 26, 2023
1 parent 6def5c4 commit c108204
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Psalm/Internal/Type/TypeTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use Psalm\Internal\Type\TypeAlias\InlineTypeAlias;
use Psalm\Type;

use function array_slice;
use function array_splice;
use function array_unshift;
use function count;
use function implode;
use function in_array;
use function is_numeric;
use function preg_match;
Expand Down Expand Up @@ -146,11 +148,9 @@ public static function tokenize(string $string_type, bool $ignore_space = true):
$type_tokens[++$rtc] = [' ', $i - 1];
$type_tokens[++$rtc] = ['', $i];
} elseif ($was_space
&& ($char === 'a' || $char === 'i')
&& ($chars[$i + 1] ?? null) === 's'
&& ($chars[$i + 2] ?? null) === ' '
&& in_array(implode('', array_slice($chars, $i, 3)), ['as ', 'is ', 'of '])
) {
$type_tokens[++$rtc] = [$char . 's', $i - 1];
$type_tokens[++$rtc] = [$char . $chars[$i+1], $i - 1];
$type_tokens[++$rtc] = ['', ++$i];
$was_char = false;
continue;
Expand Down
8 changes: 8 additions & 0 deletions tests/TypeParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,14 @@ public function testClassStringMap(): void
);
}

public function testClassStringMapOf(): void
{
$this->assertSame(
'class-string-map<T of Foo, T>',
Type::parseString('class-string-map<T of Foo, T>')->getId(false),
);
}

public function testVeryLargeType(): void
{
$very_large_type = 'array{a: Closure():(array<array-key, mixed>|null), b?: Closure():array<array-key, mixed>, c?: Closure():array<array-key, mixed>, d?: Closure():array<array-key, mixed>, e?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), p?: Closure():(array{f: null|string, g: null|string, h: null|string, i: string, j: mixed, k: mixed, l: mixed, m: mixed, n: bool, o?: array{0: string}}|null), q: string, r?: Closure():(array<array-key, mixed>|null), s: array<array-key, mixed>}|null';
Expand Down

0 comments on commit c108204

Please sign in to comment.