Completed
Push — master ( 7f5a29...8ce298 )
by Scott
13s
created

PhpStanLoader::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
/**
5
 * Class PhpStanLoader
6
 * Used for parsing phpstan standard output
7
 * @package exussum12\CoverageChecker
8
 */
9
class PhpStanLoader implements FileChecker
10
{
11
    protected $lineRegex = '/^\s+(?<lineNumber>[0-9]+)/';
12
13
    protected $file;
14
15
    /**
16
     * @var array
17
     */
18
    protected $invalidLines = [];
19
20
    /**
21
     * @param string $filename the path to the phpstan.txt file
22
     */
23
    public function __construct($filename)
24
    {
25
        $this->file = fopen($filename, 'r');
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getLines()
32
    {
33
        $filename = '';
34
        $lineNumber = 0;
35
        while (($line = fgets($this->file)) !== false) {
36
            $filename = $this->checkForFileName($line, $filename);
37
            if ($lineNumber = $this->getLineNumber($line, $lineNumber)) {
38
                if (!isset($this->invalidLines[$filename][$lineNumber])) {
39
                    $this->invalidLines[$filename][$lineNumber] = '';
40
                }
41
42
                $error = $this->getMessage($line);
43
                $this->invalidLines[$filename][$lineNumber] .= $error . ' ';
44
            }
45
        }
46
47
        $this->trimLines();
48
49
        return $this->invalidLines;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function isValidLine($file, $lineNumber)
56
    {
57
        return empty($this->invalidLines[$file][$lineNumber]);
58
    }
59
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function handleNotFoundFile()
65
    {
66
        return true;
67
    }
68
69
    /**
70
     * @param string $line
71
     * @param string $currentFile
72
     * @return string the currentFileName
73
     */
74
    protected function checkForFilename($line, $currentFile)
75
    {
76
        if (strpos($line, " Line ")) {
77
            return trim(str_replace('Line', '',$line));
78
        }
79
        return $currentFile;
80
    }
81
82
    protected function getLineNumber($line, $currentLineNumber)
83
    {
84
       $matches = [];
85
       if (!preg_match($this->lineRegex, $line, $matches)) {
86
           if (preg_match('#^\s{3,}#', $line)) {
87
                return $currentLineNumber;
88
           }
89
90
           return false;
91
       }
92
93
       return $matches['lineNumber'];
94
    }
95
96
    protected function getMessage($line)
97
    {
98
        return trim(preg_replace($this->lineRegex, "", $line));
99
    }
100
101
    protected function trimLines()
102
    {
103
        array_walk_recursive($this->invalidLines, function (&$item) {
104
            if (is_string($item)) {
105
                $item = trim($item);
106
            }
107
        });
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public static function getDescription()
114
    {
115
        return 'Parses the text output of phpstan';
116
    }
117
}
118