@@ -14,26 +14,42 @@ public function validate(CodeNode $node, IssueCollection $issues): void
14
14
return ;
15
15
}
16
16
17
- try {
18
- set_error_handler (static function ($ errno , $ errstr ) {
19
- throw new \RuntimeException ($ errstr , $ errno );
20
- });
21
-
22
- try {
23
- // Remove first comment only. (No multiline)
24
- $ xml = preg_replace ('#^<!-- .* -->\n# ' , '' , $ node ->getValue ());
25
- if ('' !== $ xml ) {
26
- $ xmlObject = new \SimpleXMLElement ($ xml );
27
- }
28
- } finally {
29
- restore_error_handler ();
17
+ $ count = 0 ;
18
+ $ xml = trim (preg_replace ('#^<!-- .* -->\n# ' , '' , $ node ->getValue (), -1 , $ count ));
19
+ if ('' === $ xml ) {
20
+ return ;
21
+ }
22
+
23
+ libxml_use_internal_errors (true );
24
+ $ doc = new \DOMDocument ();
25
+ $ doc ->loadXML ($ xml );
26
+
27
+ /** @var \LibXMLError[] $errors */
28
+ $ errors = libxml_get_errors ();
29
+ libxml_clear_errors ();
30
+
31
+ if (!$ errors ) {
32
+ return ;
33
+ }
34
+
35
+ $ error = null ;
36
+ foreach ($ errors as $ candidate ) {
37
+ if (preg_match ('#^Namespace prefix .+ is not defined$# ' , $ candidate ->message )) {
38
+ // Ignore take namespace error
39
+ continue ;
30
40
}
31
- } catch (\Throwable $ e ) {
32
- if ('SimpleXMLElement::__construct(): namespace error : Namespace prefix ' === substr ($ e ->getMessage (), 0 , 67 )) {
33
- return ;
41
+
42
+ if (0 === strpos ($ candidate ->message , 'Extra content at the end of the document ' )) {
43
+ // This is because there is not element wrapping the content
44
+ continue ;
34
45
}
35
46
36
- $ issues ->addIssue (new Issue ($ node , $ e ->getMessage (), 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), 0 ));
47
+ $ error = $ candidate ;
48
+ break ;
49
+ }
50
+
51
+ if (null !== $ error ) {
52
+ $ issues ->addIssue (new Issue ($ node , $ error ->message , 'XML syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), $ error ->line + $ count ));
37
53
}
38
54
}
39
55
}
0 commit comments