Skip to content

Commit

Permalink
NodeDumper: Resolve type for include/use as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 25, 2016
1 parent 174e6c3 commit 1dea911
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 44 deletions.
37 changes: 37 additions & 0 deletions lib/PhpParser/NodeDumper.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -42,6 +46,11 @@ public function dump($node) {
} 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;
}
Expand Down Expand Up @@ -107,4 +116,32 @@ protected function dumpFlags($flags) {
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 . ')';
}
}
8 changes: 4 additions & 4 deletions test/code/parser/expr/includeAndEval.test
Expand Up @@ -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(
Expand Down
38 changes: 19 additions & 19 deletions test/code/parser/stmt/namespace/alias.test
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -61,7 +61,7 @@ array(
alias: H
)
1: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: J
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions test/code/parser/stmt/namespace/groupUse.test
Expand Up @@ -10,15 +10,15 @@ 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
)
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: B
Expand All @@ -29,15 +29,15 @@ array(
)
)
1: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
)
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: B
Expand All @@ -47,7 +47,7 @@ array(
alias: C
)
1: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: D
Expand All @@ -58,7 +58,7 @@ array(
)
)
2: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
Expand All @@ -67,7 +67,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: C
Expand All @@ -77,7 +77,7 @@ array(
alias: D
)
1: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: E
Expand All @@ -88,15 +88,15 @@ array(
)
)
3: Stmt_GroupUse(
type: 2
type: TYPE_FUNCTION (2)
prefix: Name(
parts: array(
0: A
)
)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: b
Expand All @@ -106,7 +106,7 @@ array(
alias: c
)
1: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: d
Expand All @@ -117,15 +117,15 @@ array(
)
)
4: Stmt_GroupUse(
type: 3
type: TYPE_CONSTANT (3)
prefix: Name(
parts: array(
0: A
)
)
uses: array(
0: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: B
Expand All @@ -135,7 +135,7 @@ array(
alias: C
)
1: Stmt_UseUse(
type: 0
type: TYPE_UNKNOWN (0)
name: Name(
parts: array(
0: D
Expand All @@ -146,7 +146,7 @@ array(
)
)
5: Stmt_GroupUse(
type: 0
type: TYPE_UNKNOWN (0)
prefix: Name(
parts: array(
0: A
Expand All @@ -155,7 +155,7 @@ array(
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: C
Expand All @@ -165,7 +165,7 @@ array(
alias: D
)
1: Stmt_UseUse(
type: 2
type: TYPE_FUNCTION (2)
name: Name(
parts: array(
0: b
Expand All @@ -175,7 +175,7 @@ array(
alias: c
)
2: Stmt_UseUse(
type: 3
type: TYPE_CONSTANT (3)
name: Name(
parts: array(
0: D
Expand All @@ -185,4 +185,4 @@ array(
)
)
)
)
)
4 changes: 2 additions & 2 deletions test/code/parser/stmt/namespace/groupUseErrors.test
Expand Up @@ -8,15 +8,15 @@ 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
)
)
uses: array(
0: Stmt_UseUse(
type: 1
type: TYPE_NORMAL (1)
name: Name(
parts: array(
0: Foo
Expand Down

0 comments on commit 1dea911

Please sign in to comment.