1 | <?php |
||
2 | namespace exussum12\CoverageChecker\Loaders; |
||
3 | |||
4 | use exussum12\CoverageChecker\FileChecker; |
||
5 | |||
6 | class Infection implements FileChecker |
||
7 | { |
||
8 | |||
9 | protected $file; |
||
10 | protected $errors = []; |
||
11 | |||
12 | protected $errorTypes = [ |
||
13 | 'Escaped mutants', |
||
14 | 'Errors mutants', |
||
15 | 'Not covered mutants', |
||
16 | ]; |
||
17 | |||
18 | protected $currentFile; |
||
19 | |||
20 | protected $currentLine; |
||
21 | |||
22 | protected $partialError; |
||
23 | |||
24 | protected $currentType; |
||
25 | |||
26 | public function __construct($filePath) |
||
27 | { |
||
28 | $this->file = fopen($filePath, 'r'); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return array the list of files from this change |
||
33 | */ |
||
34 | public function parseLines(): array |
||
35 | { |
||
36 | $this->currentFile = ''; |
||
37 | $this->currentLine = 0; |
||
38 | $this->partialError = ''; |
||
39 | $this->currentType = ''; |
||
40 | |||
41 | while (($line = fgets($this->file)) !== false) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
42 | $this->handleLine($line); |
||
43 | } |
||
44 | // the last error in the file |
||
45 | $this->addError(); |
||
46 | |||
47 | return array_keys($this->errors); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Method to determine if the line is valid in the context |
||
52 | * returning null does not include the line in the stats |
||
53 | * Returns an array containing errors on a certain line - empty array means no errors |
||
54 | * |
||
55 | * @return array|null |
||
56 | */ |
||
57 | public function getErrorsOnLine(string $file, int $lineNumber) |
||
58 | { |
||
59 | if (!isset($this->errors[$file][$lineNumber])) { |
||
60 | return []; |
||
61 | } |
||
62 | |||
63 | return $this->errors[$file][$lineNumber]; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Method to determine what happens to files which have not been found |
||
68 | * true adds as covered |
||
69 | * false adds as uncovered |
||
70 | * null does not include the file in the stats |
||
71 | * @return bool|null |
||
72 | */ |
||
73 | public function handleNotFoundFile() |
||
74 | { |
||
75 | return true; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Shows the description of the class, used for explaining why |
||
80 | * this checker would be used |
||
81 | * @return string |
||
82 | */ |
||
83 | public static function getDescription(): string |
||
84 | { |
||
85 | return 'Parses the infection text log format'; |
||
86 | } |
||
87 | |||
88 | protected function updateType($line) |
||
89 | { |
||
90 | $matches = []; |
||
91 | if (preg_match('/^([a-z ]+):$/i', $line, $matches)) { |
||
92 | $this->addError(); |
||
93 | $this->currentFile = ''; |
||
94 | $this->currentLine = ''; |
||
95 | $this->partialError = ''; |
||
96 | $this->currentType = $matches[1]; |
||
97 | |||
98 | return true; |
||
99 | } |
||
100 | |||
101 | return false; |
||
102 | } |
||
103 | |||
104 | protected function updateFile($line) |
||
105 | { |
||
106 | $matches = []; |
||
107 | if (preg_match('/^[0-9]+\) (.*?):([0-9]+) (.*)/i', $line, $matches)) { |
||
108 | $this->addError(); |
||
109 | $this->currentFile = $matches[1]; |
||
110 | $this->currentLine = $matches[2]; |
||
111 | $this->partialError = ''; |
||
112 | |||
113 | return true; |
||
114 | } |
||
115 | |||
116 | return false; |
||
117 | } |
||
118 | |||
119 | protected function addError() |
||
120 | { |
||
121 | if (!($this->currentFile && $this->currentLine)) { |
||
122 | return; |
||
123 | } |
||
124 | |||
125 | if (!in_array($this->currentType, $this->errorTypes)) { |
||
126 | return; |
||
127 | } |
||
128 | |||
129 | $this->errors |
||
130 | [$this->currentFile] |
||
131 | [$this->currentLine][] = $this->currentType . $this->partialError; |
||
132 | } |
||
133 | |||
134 | protected function handleLine($line) |
||
135 | { |
||
136 | if ($this->updateType($line)) { |
||
137 | return; |
||
138 | } |
||
139 | |||
140 | if ($this->updateFile($line)) { |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | $this->partialError .= $line; |
||
145 | } |
||
146 | } |
||
147 |