mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 07:08:14 +01:00
Add support for function and constant import (PHP 5.6)
This commit is contained in:
parent
bea89a0bf2
commit
3b7829b011
@ -138,7 +138,9 @@ top_statement:
|
||||
| T_NAMESPACE namespace_name ';' { $$ = Stmt\Namespace_[$2, null]; }
|
||||
| T_NAMESPACE namespace_name '{' top_statement_list '}' { $$ = Stmt\Namespace_[$2, $4]; }
|
||||
| T_NAMESPACE '{' top_statement_list '}' { $$ = Stmt\Namespace_[null, $3]; }
|
||||
| T_USE use_declarations ';' { $$ = Stmt\Use_[$2]; }
|
||||
| T_USE use_declarations ';' { $$ = Stmt\Use_[$2, Stmt\Use_::TYPE_NORMAL]; }
|
||||
| T_USE T_FUNCTION use_declarations ';' { $$ = Stmt\Use_[$3, Stmt\Use_::TYPE_FUNCTION]; }
|
||||
| T_USE T_CONST use_declarations ';' { $$ = Stmt\Use_[$3, Stmt\Use_::TYPE_CONSTANT]; }
|
||||
| T_CONST constant_declaration_list ';' { $$ = Stmt\Const_[$2]; }
|
||||
;
|
||||
|
||||
|
@ -5,19 +5,26 @@ namespace PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
/**
|
||||
* @property int $type Type of alias
|
||||
* @property UseUse[] $uses Aliases
|
||||
*/
|
||||
class Use_ extends Stmt
|
||||
{
|
||||
const TYPE_NORMAL = 1;
|
||||
const TYPE_FUNCTION = 2;
|
||||
const TYPE_CONSTANT = 3;
|
||||
|
||||
/**
|
||||
* Constructs an alias (use) list node.
|
||||
*
|
||||
* @param UseUse[] $uses Aliases
|
||||
* @param int $type Type of alias
|
||||
* @param array $attributes Additional attributes
|
||||
*/
|
||||
public function __construct(array $uses, array $attributes = array()) {
|
||||
public function __construct(array $uses, $type = self::TYPE_NORMAL, array $attributes = array()) {
|
||||
parent::__construct(
|
||||
array(
|
||||
'type' => $type,
|
||||
'uses' => $uses,
|
||||
),
|
||||
$attributes
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -496,7 +496,10 @@ class Standard extends PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
public function pStmt_Use(Stmt\Use_ $node) {
|
||||
return 'use ' . $this->pCommaSeparated($node->uses) . ';';
|
||||
return 'use '
|
||||
. ($node->type === Stmt\Use_::TYPE_FUNCTION ? 'function ' : '')
|
||||
. ($node->type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '')
|
||||
. $this->pCommaSeparated($node->uses) . ';';
|
||||
}
|
||||
|
||||
public function pStmt_UseUse(Stmt\UseUse $node) {
|
||||
|
@ -9,9 +9,16 @@ use F\G as H, J;
|
||||
// evil alias notation - Do Not Use!
|
||||
use \A;
|
||||
use \A as B;
|
||||
|
||||
// function and constant aliases
|
||||
use function foo\bar;
|
||||
use function foo\bar as baz;
|
||||
use const foo\BAR;
|
||||
use const foo\BAR as BAZ;
|
||||
-----
|
||||
array(
|
||||
0: Stmt_Use(
|
||||
type: 1
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
@ -25,6 +32,7 @@ array(
|
||||
)
|
||||
)
|
||||
1: Stmt_Use(
|
||||
type: 1
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
@ -38,6 +46,7 @@ array(
|
||||
)
|
||||
)
|
||||
2: Stmt_Use(
|
||||
type: 1
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
@ -59,6 +68,7 @@ array(
|
||||
)
|
||||
)
|
||||
3: Stmt_Use(
|
||||
type: 1
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
@ -71,6 +81,7 @@ array(
|
||||
)
|
||||
)
|
||||
4: Stmt_Use(
|
||||
type: 1
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
@ -82,4 +93,60 @@ array(
|
||||
)
|
||||
)
|
||||
)
|
||||
5: Stmt_Use(
|
||||
type: 2
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: foo
|
||||
1: bar
|
||||
)
|
||||
)
|
||||
alias: bar
|
||||
)
|
||||
)
|
||||
)
|
||||
6: Stmt_Use(
|
||||
type: 2
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: foo
|
||||
1: bar
|
||||
)
|
||||
)
|
||||
alias: baz
|
||||
)
|
||||
)
|
||||
)
|
||||
7: Stmt_Use(
|
||||
type: 3
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: foo
|
||||
1: BAR
|
||||
)
|
||||
)
|
||||
alias: BAR
|
||||
)
|
||||
)
|
||||
)
|
||||
8: Stmt_Use(
|
||||
type: 3
|
||||
uses: array(
|
||||
0: Stmt_UseUse(
|
||||
name: Name(
|
||||
parts: array(
|
||||
0: foo
|
||||
1: BAR
|
||||
)
|
||||
)
|
||||
alias: BAZ
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
20
test/code/prettyPrinter/alias.test
Normal file
20
test/code/prettyPrinter/alias.test
Normal file
@ -0,0 +1,20 @@
|
||||
Aliases (namespacing)
|
||||
-----
|
||||
<?php
|
||||
|
||||
use A\B;
|
||||
use C\D as E;
|
||||
use F\G as H, J;
|
||||
|
||||
use function foo\bar;
|
||||
use function foo\bar as baz;
|
||||
use const foo\BAR;
|
||||
use const foo\BAR as BAZ;
|
||||
-----
|
||||
use A\B;
|
||||
use C\D as E;
|
||||
use F\G as H, J;
|
||||
use function foo\bar;
|
||||
use function foo\bar as baz;
|
||||
use const foo\BAR;
|
||||
use const foo\BAR as BAZ;
|
Loading…
x
Reference in New Issue
Block a user