Skip to content

Commit 1dea911

Browse files
committed
NodeDumper: Resolve type for include/use as well
1 parent 174e6c3 commit 1dea911

File tree

5 files changed

+81
-44
lines changed

5 files changed

+81
-44
lines changed

lib/PhpParser/NodeDumper.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace PhpParser;
44

5+
use PhpParser\Node\Expr\Include_;
56
use PhpParser\Node\Stmt\Class_;
7+
use PhpParser\Node\Stmt\GroupUse;
8+
use PhpParser\Node\Stmt\Use_;
9+
use PhpParser\Node\Stmt\UseUse;
610

711
class NodeDumper
812
{
@@ -42,6 +46,11 @@ public function dump($node) {
4246
} elseif (is_scalar($value)) {
4347
if ('flags' === $key || 'newModifier' === $key) {
4448
$r .= $this->dumpFlags($value);
49+
} else if ('type' === $key && $node instanceof Include_) {
50+
$r .= $this->dumpIncludeType($value);
51+
} else if ('type' === $key
52+
&& ($node instanceof Use_ || $node instanceof UseUse || $node instanceof GroupUse)) {
53+
$r .= $this->dumpUseType($value);
4554
} else {
4655
$r .= $value;
4756
}
@@ -107,4 +116,32 @@ protected function dumpFlags($flags) {
107116
return $flags;
108117
}
109118
}
119+
120+
protected function dumpIncludeType($type) {
121+
$map = [
122+
Include_::TYPE_INCLUDE => 'TYPE_INCLUDE',
123+
Include_::TYPE_INCLUDE_ONCE => 'TYPE_INCLUDE_ONCE',
124+
Include_::TYPE_REQUIRE => 'TYPE_REQUIRE',
125+
Include_::TYPE_REQUIRE_ONCE => 'TYPE_REQURE_ONCE',
126+
];
127+
128+
if (!isset($map[$type])) {
129+
return $type;
130+
}
131+
return $map[$type] . ' (' . $type . ')';
132+
}
133+
134+
protected function dumpUseType($type) {
135+
$map = [
136+
Use_::TYPE_UNKNOWN => 'TYPE_UNKNOWN',
137+
Use_::TYPE_NORMAL => 'TYPE_NORMAL',
138+
Use_::TYPE_FUNCTION => 'TYPE_FUNCTION',
139+
Use_::TYPE_CONSTANT => 'TYPE_CONSTANT',
140+
];
141+
142+
if (!isset($map[$type])) {
143+
return $type;
144+
}
145+
return $map[$type] . ' (' . $type . ')';
146+
}
110147
}

