Skip to content

Commit 75906c3

Browse files
committed
Add support for Infection
1 parent 1b8c3fb commit 75906c3

File tree

5 files changed

+449
-0
lines changed

5 files changed

+449
-0
lines changed

src/InfectionLoader.php

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

src/Runners/generic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'clover' => 'CloverLoader',
3333
'codeclimate' => 'CodeClimateLoader',
3434
'humbug' => 'HumbugLoader',
35+
'infecton' => 'InfectionLoader',
3536
'jacoco' => 'JacocoReport',
3637
'phan' => 'PhanTextLoader',
3738
'phanJson' => 'PhanJsonLoader',

src/functions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
function findAutoLoader()
88
{
99
$locations = [
10+
// Vendor directory locally
1011
__DIR__ . '/../vendor/autoload.php',
12+
// Vendor directory when installed with composer
1113
__DIR__ . '/../../../vendor/autoload.php',
14+
// Local install (without composer)
1215
__DIR__ . '/../autoload.php'
1316
];
1417

tests/InfectionLoaderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace exussum12\CoverageChecker\tests;
3+
4+
use exussum12\CoverageChecker\InfectionLoader;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class InfectionLoaderTest extends TestCase
8+
{
9+
10+
public function testCanParseFile()
11+
{
12+
13+
$infection = new InfectionLoader(__DIR__ . '/fixtures/infection-log.txt');
14+
$infection->parseLines();
15+
$file = '/home/scott/code/coverageChecker/src/DiffFilter.php';
16+
17+
$this->assertFalse(
18+
(bool)$infection->getErrorsOnLine(
19+
$file,
20+
20
21+
)
22+
);
23+
$this->assertTrue(
24+
(bool)$infection->getErrorsOnLine(
25+
$file,
26+
21
27+
)
28+
);
29+
}
30+
31+
public function testFileNotFound()
32+
{
33+
$infection = new InfectionLoader(__DIR__ . '/fixtures/infection-log.txt');
34+
$this->assertTrue($infection->handleNotFoundFile());
35+
}
36+
}

0 commit comments

Comments
 (0)