Support scalar type declarations

This commit is contained in:
Nikita Popov 2015-06-13 20:16:09 +02:00
parent 71fa7c6674
commit 0da72fad00
5 changed files with 65 additions and 2 deletions

View File

@ -330,7 +330,7 @@ parameter:
;
type:
name { $$ = $1; }
name { $$ = $this->handleScalarTypes($1); }
| T_ARRAY { $$ = 'array'; }
| T_CALLABLE { $$ = 'callable'; }
;

View File

@ -1623,7 +1623,7 @@ class Php7 extends \PhpParser\ParserAbstract
}
protected function reduceRule203() {
$this->semValue = $this->semStack[$this->stackPos-(1-1)];
$this->semValue = $this->handleScalarTypes($this->semStack[$this->stackPos-(1-1)]);
}
protected function reduceRule204() {

View File

@ -6,6 +6,8 @@ namespace PhpParser;
* This parser is based on a skeleton written by Moriyoshi Koizumi, which in
* turn is based on work by Masato Bito.
*/
use PhpParser\Node\Name;
abstract class ParserAbstract implements ParserInterface
{
const SYMBOL_NONE = -1;
@ -461,4 +463,20 @@ abstract class ParserAbstract implements ParserInterface
}
return $style;
}
protected function handleScalarTypes(Name $name) {
$scalarTypes = [
'bool' => true,
'int' => true,
'float' => true,
'string' => true,
];
if (!$name->isUnqualified()) {
return $name;
}
$lowerName = strtolower($name->toString());
return isset($scalarTypes[$lowerName]) ? $lowerName : $name;
}
}

View File

@ -0,0 +1,45 @@
Scalar type declarations
-----
<?php
function test(bool $a, Int $b, FLOAT $c, StRiNg $d) {}
-----
!!php7
array(
0: Stmt_Function(
byRef: false
name: test
params: array(
0: Param(
type: bool
byRef: false
variadic: false
name: a
default: null
)
1: Param(
type: int
byRef: false
variadic: false
name: b
default: null
)
2: Param(
type: float
byRef: false
variadic: false
name: c
default: null
)
3: Param(
type: string
byRef: false
variadic: false
name: d
default: null
)
)
returnType: null
stmts: array(
)
)
)