Skip to content

Commit 7dae6c7

Browse files
committed
Implement JsonSerializable for Nodes and Comments
Exposes the properties and adds an additional nodeType property.
1 parent 2b209aa commit 7dae6c7

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

lib/PhpParser/Comment.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PhpParser;
44

5-
class Comment
5+
class Comment implements \JsonSerializable
66
{
77
protected $text;
88
protected $line;
@@ -126,4 +126,15 @@ private function getShortestWhitespacePrefixLen($str) {
126126
}
127127
return $shortestPrefixLen;
128128
}
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+
}
129140
}

lib/PhpParser/NodeAbstract.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PhpParser;
44

5-
abstract class NodeAbstract implements Node
5+
abstract class NodeAbstract implements Node, \JsonSerializable
66
{
77
protected $attributes;
88

@@ -82,4 +82,8 @@ public function &getAttribute($key, $default = null) {
8282
public function getAttributes() {
8383
return $this->attributes;
8484
}
85+
86+
public function jsonSerialize() {
87+
return ['nodeType' => $this->getType()] + get_object_vars($this);
88+
}
8589
}

test/PhpParser/NodeAbstractTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,109 @@ public function testAttributes() {
144144
$node->getAttributes()
145145
);
146146
}
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+
}
147252
}

0 commit comments

Comments
 (0)