44
55namespace Symfony \CodeBlockChecker \Listener ;
66
7- use Doctrine \RST \ErrorManager ;
87use Doctrine \RST \Event \PostNodeCreateEvent ;
98use Doctrine \RST \Nodes \CodeNode ;
9+ use Symfony \CodeBlockChecker \Issue \Issue ;
10+ use Symfony \CodeBlockChecker \Issue \IssueManger ;
1011use Symfony \CodeBlockChecker \Twig \DummyExtension ;
1112use Symfony \Component \Process \Process ;
1213use Symfony \Component \Yaml \Exception \ParseException ;
@@ -26,7 +27,7 @@ class ValidCodeNodeListener
2627 private $ errorManager ;
2728 private $ twig ;
2829
29- public function __construct (ErrorManager $ errorManager )
30+ public function __construct (IssueManger $ errorManager )
3031 {
3132 $ this ->errorManager = $ errorManager ;
3233 }
@@ -70,11 +71,13 @@ private function validatePhp(CodeNode $node)
7071 return ;
7172 }
7273
73- $ this ->errorManager ->error (sprintf (
74- 'Invalid PHP syntax in "%s": %s ' ,
75- $ node ->getEnvironment ()->getCurrentFileName (),
76- str_replace ($ file , 'example.php ' , $ process ->getErrorOutput ())
77- ));
74+ $ line = 0 ;
75+ $ text = str_replace ($ file , 'example.php ' , $ process ->getErrorOutput ());
76+ if (preg_match ('| in example.php on line ([0-9]+)|s ' , $ text , $ matches )) {
77+ $ text = str_replace ($ matches [0 ], '' , $ text );
78+ $ line = (int ) $ matches [1 ];
79+ }
80+ $ this ->errorManager ->addIssue (new Issue ($ text , 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), $ line ));
7881 }
7982
8083 private function validateXml (CodeNode $ node )
@@ -97,11 +100,8 @@ private function validateXml(CodeNode $node)
97100 if ('SimpleXMLElement::__construct(): namespace error : Namespace prefix ' === substr ($ e ->getMessage (), 0 , 67 )) {
98101 return ;
99102 }
100- $ this ->errorManager ->error (sprintf (
101- 'Invalid Xml in "%s": %s ' ,
102- $ node ->getEnvironment ()->getCurrentFileName (),
103- $ e ->getMessage ()
104- ));
103+
104+ $ this ->errorManager ->addIssue (new Issue ($ e ->getMessage (), 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), 0 ));
105105 }
106106 }
107107
@@ -116,11 +116,7 @@ private function validateYaml(CodeNode $node)
116116 return ;
117117 }
118118
119- $ this ->errorManager ->error (sprintf (
120- 'Invalid Yaml in "%s": %s ' ,
121- $ node ->getEnvironment ()->getCurrentFileName (),
122- $ e ->getMessage ()
123- ));
119+ $ this ->errorManager ->addIssue (new Issue ($ e ->getMessage (), 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), 0 ));
124120 }
125121 }
126122
@@ -136,21 +132,16 @@ private function validateTwig(CodeNode $node)
136132 // We cannot parse the TokenStream because we dont have all extensions loaded.
137133 $ this ->twig ->parse ($ tokens );
138134 } catch (SyntaxError $ e ) {
139- $ this ->errorManager ->error (sprintf (
140- 'Invalid Twig syntax: %s ' ,
141- $ e ->getMessage ()
142- ));
135+ $ this ->errorManager ->addIssue (new Issue ($ e ->getMessage (), 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), 0 ));
143136 }
144137 }
145138
146139 private function validateJson (CodeNode $ node )
147140 {
148- $ data = json_decode ($ node ->getValue (), true );
149- if (null === $ data ) {
150- $ this ->errorManager ->error (sprintf (
151- 'Invalid Json in "%s" ' ,
152- $ node ->getEnvironment ()->getCurrentFileName ()
153- ));
141+ try {
142+ $ data = json_decode ($ node ->getValue (), true , 512 , JSON_THROW_ON_ERROR );
143+ } catch (\JsonException $ e ) {
144+ $ this ->errorManager ->addIssue (new Issue ($ e ->getMessage (), 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), 0 ));
154145 }
155146 }
156147}
0 commit comments