@@ -12,19 +12,13 @@ class PhpValidator implements Validator
1212 public function validate (CodeNode $ node , IssueCollection $ issues ): void
1313 {
1414 $ language = $ node ->getLanguage () ?? ($ node ->isRaw () ? null : 'php ' );
15- if (!in_array ($ language , ['php ' , 'php-symfony ' , 'php-standalone ' , 'php-annotations ' ])) {
15+ if (!in_array ($ language , ['php ' , 'php-symfony ' , 'php-standalone ' , 'php-annotations ' , ' html+php ' ])) {
1616 return ;
1717 }
1818
1919 $ file = sys_get_temp_dir ().'/ ' .uniqid ('doc_builder ' , true ).'.php ' ;
20- $ contents = $ node ->getValue ();
21- if (!preg_match ('#(class|interface) [a-zA-Z]+#s ' , $ contents ) && preg_match ('#(public|protected|private)( static)? (\$[a-z]+|function)#s ' , $ contents )) {
22- $ contents = 'class Foobar { ' .$ contents .'} ' ;
23- }
2420
25- // Allow us to use "..." as a placeholder
26- $ contents = str_replace ('... ' , 'null ' , $ contents );
27- file_put_contents ($ file , '<?php ' .PHP_EOL .$ contents );
21+ file_put_contents ($ file , $ this ->getContents ($ node , $ language ));
2822
2923 $ process = new Process (['php ' , '-l ' , $ file ]);
3024 $ process ->run ();
@@ -40,4 +34,26 @@ public function validate(CodeNode $node, IssueCollection $issues): void
4034 }
4135 $ issues ->addIssue (new Issue ($ node , $ text , 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), $ line ));
4236 }
37+
38+ private function getContents (CodeNode $ node , string $ language ): string
39+ {
40+ $ contents = $ node ->getValue ();
41+ if ('html+php ' === $ language ) {
42+ return $ contents ;
43+ }
44+
45+ if (!preg_match ('#(class|interface) [a-zA-Z]+#s ' , $ contents ) && preg_match ('#(public|protected|private)( static)? (\$[a-z]+|function)#s ' , $ contents )) {
46+ $ contents = 'class Foobar { ' .$ contents .'} ' ;
47+ }
48+
49+ // Allow us to use "..." as a placeholder
50+ $ contents = str_replace ('... ' , 'null ' , $ contents );
51+
52+ $ lines = explode (PHP_EOL , $ contents );
53+ if (!str_contains ($ lines [0 ] ?? '' , '<?php ' ) && !str_contains ($ lines [1 ] ?? '' , '<?php ' ) && !str_contains ($ lines [2 ] ?? '' , '<?php ' )) {
54+ $ contents = '<?php ' .PHP_EOL .$ contents ;
55+ }
56+
57+ return $ contents ;
58+ }
4359}
0 commit comments