11<?php
2+
23namespace Syntax ;
34
45use Symfony \Component \Process \Process ;
@@ -13,7 +14,6 @@ class Php
1314 private $ sourceCharset ;
1415 private $ resultCharset = 'UTF-8 ' ;
1516
16-
1717 public function __construct ()
1818 {
1919 $ this ->tempDirectory = \sys_get_temp_dir ();
@@ -22,6 +22,7 @@ public function __construct()
2222 public function setResultCharset (string $ resultCharset ): self
2323 {
2424 $ this ->resultCharset = $ resultCharset ;
25+
2526 return $ this ;
2627 }
2728
@@ -33,6 +34,7 @@ public function getResultCharset(): string
3334 public function setSourceCharset (?string $ sourceCharset ): self
3435 {
3536 $ this ->sourceCharset = $ sourceCharset ;
37+
3638 return $ this ;
3739 }
3840
@@ -44,6 +46,7 @@ public function getSourceCharset(): ?string
4446 public function setCli (string $ path ): self
4547 {
4648 $ this ->cli = $ path ;
49+
4750 return $ this ;
4851 }
4952
@@ -55,6 +58,7 @@ public function getCli(): string
5558 public function setTempDirectory (string $ path ): self
5659 {
5760 $ this ->tempDirectory = $ path ;
61+
5862 return $ this ;
5963 }
6064
@@ -82,11 +86,11 @@ public function check(string $source): array
8286 throw new \Exception ('Could not close temp file ' );
8387 }
8488
85-
8689 try {
8790 $ result = $ this ->checkFile ($ file );
8891 } catch (\Throwable $ e ) {
8992 \unlink ($ file );
93+
9094 throw $ e ;
9195 }
9296
@@ -95,31 +99,20 @@ public function check(string $source): array
9599 return $ this ->formatCheckOutput ($ result );
96100 }
97101
98- protected function formatCheckOutput (array $ result ): array
99- {
100- if (isset ($ result ['errors ' ])) {
101- \array_walk ($ result ['errors ' ], static function (&$ item ) {
102- $ item ['file ' ] = null ;
103- });
104- }
105-
106- return $ result ;
107- }
108-
109102 public function checkFile (string $ file ): array
110103 {
111104 $ result = $ this ->execute ($ file );
112105
113106 if (0 === $ result ['code ' ]) {
114107 return [
115108 'validity ' => true ,
116- 'errors ' => null
109+ 'errors ' => null ,
117110 ];
118111 }
119112
120113 $ fullMessage = \preg_replace ('/ in (?:.+) on line (?:[0-9]+)$/ ' , '' , $ result ['output ' ]);
121114 \preg_match ('/ on line ([0-9]+)$/ ' , $ result ['output ' ], $ matchLine );
122- $ line = isset ($ matchLine [1 ]) ? (int )($ matchLine [1 ]) : null ;
115+ $ line = isset ($ matchLine [1 ]) ? (int ) ($ matchLine [1 ]) : null ;
123116
124117 [$ type , $ message ] = \explode (': ' , $ fullMessage );
125118
@@ -131,12 +124,28 @@ public function checkFile(string $file): array
131124 'code ' => $ result ['code ' ],
132125 'line ' => $ line ,
133126 'type ' => $ type ,
134- 'message ' => $ this ->convertMessage ($ message )
127+ 'message ' => $ this ->convertMessage ($ message ),
135128 ],
136129 ],
137130 ];
138131 }
139132
133+ public static function formatOutputHelper (string $ source , int $ line , string $ cssCodeClass = 'syntax-code ' , string $ cssCodeCorrectLineClass = 'syntax-correct-line ' , string $ cssCodeIncorrectLineClass = 'syntax-incorrect-line ' ): string
134+ {
135+ return '<div class=" ' .\htmlspecialchars ($ cssCodeClass ).'"><pre><code> ' .self ::formatCode ($ source , $ line , $ cssCodeCorrectLineClass , $ cssCodeIncorrectLineClass ).'</code></pre></div> ' ;
136+ }
137+
138+ protected function formatCheckOutput (array $ result ): array
139+ {
140+ if (isset ($ result ['errors ' ])) {
141+ \array_walk ($ result ['errors ' ], static function (&$ item ) {
142+ $ item ['file ' ] = null ;
143+ });
144+ }
145+
146+ return $ result ;
147+ }
148+
140149 protected function convertMessage (string $ message ): string
141150 {
142151 if (null !== $ this ->getSourceCharset ()) {
@@ -169,11 +178,6 @@ protected function execute(string $file): array
169178 ];
170179 }
171180
172- public static function formatOutputHelper (string $ source , int $ line , string $ cssCodeClass = 'syntax-code ' , string $ cssCodeCorrectLineClass = 'syntax-correct-line ' , string $ cssCodeIncorrectLineClass = 'syntax-incorrect-line ' ): string
173- {
174- return '<div class=" ' . \htmlspecialchars ($ cssCodeClass ) . '"><pre><code> ' . self ::formatCode ($ source , $ line , $ cssCodeCorrectLineClass , $ cssCodeIncorrectLineClass ) . '</code></pre></div> ' ;
175- }
176-
177181 protected static function formatCode (string $ source , int $ line , string $ cssCodeCorrectLineClass = 'syntax-correct-line ' , string $ cssCodeIncorrectLineClass = 'syntax-incorrect-line ' ): string
178182 {
179183 $ array = self ::formatXhtmlHighlight ($ source );
@@ -183,7 +187,7 @@ protected static function formatCode(string $source, int $line, string $cssCodeC
183187 for ($ i = 0 ; $ i < $ all ; ++$ i ) {
184188 $ next = $ i + 1 ;
185189 $ l = \strlen ($ next );
186- $ page .= '<span class=" ' . \htmlspecialchars ($ line === $ next ? $ cssCodeIncorrectLineClass : $ cssCodeCorrectLineClass ) . '"> ' . ($ l < $ len ? \str_repeat ('  ' , $ len - $ l ) : '' ) . $ next . '</span> ' . $ array [$ i ] . "\n" ;
190+ $ page .= '<span class=" ' . \htmlspecialchars ($ line === $ next ? $ cssCodeIncorrectLineClass : $ cssCodeCorrectLineClass ). '"> ' . ($ l < $ len ? \str_repeat ('  ' , $ len - $ l ) : '' ). $ next. '</span> ' . $ array [$ i ]. "\n" ;
187191 }
188192
189193 return $ page ;
0 commit comments