1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

NodeDumper: Resolve type for include/use as well

This commit is contained in:
Nikita Popov 2016-07-25 14:04:04 +02:00
parent 174e6c3cab
commit 1dea9111a2
5 changed files with 81 additions and 44 deletions

View File

@ -2,7 +2,11 @@
namespace PhpParser;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
class NodeDumper
{
@ -42,6 +46,11 @@ class NodeDumper
} elseif (is_scalar($value)) {
if ('flags' === $key || 'newModifier' === $key) {
$r .= $this->dumpFlags($value);
} else if ('type' === $key && $node instanceof Include_) {
$r .= $this->dumpIncludeType($value);
} else if ('type' === $key
&& ($node instanceof Use_ || $node instanceof UseUse || $node instanceof GroupUse)) {
$r .= $this->dumpUseType($value);
} else {
$r .= $value;
}
@ -107,4 +116,32 @@ class NodeDumper
return $flags;
}
}
protected function dumpIncludeType($type) {
$map = [
Include_::TYPE_INCLUDE => 'TYPE_INCLUDE',
Include_::TYPE_INCLUDE_ONCE => 'TYPE_INCLUDE_ONCE',
Include_::TYPE_REQUIRE => 'TYPE_REQUIRE',
Include_::TYPE_REQUIRE_ONCE => 'TYPE_REQURE_ONCE',
];
if (!isset($map[$type])) {
return $type;
}
return $map[$type] . ' (' . $type . ')';
}
protected function dumpUseType($type) {
$map = [
Use_::TYPE_UNKNOWN => 'TYPE_UNKNOWN',
Use_::TYPE_NORMAL => 'TYPE_NORMAL',
Use_::TYPE_FUNCTION => 'TYPE_FUNCTION',
Use_::TYPE_CONSTANT => 'TYPE_CONSTANT',
];
if (!isset($map[$type])) {
return $type;
}
return $map[$type] . ' (' . $type . ')';
}
}

View File

@ -12,25 +12,25 @@ array(
expr: Scalar_String(
value: A.php
)
type: 1
type: TYPE_INCLUDE (1)
)
1: Expr_Include(
expr: Scalar_String(
value: A.php
)
type: 2
type: TYPE_INCLUDE_ONCE (2)
)
2: Expr_Include(
expr: Scalar_String(
value: A.php
)
type: 3
type: TYPE_REQUIRE (3)
)
3: Expr_Include(
expr: Scalar_String(
value: A.php
)
type: 4
type: TYPE_REQURE_ONCE (4)
)
4: Expr_Eval(
expr: Scalar_String(

View File

@ -18,10 +18,10 @@ use const foo\BAR as BAZ;
-----
array(
0: Stmt_Use(
type: 1
type: TYPE_NORMAL (1)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: A
@ -33,10 +33,10 @@ array(
)
)
1: Stmt_Use(
type: 1
type: TYPE_NORMAL (1)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: C
@ -48,10 +48,10 @@ array(
)
)
2: Stmt_Use(
type: 1
type: TYPE_NORMAL (1)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: F
@ -61,7 +61,7 @@ array(
alias: H
)
1: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: J
@ -72,10 +72,10 @@ array(
)
)
3: Stmt_Use(
type: 1
type: TYPE_NORMAL (1)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: A
@ -89,10 +89,10 @@ array(
)
)
4: Stmt_Use(
type: 1
type: TYPE_NORMAL (1)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: A
@ -103,10 +103,10 @@ array(
)
)
5: Stmt_Use(
type: 2
type: TYPE_FUNCTION (2)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: foo
@ -121,10 +121,10 @@ array(
)
)
6: Stmt_Use(
type: 2
type: TYPE_FUNCTION (2)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: foo
@ -136,10 +136,10 @@ array(
)
)
7: Stmt_Use(
type: 3
type: TYPE_CONSTANT (3)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: foo
@ -151,10 +151,10 @@ array(
)
)
8: Stmt_Use(
type: 3
type: TYPE_CONSTANT (3)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: foo

View File

@ -10,7 +10,7 @@ use A\B\{C\D, function b\c, const D};
-----
array(
0: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
@ -18,7 +18,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: B
@ -29,7 +29,7 @@ array(
)
)
1: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
@ -37,7 +37,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: B
@ -47,7 +47,7 @@ array(
alias: C
)
1: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: D
@ -58,7 +58,7 @@ array(
)
)
2: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
@ -67,7 +67,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: C
@ -77,7 +77,7 @@ array(
alias: D
)
1: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: E
@ -88,7 +88,7 @@ array(
)
)
3: Stmt_GroupUse(
type: 2
type: TYPE_FUNCTION (2)
prefix: Name(
parts: array(
0: A
@ -96,7 +96,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: b
@ -106,7 +106,7 @@ array(
alias: c
)
1: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: d
@ -117,7 +117,7 @@ array(
)
)
4: Stmt_GroupUse(
type: 3
type: TYPE_CONSTANT (3)
prefix: Name(
parts: array(
0: A
@ -125,7 +125,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: B
@ -135,7 +135,7 @@ array(
alias: C
)
1: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: D
@ -146,7 +146,7 @@ array(
)
)
5: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
@ -155,7 +155,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: C
@ -165,7 +165,7 @@ array(
alias: D
)
1: Stmt_UseUse(
type: 2
type: TYPE_FUNCTION (2)
name: Name(
parts: array(
0: b
@ -175,7 +175,7 @@ array(
alias: c
)
2: Stmt_UseUse(
type: 3
type: TYPE_CONSTANT (3)
name: Name(
parts: array(
0: D
@ -185,4 +185,4 @@ array(
)
)
)
)
)

View File

@ -8,7 +8,7 @@ use Bar\{Foo};
Syntax error, unexpected T_USE, expecting ';' from 4:1 to 4:3
array(
0: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: Bar
@ -16,7 +16,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: Foo