File tree Expand file tree Collapse file tree 3 files changed +122
-2
lines changed Expand file tree Collapse file tree 3 files changed +122
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace PhpParser ;
4
4
5
- class Comment
5
+ class Comment implements \JsonSerializable
6
6
{
7
7
protected $ text ;
8
8
protected $ line ;
@@ -126,4 +126,15 @@ private function getShortestWhitespacePrefixLen($str) {
126
126
}
127
127
return $ shortestPrefixLen ;
128
128
}
129
+
130
+ public function jsonSerialize () {
131
+ // Technically not a node, but we make it look like one anyway
132
+ $ type = $ this instanceof Comment \Doc ? 'Comment_Doc ' : 'Comment ' ;
133
+ return [
134
+ 'nodeType ' => $ type ,
135
+ 'text ' => $ this ->text ,
136
+ 'line ' => $ this ->line ,
137
+ 'filePos ' => $ this ->filePos ,
138
+ ];
139
+ }
129
140
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace PhpParser ;
4
4
5
- abstract class NodeAbstract implements Node
5
+ abstract class NodeAbstract implements Node, \JsonSerializable
6
6
{
7
7
protected $ attributes ;
8
8
@@ -82,4 +82,8 @@ public function &getAttribute($key, $default = null) {
82
82
public function getAttributes () {
83
83
return $ this ->attributes ;
84
84
}
85
+
86
+ public function jsonSerialize () {
87
+ return ['nodeType ' => $ this ->getType ()] + get_object_vars ($ this );
88
+ }
85
89
}
Original file line number Diff line number Diff line change @@ -144,4 +144,109 @@ public function testAttributes() {
144
144
$ node ->getAttributes ()
145
145
);
146
146
}
147
+
148
+ public function testJsonSerialization () {
149
+ $ code = <<<'PHP'
150
+ <?php
151
+ // comment
152
+ /** doc comment */
153
+ function functionName(&$a = 0, $b = 1.0) {
154
+ echo 'Foo';
155
+ }
156
+ PHP;
157
+ $ expected = <<<'JSON'
158
+ [
159
+ {
160
+ "nodeType": "Stmt_Function",
161
+ "byRef": false,
162
+ "name": "functionName",
163
+ "params": [
164
+ {
165
+ "nodeType": "Param",
166
+ "type": null,
167
+ "byRef": true,
168
+ "variadic": false,
169
+ "name": "a",
170
+ "default": {
171
+ "nodeType": "Scalar_LNumber",
172
+ "value": 0,
173
+ "attributes": {
174
+ "startLine": 4,
175
+ "endLine": 4,
176
+ "kind": 10
177
+ }
178
+ },
179
+ "attributes": {
180
+ "startLine": 4,
181
+ "endLine": 4
182
+ }
183
+ },
184
+ {
185
+ "nodeType": "Param",
186
+ "type": null,
187
+ "byRef": false,
188
+ "variadic": false,
189
+ "name": "b",
190
+ "default": {
191
+ "nodeType": "Scalar_DNumber",
192
+ "value": 1,
193
+ "attributes": {
194
+ "startLine": 4,
195
+ "endLine": 4
196
+ }
197
+ },
198
+ "attributes": {
199
+ "startLine": 4,
200
+ "endLine": 4
201
+ }
202
+ }
203
+ ],
204
+ "returnType": null,
205
+ "stmts": [
206
+ {
207
+ "nodeType": "Stmt_Echo",
208
+ "exprs": [
209
+ {
210
+ "nodeType": "Scalar_String",
211
+ "value": "Foo",
212
+ "attributes": {
213
+ "startLine": 5,
214
+ "endLine": 5,
215
+ "kind": 1
216
+ }
217
+ }
218
+ ],
219
+ "attributes": {
220
+ "startLine": 5,
221
+ "endLine": 5
222
+ }
223
+ }
224
+ ],
225
+ "attributes": {
226
+ "startLine": 4,
227
+ "comments": [
228
+ {
229
+ "nodeType": "Comment",
230
+ "text": "\/\/ comment\r\n",
231
+ "line": 2,
232
+ "filePos": 7
233
+ },
234
+ {
235
+ "nodeType": "Comment_Doc",
236
+ "text": "\/** doc comment *\/",
237
+ "line": 3,
238
+ "filePos": 19
239
+ }
240
+ ],
241
+ "endLine": 6
242
+ }
243
+ }
244
+ ]
245
+ JSON;
246
+
247
+ $ parser = new Parser \Php7 (new Lexer ());
248
+ $ stmts = $ parser ->parse ($ code );
249
+ $ json = json_encode ($ stmts , JSON_PRETTY_PRINT );
250
+ $ this ->assertEquals (canonicalize ($ expected ), canonicalize ($ json ));
251
+ }
147
252
}
You can’t perform that action at this time.
0 commit comments