Completed
Pull Request — master (#8)
by Scott
02:07
created

PhpStanLoader::getLineNumber()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 9.4285
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
                $this->invalidLines
43
                [$filename]
44
                [$lineNumber] .= $this->getMessage($line) . ' ';
45
            }
46
        }
47
48
        $this->trimLines();
49
50
        return $this->invalidLines;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function isValidLine($file, $lineNumber)
57
    {
58
        return empty($this->invalidLines[$file][$lineNumber]);
59
    }
60
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function handleNotFoundFile()
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * @param string $line
72
     * @param string $currentFile
73
     * @return string the currentFileName
74
     */
75
    protected function checkForFilename($line, $currentFile)
76
    {
77
        if (strpos($line, " Line ")) {
78
            return trim(str_replace('Line', '',$line));
79
        }
80
        return $currentFile;
81
    }
82
83
    protected function getLineNumber($line, $currentLineNumber)
84
    {
85
       $matches = [];
86
       if (!preg_match($this->lineRegex, $line, $matches)) {
87
           if (preg_match('#^\s{3,}#', $line)) {
88
                return $currentLineNumber;
89
           }
90
91
           return false;
92
       }
93
94
       return $matches['lineNumber'];
95
    }
96
97
    protected function getMessage($line)
98
    {
99
        return trim(preg_replace($this->lineRegex, "", $line));
100
    }
101
102
    protected function trimLines()
103
    {
104
        array_walk_recursive($this->invalidLines, function (&$item) {
105
            if (is_string($item)) {
106
                $item = trim($item);
107
            }
108
        });
109
    }
110
}
111