mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
PHP 7: Support nullable types
Using a new NullableType node.
This commit is contained in:
parent
7a54aca468
commit
1edf72c040
@ -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:
|
||||
|
26
lib/PhpParser/Node/NullableType.php
Normal file
26
lib/PhpParser/Node/NullableType.php
Normal 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
@ -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) {
|
||||
|
47
test/code/parser/stmt/function/nullableTypes.test
Normal file
47
test/code/parser/stmt/function/nullableTypes.test
Normal 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(
|
||||
)
|
||||
)
|
||||
)
|
11
test/code/prettyPrinter/stmt/nullable_types.test
Normal file
11
test/code/prettyPrinter/stmt/nullable_types.test
Normal file
@ -0,0 +1,11 @@
|
||||
Nullable types
|
||||
-----
|
||||
<?php
|
||||
function test(?Foo $bar, ?string $foo) : ?Baz
|
||||
{
|
||||
}
|
||||
-----
|
||||
!!php7
|
||||
function test(?Foo $bar, ?string $foo) : ?Baz
|
||||
{
|
||||
}
|
Loading…
Reference in New Issue
Block a user