Skip to content
Permalink
Browse files

Add support for spread operator before type as well

Ref #2429
  • Loading branch information
muglug committed Dec 19, 2019
1 parent bb52c4c commit ab03ac06f498d4fe1c7319ba635ad0fd2a9baaef
Showing with 28 additions and 10 deletions.
  1. +15 −5 src/Psalm/Internal/Type/ParseTree.php
  2. +7 −1 src/Psalm/Type.php
  3. +6 −4 tests/TypeParseTest.php
@@ -243,7 +243,13 @@ public static function createFromTokens(array $type_tokens)
}

if (!$current_parent || !$current_leaf) {
throw new TypeParseTreeException('Unexpected token ' . $type_token[0]);
if ($current_leaf instanceof ParseTree\CallableTree
&& $type_token[0] === '...'
) {
$current_parent = $current_leaf;
} else {
throw new TypeParseTreeException('Unexpected token ' . $type_token[0]);
}
}

if ($current_parent instanceof ParseTree\CallableParamTree) {
@@ -253,12 +259,16 @@ public static function createFromTokens(array $type_tokens)
$new_leaf = new ParseTree\CallableParamTree($current_parent);
$new_leaf->has_default = $type_token[0] === '=';
$new_leaf->variadic = $type_token[0] === '...';
$new_leaf->children = [$current_leaf];

$current_leaf->parent = $new_leaf;
if ($current_parent !== $current_leaf) {
$new_leaf->children = [$current_leaf];
$current_leaf->parent = $new_leaf;

array_pop($current_parent->children);
$current_parent->children[] = $new_leaf;
array_pop($current_parent->children);
$current_parent->children[] = $new_leaf;
} else {
$current_parent->children[] = $new_leaf;
}

$current_leaf = $new_leaf;

@@ -805,7 +805,13 @@ public static function tokenize($string_type, $ignore_space = true)
continue;
}

if ($was_space && ($char === '$' || $char === '.')) {
if ($was_space
&& ($char === '$'
|| ($char === '.'
&& ($chars[$i + 1] ?? null) === '.'
&& ($chars[$i + 2] ?? null) === '.'
&& ($chars[$i + 3] ?? null) === '$'))
) {
$type_tokens[++$rtc] = [' ', $i - 1];
$type_tokens[++$rtc] = ['', $i];
} elseif ($was_char) {
@@ -426,7 +426,7 @@ public function testObjectLikeOptional()
/**
* @return void
*/
public function testCallable()
public function testSimpleCallable()
{
$this->assertSame(
'callable(int, string):void',
@@ -543,10 +543,12 @@ public function testCallableWithGoodVariadic()
/**
* @return void
*/
public function testCallableWithBadVariadic()
public function testCallableWithSpreadBefore()
{
$this->expectException(\Psalm\Exception\TypeParseTreeException::class);
Type::parseString('callable(int, ...string) : void');
$this->assertSame(
'callable(int, string...):void',
(string)Type::parseString('callable(int, ...string):void')
);
}

/**

0 comments on commit ab03ac0

Please sign in to comment.
You can’t perform that action at this time.