mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Support scalar type declarations
This commit is contained in:
parent
71fa7c6674
commit
0da72fad00
@ -330,7 +330,7 @@ parameter:
|
||||
;
|
||||
|
||||
type:
|
||||
name { $$ = $1; }
|
||||
name { $$ = $this->handleScalarTypes($1); }
|
||||
| T_ARRAY { $$ = 'array'; }
|
||||
| T_CALLABLE { $$ = 'callable'; }
|
||||
;
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
45
test/code/parser/stmt/function/scalarTypeDeclarations.test
Normal file
45
test/code/parser/stmt/function/scalarTypeDeclarations.test
Normal 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(
|
||||
)
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue
Block a user