@@ -12,19 +12,13 @@ class PhpValidator implements Validator
12
12
public function validate (CodeNode $ node , IssueCollection $ issues ): void
13
13
{
14
14
$ 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 ' ])) {
16
16
return ;
17
17
}
18
18
19
19
$ 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
- }
24
20
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 ));
28
22
29
23
$ process = new Process (['php ' , '-l ' , $ file ]);
30
24
$ process ->run ();
@@ -40,4 +34,26 @@ public function validate(CodeNode $node, IssueCollection $issues): void
40
34
}
41
35
$ issues ->addIssue (new Issue ($ node , $ text , 'Invalid syntax ' , $ node ->getEnvironment ()->getCurrentFileName (), $ line ));
42
36
}
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
+ }
43
59
}
0 commit comments