1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Bdelespierre\LaravelBladeLinter\Backend; |
5
|
|
|
|
6
|
|
|
use Bdelespierre\LaravelBladeLinter\Backend; |
7
|
|
|
use Bdelespierre\LaravelBladeLinter\ErrorRecord; |
8
|
|
|
|
9
|
|
|
final class Cli implements Backend |
10
|
|
|
{ |
11
|
|
|
private const REGEX = '/ in Standard input code on line (\d+)[\s\r\n]*/'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param \SplFileInfo $file |
15
|
|
|
* @param string $code |
16
|
|
|
* @return list<ErrorRecord> |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
public function analyze(\SplFileInfo $file, string $code): array |
19
|
|
|
{ |
20
|
|
|
$message = ''; |
21
|
|
|
$result = $this->lint($code, $output, $message); |
22
|
|
|
|
23
|
|
|
if (! $result) { |
24
|
|
|
$line = null; |
25
|
|
|
if (false !== preg_match(self::REGEX, trim($message), $matches)) { |
26
|
|
|
$line = isset($matches[1]) ? (int) $matches[1] : null; |
27
|
|
|
$message = preg_replace(self::REGEX, '', $message); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return [ |
|
|
|
|
31
|
|
|
new ErrorRecord( |
32
|
|
|
trim($message), |
33
|
|
|
$file->getPathname(), |
34
|
|
|
$line |
35
|
|
|
), |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return []; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function name(): string |
43
|
|
|
{ |
44
|
|
|
return 'cli'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function lint(string $code, ?string &$stdout = "", ?string &$stderr = ""): bool |
48
|
|
|
{ |
49
|
|
|
$descriptors = [ |
50
|
|
|
0 => ["pipe", "r"], // read from stdin |
51
|
|
|
1 => ["pipe", "w"], // write to stdout |
52
|
|
|
2 => ["pipe", "w"], // write to stderr |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
// open linter process (php -l) |
56
|
|
|
$process = proc_open('php -d display_errors=stderr -l', $descriptors, $pipes); |
57
|
|
|
|
58
|
|
|
if (! is_resource($process)) { |
59
|
|
|
throw new \RuntimeException("unable to open process 'php -l'"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
fwrite($pipes[0], $code); |
63
|
|
|
fclose($pipes[0]); |
64
|
|
|
|
65
|
|
|
$stdout = stream_get_contents($pipes[1]); |
66
|
|
|
fclose($pipes[1]); |
67
|
|
|
|
68
|
|
|
$stderr = stream_get_contents($pipes[2]); |
69
|
|
|
fclose($pipes[2]); |
70
|
|
|
|
71
|
|
|
// it is important that you close any pipes before calling |
72
|
|
|
// proc_close in order to avoid a deadlock |
73
|
|
|
$retval = proc_close($process); |
74
|
|
|
|
75
|
|
|
// zero actually means "no error" |
76
|
|
|
return $retval === 0; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths