1 | <?php |
||
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) |
||
82 | |||
83 | protected function getLineNumber($line, $currentLineNumber) |
||
84 | { |
||
85 | $matches = []; |
||
86 | if (!preg_match($this->lineRegex, $line, $matches)) { |
||
96 | |||
97 | protected function getMessage($line) |
||
101 | |||
102 | protected function trimLines() |
||
110 | } |
||
111 |