From 1dea9111a2225e9f6610b9adeb52f155f571d94c Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 25 Jul 2016 14:04:04 +0200 Subject: [PATCH] NodeDumper: Resolve type for include/use as well --- lib/PhpParser/NodeDumper.php | 37 ++++++++++++++++++ test/code/parser/expr/includeAndEval.test | 8 ++-- test/code/parser/stmt/namespace/alias.test | 38 +++++++++---------- test/code/parser/stmt/namespace/groupUse.test | 38 +++++++++---------- .../parser/stmt/namespace/groupUseErrors.test | 4 +- 5 files changed, 81 insertions(+), 44 deletions(-) diff --git a/lib/PhpParser/NodeDumper.php b/lib/PhpParser/NodeDumper.php index 419e5f5..36b36e5 100644 --- a/lib/PhpParser/NodeDumper.php +++ b/lib/PhpParser/NodeDumper.php @@ -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 . ')'; + } } diff --git a/test/code/parser/expr/includeAndEval.test b/test/code/parser/expr/includeAndEval.test index 69458a4..8870ea5 100644 --- a/test/code/parser/expr/includeAndEval.test +++ b/test/code/parser/expr/includeAndEval.test @@ -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( diff --git a/test/code/parser/stmt/namespace/alias.test b/test/code/parser/stmt/namespace/alias.test index 1a1b623..9bb53bd 100644 --- a/test/code/parser/stmt/namespace/alias.test +++ b/test/code/parser/stmt/namespace/alias.test @@ -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 diff --git a/test/code/parser/stmt/namespace/groupUse.test b/test/code/parser/stmt/namespace/groupUse.test index 58834de..6597076 100644 --- a/test/code/parser/stmt/namespace/groupUse.test +++ b/test/code/parser/stmt/namespace/groupUse.test @@ -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( ) ) ) -) +) \ No newline at end of file diff --git a/test/code/parser/stmt/namespace/groupUseErrors.test b/test/code/parser/stmt/namespace/groupUseErrors.test index def1a5d..1f42042 100644 --- a/test/code/parser/stmt/namespace/groupUseErrors.test +++ b/test/code/parser/stmt/namespace/groupUseErrors.test @@ -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