1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #808 - detect badly-placed ampersand early

This commit is contained in:
Matt Brown 2018-06-11 17:23:28 -04:00
parent 6542a0a784
commit 54893fdd55
3 changed files with 69 additions and 2 deletions

View File

@ -54,11 +54,11 @@ class ParseTree
throw new TypeParseTreeException('Unexpected token ' . $type_token);
case '[':
if ($next_token !== ']') {
if ($current_leaf instanceof ParseTree\Root) {
throw new TypeParseTreeException('Unexpected token ' . $type_token);
}
if ($current_leaf instanceof ParseTree\Root) {
if ($next_token !== ']') {
throw new TypeParseTreeException('Unexpected token ' . $type_token);
}
@ -143,6 +143,11 @@ class ParseTree
break;
case ',':
if ($current_leaf instanceof ParseTree\Root) {
throw new TypeParseTreeException('Unexpected token ' . $type_token);
}
if (!$current_leaf->parent) {
throw new TypeParseTreeException('Cannot parse comma without a parent node');
}
@ -213,6 +218,10 @@ class ParseTree
break;
case ':':
if ($current_leaf instanceof ParseTree\Root) {
throw new TypeParseTreeException('Unexpected token ' . $type_token);
}
$current_parent = $current_leaf->parent;
if ($current_leaf instanceof ParseTree\CallableTree) {
@ -277,6 +286,10 @@ class ParseTree
break;
case '|':
if ($current_leaf instanceof ParseTree\Root) {
throw new TypeParseTreeException('Unexpected token ' . $type_token);
}
$added_null = false;
$current_parent = $current_leaf->parent;
@ -321,6 +334,12 @@ class ParseTree
break;
case '&':
if ($current_leaf instanceof ParseTree\Root) {
throw new TypeParseTreeException(
'Unexpected &'
);
}
$current_parent = $current_leaf->parent;
if ($current_parent && $current_parent instanceof ParseTree\IntersectionTree) {

View File

@ -1426,6 +1426,14 @@ class AnnotationTest extends TestCase
}',
'error_message' => 'InvalidDocblock',
],
'badAmpersand' => [
'<?php
/** @return &array */
function foo() : array {
return [];
}',
'error_message' => 'InvalidDocblock',
],
];
}
}

View File

@ -453,6 +453,46 @@ class TypeParseTest extends TestCase
Type::parseString('string;');
}
/**
* @expectedException \Psalm\Exception\TypeParseTreeException
*
* @return void
*/
public function testBadAmpersand()
{
Type::parseString('&array');
}
/**
* @expectedException \Psalm\Exception\TypeParseTreeException
*
* @return void
*/
public function testBadColon()
{
Type::parseString(':array');
}
/**
* @expectedException \Psalm\Exception\TypeParseTreeException
*
* @return void
*/
public function testBadEquals()
{
Type::parseString('=array');
}
/**
* @expectedException \Psalm\Exception\TypeParseTreeException
*
* @return void
*/
public function testBadBar()
{
Type::parseString('|array');
}
/**
* @expectedException \Psalm\Exception\TypeParseTreeException
*