Skip to content

Commit

Permalink
Fix #4287 - intersection of regular arrays should not result in crash
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 7, 2020
1 parent c8d4baf commit 1f2d276
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -411,11 +411,12 @@ function (ParseTree $child_tree) use ($codebase, $template_type_map, $type_alias
$parse_tree->children
);

$onlyTKeyedArray = true;

$first_type = \reset($intersection_types);
$last_type = \end($intersection_types);

$onlyTKeyedArray = $first_type instanceof TKeyedArray
&& $last_type instanceof TKeyedArray;

foreach ($intersection_types as $intersection_type) {
if (!$intersection_type instanceof TKeyedArray
&& ($intersection_type !== $first_type
Expand Down
6 changes: 6 additions & 0 deletions tests/TypeParseTest.php
Expand Up @@ -203,6 +203,12 @@ public function testIntersectionOfTKeyedArrayWithConflictingProperties(): void
Type::parseString('array{a: string}&array{a: int}');
}

public function testIntersectionOfTwoRegularArrays() : void
{
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
Type::parseString('string[]&array<string, string>');
}

public function testUnionOfIntersectionOfTKeyedArray(): void
{
$this->assertSame('array{a: int|string, b?: int}', (string) Type::parseString('array{a: int}|array{a: string}&array{b: int}'));
Expand Down

0 comments on commit 1f2d276

Please sign in to comment.