Test Setup Failed
Pull Request — master (#11)
by Benjamin
11:55 queued 02:07
created

Cli::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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>
0 ignored issues
show
Bug introduced by
The type Bdelespierre\LaravelBladeLinter\Backend\list was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(new Bdelesp...>getPathname(), $line)) returns the type array<integer,Bdelespier...ladeLinter\ErrorRecord> which is incompatible with the documented return type Bdelespierre\LaravelBladeLinter\Backend\list.
Loading history...
31
                new ErrorRecord(
32
                    trim($message),
33
                    $file->getPathname(),
34
                    $line
35
                ),
36
            ];
37
        }
38
39
        return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type Bdelespierre\LaravelBladeLinter\Backend\list.
Loading history...
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