From 122f4499608620a2a9336ad8d790333d4790ca73 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 22 Dec 2016 22:23:30 +0100 Subject: [PATCH] Represent builtin types using Identifier as well --- grammar/php5.y | 4 +-- grammar/php7.y | 4 +-- lib/PhpParser/Parser/Php5.php | 4 +-- lib/PhpParser/Parser/Php7.php | 4 +-- lib/PhpParser/ParserAbstract.php | 8 +++++- test/code/parser/identMode.test | 45 ++++++++++++++++++++++++++++++-- 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/grammar/php5.y b/grammar/php5.y index 3c82bb1..10ba1df 100644 --- a/grammar/php5.y +++ b/grammar/php5.y @@ -380,8 +380,8 @@ parameter: type: name { $$ = $1; } - | T_ARRAY { $$ = 'array'; } - | T_CALLABLE { $$ = 'callable'; } + | T_ARRAY { $$ = makeIdent('array'); } + | T_CALLABLE { $$ = makeIdent('callable'); } ; optional_param_type: diff --git a/grammar/php7.y b/grammar/php7.y index 100c1ad..0bc721c 100644 --- a/grammar/php7.y +++ b/grammar/php7.y @@ -387,8 +387,8 @@ type_expr: type: name { $$ = $this->handleBuiltinTypes($1); } - | T_ARRAY { $$ = 'array'; } - | T_CALLABLE { $$ = 'callable'; } + | T_ARRAY { $$ = makeIdent('array'); } + | T_CALLABLE { $$ = makeIdent('callable'); } ; optional_param_type: diff --git a/lib/PhpParser/Parser/Php5.php b/lib/PhpParser/Parser/Php5.php index 39410d8..02b7f9d 100644 --- a/lib/PhpParser/Parser/Php5.php +++ b/lib/PhpParser/Parser/Php5.php @@ -1819,11 +1819,11 @@ class Php5 extends \PhpParser\ParserAbstract } protected function reduceRule227() { - $this->semValue = 'array'; + $this->semValue = ($this->useIdentifierNodes ? new Node\Identifier('array', $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : 'array'); } protected function reduceRule228() { - $this->semValue = 'callable'; + $this->semValue = ($this->useIdentifierNodes ? new Node\Identifier('callable', $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : 'callable'); } protected function reduceRule229() { diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index 7a4f59d..f8eac91 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -1726,11 +1726,11 @@ class Php7 extends \PhpParser\ParserAbstract } protected function reduceRule228() { - $this->semValue = 'array'; + $this->semValue = ($this->useIdentifierNodes ? new Node\Identifier('array', $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : 'array'); } protected function reduceRule229() { - $this->semValue = 'callable'; + $this->semValue = ($this->useIdentifierNodes ? new Node\Identifier('callable', $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : 'callable'); } protected function reduceRule230() { diff --git a/lib/PhpParser/ParserAbstract.php b/lib/PhpParser/ParserAbstract.php index c035e77..1510b7b 100644 --- a/lib/PhpParser/ParserAbstract.php +++ b/lib/PhpParser/ParserAbstract.php @@ -539,7 +539,13 @@ abstract class ParserAbstract implements Parser } $lowerName = strtolower($name->toString()); - return isset($scalarTypes[$lowerName]) ? $lowerName : $name; + if (!isset($scalarTypes[$lowerName])) { + return $name; + } + + return $this->useIdentifierNodes + ? new Node\Identifier($lowerName, $name->getAttributes()) + : $lowerName; } protected static $specialNames = array( diff --git a/test/code/parser/identMode.test b/test/code/parser/identMode.test index c658d5f..697566e 100644 --- a/test/code/parser/identMode.test +++ b/test/code/parser/identMode.test @@ -16,7 +16,7 @@ class Foo { interface Bar {} trait Baz {} -function foo() {} +function foo(array $x) : callable {} const FOO = 1; @@ -148,8 +148,19 @@ array( name: foo ) params: array( + 0: Param( + type: Identifier( + name: array + ) + byRef: false + variadic: false + name: x + default: null + ) + ) + returnType: Identifier( + name: callable ) - returnType: null stmts: array( ) ) @@ -243,4 +254,34 @@ array( 14: Expr_Variable( name: foo ) +) +----- +