mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Fix #808 - detect badly-placed ampersand early
This commit is contained in:
parent
6542a0a784
commit
54893fdd55
@ -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) {
|
||||
|
@ -1426,6 +1426,14 @@ class AnnotationTest extends TestCase
|
||||
}',
|
||||
'error_message' => 'InvalidDocblock',
|
||||
],
|
||||
'badAmpersand' => [
|
||||
'<?php
|
||||
/** @return &array */
|
||||
function foo() : array {
|
||||
return [];
|
||||
}',
|
||||
'error_message' => 'InvalidDocblock',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user