PHP 7: Support nullable types

Using a new NullableType node.
This commit is contained in:
Nikita Popov 2016-07-06 18:34:09 +02:00
parent 7a54aca468
commit 1edf72c040
6 changed files with 1096 additions and 997 deletions

View File

@ -363,6 +363,11 @@ parameter:
{ $$ = Node\Param[parseVar($4), $6, $1, $2, $3]; }
;
type_expr:
type { $$ = $1; }
| '?' type { $$ = Node\NullableType[$2]; }
;
type:
name { $$ = $this->handleBuiltinTypes($1); }
| T_ARRAY { $$ = 'array'; }
@ -371,12 +376,12 @@ type:
optional_param_type:
/* empty */ { $$ = null; }
| type { $$ = $1; }
| type_expr { $$ = $1; }
;
optional_return_type:
/* empty */ { $$ = null; }
| ':' type { $$ = $2; }
| ':' type_expr { $$ = $2; }
;
argument_list:

View File

@ -0,0 +1,26 @@
<?php
namespace PhpParser\Node;
use PhpParser\NodeAbstract;
class NullableType extends NodeAbstract
{
/** @var string|Name Type */
public $type;
/**
* Constructs a nullable type (wrapping another type).
*
* @param string|Name $type Type
* @param array $attributes Additional attributes
*/
public function __construct($type, array $attributes = array()) {
parent::__construct($attributes);
$this->type = $type;
}
public function getSubNodeNames() {
return array('type');
}
}

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,10 @@ class Standard extends PrettyPrinterAbstract
return $node->name . ' = ' . $this->p($node->value);
}
public function pNullableType(Node\NullableType $node) {
return '?' . $node->type;
}
// Names
public function pName(Name $node) {

View File

@ -0,0 +1,47 @@
Nullable types
-----
<?php
function test(?Foo $bar, ?string $foo) : ?Baz {
}
-----
!!php7
array(
0: Stmt_Function(
byRef: false
name: test
params: array(
0: Param(
type: NullableType(
type: Name(
parts: array(
0: Foo
)
)
)
byRef: false
variadic: false
name: bar
default: null
)
1: Param(
type: NullableType(
type: string
)
byRef: false
variadic: false
name: foo
default: null
)
)
returnType: NullableType(
type: Name(
parts: array(
0: Baz
)
)
)
stmts: array(
)
)
)

View File

@ -0,0 +1,11 @@
Nullable types
-----
<?php
function test(?Foo $bar, ?string $foo) : ?Baz
{
}
-----
!!php7
function test(?Foo $bar, ?string $foo) : ?Baz
{
}