File tree 3 files changed +17
-5
lines changed 3 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -57,12 +57,15 @@ public function toXML(TokenCollection $tokens): string {
57
57
$ this ->writer ->startDocument ();
58
58
$ this ->writer ->startElement ('source ' );
59
59
$ this ->writer ->writeAttribute ('xmlns ' , $ this ->xmlns ->asString ());
60
- $ this ->writer ->startElement ('line ' );
61
- $ this ->writer ->writeAttribute ('no ' , '1 ' );
62
60
63
- $ this ->previousToken = $ tokens [0 ];
64
- foreach ($ tokens as $ token ) {
65
- $ this ->addToken ($ token );
61
+ if (count ($ tokens ) > 0 ) {
62
+ $ this ->writer ->startElement ('line ' );
63
+ $ this ->writer ->writeAttribute ('no ' , '1 ' );
64
+
65
+ $ this ->previousToken = $ tokens [0 ];
66
+ foreach ($ tokens as $ token ) {
67
+ $ this ->addToken ($ token );
68
+ }
66
69
}
67
70
68
71
$ this ->writer ->endElement ();
Original file line number Diff line number Diff line change @@ -40,4 +40,11 @@ public function testCanBeSerializedToXmlWithCustomNamespace() {
40
40
$ this ->assertEquals ($ expected , $ serializer ->toXML ($ this ->tokens ));
41
41
}
42
42
43
+ public function testEmptyCollectionCreatesEmptyDocument () {
44
+ $ expected = file_get_contents (__DIR__ . '/_files/empty.xml ' );
45
+
46
+ $ serializer = new XMLSerializer ();
47
+ $ this ->assertEquals ($ expected , $ serializer ->toXML ((new TokenCollection ())));
48
+ }
49
+
43
50
}
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" ?>
2
+ <source xmlns =" https://github.com/theseer/tokenizer" />
You can’t perform that action at this time.
0 commit comments