test/code/parser/expr/includeAndEval.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ array(
1212
expr: Scalar_String(
1313
value: A.php
1414
)
15-
type: 1
15+
type: TYPE_INCLUDE (1)
1616
)
1717
1: Expr_Include(
1818
expr: Scalar_String(
1919
value: A.php
2020
)
21-
type: 2
21+
type: TYPE_INCLUDE_ONCE (2)
2222
)
2323
2: Expr_Include(
2424
expr: Scalar_String(
2525
value: A.php
2626
)
27-
type: 3
27+
type: TYPE_REQUIRE (3)
2828
)
2929
3: Expr_Include(
3030
expr: Scalar_String(
3131
value: A.php
3232
)
33-
type: 4
33+
type: TYPE_REQURE_ONCE (4)
3434
)
3535
4: Expr_Eval(
3636
expr: Scalar_String(

test/code/parser/stmt/namespace/alias.test

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ use const foo\BAR as BAZ;
1818
-----
1919
array(
2020
0: Stmt_Use(
21-
type: 1
21+
type: TYPE_NORMAL (1)
2222
uses: array(
2323
0: Stmt_UseUse(
24-
type: 0
24+
type: TYPE_UNKNOWN (0)
2525
name: Name(
2626
parts: array(
2727
0: A
@@ -33,10 +33,10 @@ array(
3333
)
3434
)
3535
1: Stmt_Use(
36-
type: 1
36+
type: TYPE_NORMAL (1)
3737
uses: array(
3838
0: Stmt_UseUse(
39-
type: 0
39+
type: TYPE_UNKNOWN (0)
4040
name: Name(
4141
parts: array(
4242
0: C
@@ -48,10 +48,10 @@ array(
4848
)
4949
)
5050
2: Stmt_Use(
51-
type: 1
51+
type: TYPE_NORMAL (1)
5252
uses: array(
5353
0: Stmt_UseUse(
54-
type: 0
54+
type: TYPE_UNKNOWN (0)
5555
name: Name(
5656
parts: array(
5757
0: F
@@ -61,7 +61,7 @@ array(
6161
alias: H
6262
)
6363
1: Stmt_UseUse(
64-
type: 0
64+
type: TYPE_UNKNOWN (0)
6565
name: Name(
6666
parts: array(
6767
0: J
@@ -72,10 +72,10 @@ array(
7272
)
7373
)
7474
3: Stmt_Use(
75-
type: 1
75+
type: TYPE_NORMAL (1)
7676
uses: array(
7777
0: Stmt_UseUse(
78-
type: 0
78+
type: TYPE_UNKNOWN (0)
7979
name: Name(
8080
parts: array(
8181
0: A
@@ -89,10 +89,10 @@ array(
8989
)
9090
)
9191
4: Stmt_Use(
92-
type: 1
92+
type: TYPE_NORMAL (1)
9393
uses: array(
9494
0: Stmt_UseUse(
95-
type: 0
95+
type: TYPE_UNKNOWN (0)
9696
name: Name(
9797
parts: array(
9898
0: A
@@ -103,10 +103,10 @@ array(
103103
)
104104
)
105105
5: Stmt_Use(
106-
type: 2
106+
type: TYPE_FUNCTION (2)
107107
uses: array(
108108
0: Stmt_UseUse(
109-
type: 0
109+
type: TYPE_UNKNOWN (0)
110110
name: Name(
111111
parts: array(
112112
0: foo
@@ -121,10 +121,10 @@ array(
121121
)
122122
)
123123
6: Stmt_Use(
124-
type: 2
124+
type: TYPE_FUNCTION (2)
125125
uses: array(
126126
0: Stmt_UseUse(
127-
type: 0
127+
type: TYPE_UNKNOWN (0)
128128
name: Name(
129129
parts: array(
130130
0: foo
@@ -136,10 +136,10 @@ array(
136136
)
137137
)
138138
7: Stmt_Use(
139-
type: 3
139+
type: TYPE_CONSTANT (3)
140140
uses: array(
141141
0: Stmt_UseUse(
142-
type: 0
142+
type: TYPE_UNKNOWN (0)
143143
name: Name(
144144
parts: array(
145145
0: foo
@@ -151,10 +151,10 @@ array(
151151
)
152152
)
153153
8: Stmt_Use(
154-
type: 3
154+
type: TYPE_CONSTANT (3)
155155
uses: array(
156156
0: Stmt_UseUse(
157-
type: 0
157+
type: TYPE_UNKNOWN (0)
158158
name: Name(
159159
parts: array(
160160
0: foo

test/code/parser/stmt/namespace/groupUse.test

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ use A\B\{C\D, function b\c, const D};
1010
-----
1111
array(
1212
0: Stmt_GroupUse(
13-
type: 0
13+
type: TYPE_UNKNOWN (0)
1414
prefix: Name(
1515
parts: array(
1616
0: A
1717
)
1818
)
1919
uses: array(
2020
0: Stmt_UseUse(
21-
type: 1
21+
type: TYPE_NORMAL (1)
2222
name: Name(
2323
parts: array(
2424
0: B
@@ -29,15 +29,15 @@ array(
2929
)
3030
)
3131
1: Stmt_GroupUse(
32-
type: 0
32+
type: TYPE_UNKNOWN (0)
3333
prefix: Name(
3434
parts: array(
3535
0: A
3636
)
3737
)
3838
uses: array(
3939
0: Stmt_UseUse(
40-
type: 1
40+
type: TYPE_NORMAL (1)
4141
name: Name(
4242
parts: array(
4343
0: B
@@ -47,7 +47,7 @@ array(
4747
alias: C
4848
)
4949
1: Stmt_UseUse(
50-
type: 1
50+
type: TYPE_NORMAL (1)
5151
name: Name(
5252
parts: array(
5353
0: D
@@ -58,7 +58,7 @@ array(
5858
)
5959
)
6060
2: Stmt_GroupUse(
61-
type: 0
61+
type: TYPE_UNKNOWN (0)
6262
prefix: Name(
6363
parts: array(
6464
0: A
@@ -67,7 +67,7 @@ array(
6767
)
6868
uses: array(
6969
0: Stmt_UseUse(
70-
type: 1
70+
type: TYPE_NORMAL (1)
7171
name: Name(
7272
parts: array(
7373
0: C
@@ -77,7 +77,7 @@ array(
7777
alias: D
7878
)
7979
1: Stmt_UseUse(
80-
type: 1
80+
type: TYPE_NORMAL (1)
8181
name: Name(
8282
parts: array(
8383
0: E
@@ -88,15 +88,15 @@ array(
8888
)
8989
)
9090
3: Stmt_GroupUse(
91-
type: 2
91+
type: TYPE_FUNCTION (2)
9292
prefix: Name(
9393
parts: array(
9494
0: A
9595
)
9696
)
9797
uses: array(
9898
0: Stmt_UseUse(
99-
type: 0
99+
type: TYPE_UNKNOWN (0)
100100
name: Name(
101101
parts: array(
102102
0: b
@@ -106,7 +106,7 @@ array(
106106
alias: c
107107
)
108108
1: Stmt_UseUse(
109-
type: 0
109+
type: TYPE_UNKNOWN (0)
110110
name: Name(
111111
parts: array(
112112
0: d
@@ -117,15 +117,15 @@ array(
117117
)
118118
)
119119
4: Stmt_GroupUse(
120-
type: 3
120+
type: TYPE_CONSTANT (3)
121121
prefix: Name(
122122
parts: array(
123123
0: A
124124
)
125125
)
126126
uses: array(
127127
0: Stmt_UseUse(
128-
type: 0
128+
type: TYPE_UNKNOWN (0)
129129
name: Name(
130130
parts: array(
131131
0: B
@@ -135,7 +135,7 @@ array(
135135
alias: C
136136
)
137137
1: Stmt_UseUse(
138-
type: 0
138+
type: TYPE_UNKNOWN (0)
139139
name: Name(
140140
parts: array(
141141
0: D
@@ -146,7 +146,7 @@ array(
146146
)
147147
)
148148
5: Stmt_GroupUse(
149-
type: 0
149+
type: TYPE_UNKNOWN (0)
150150
prefix: Name(
151151
parts: array(
152152
0: A
@@ -155,7 +155,7 @@ array(
155155
)
156156
uses: array(
157157
0: Stmt_UseUse(
158-
type: 1
158+
type: TYPE_NORMAL (1)
159159
name: Name(
160160
parts: array(
161161
0: C
@@ -165,7 +165,7 @@ array(
165165
alias: D
166166
)
167167
1: Stmt_UseUse(
168-
type: 2
168+
type: TYPE_FUNCTION (2)
169169
name: Name(
170170
parts: array(
171171
0: b
@@ -175,7 +175,7 @@ array(
175175
alias: c
176176
)
177177
2: Stmt_UseUse(
178-
type: 3
178+
type: TYPE_CONSTANT (3)
179179
name: Name(
180180
parts: array(
181181
0: D
@@ -185,4 +185,4 @@ array(
185185
)
186186
)
187187
)
188-
)
188+
)

test/code/parser/stmt/namespace/groupUseErrors.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use Bar\{Foo};
88
Syntax error, unexpected T_USE, expecting ';' from 4:1 to 4:3
99
array(
1010
0: Stmt_GroupUse(
11-
type: 0
11+
type: TYPE_UNKNOWN (0)
1212
prefix: Name(
1313
parts: array(
1414
0: Bar
1515
)
1616
)
1717
uses: array(
1818
0: Stmt_UseUse(
19-
type: 1
19+
type: TYPE_NORMAL (1)
2020
name: Name(
2121
parts: array(
2222
0: Foo

0 commit comments

Comments
 (0)