Skip to content

Commit 6ba04bb

Browse files
authored
Add buddy (#37)
* Add buddy * PSR-2 * fix for older php
1 parent 4f0dda8 commit 6ba04bb

File tree

5 files changed

+107
-73
lines changed

5 files changed

+107
-73
lines changed

src/BuddyLoader.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace exussum12\CoverageChecker;
3+
4+
/**
5+
* Class BuddyLoader
6+
* Used for parsing magic number reports from buddy
7+
* @package exussum12\CoverageChecker
8+
*/
9+
class BuddyLoader extends Generic implements FileChecker
10+
{
11+
protected $lineMatch = '#^(?P<fileName>.*?):(?P<lineNumber>[0-9]+) \| (?P<message>.*)$#';
12+
13+
public static function getDescription()
14+
{
15+
return 'Parses buddy (magic number detection) output';
16+
}
17+
}

src/Generic.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
namespace exussum12\CoverageChecker;
3+
4+
/**
5+
* Class Generic
6+
* Used for parsing output on a single line
7+
* @package exussum12\CoverageChecker
8+
*/
9+
abstract class Generic
10+
{
11+
protected $lineMatch = '';
12+
13+
/**
14+
* @var string
15+
*/
16+
protected $file;
17+
18+
/**
19+
* @var array
20+
*/
21+
protected $errors = [];
22+
23+
/**
24+
* PhanJsonLoader constructor.
25+
* @param string $file the path to the file containing phan output
26+
*/
27+
public function __construct($file)
28+
{
29+
$this->file = $file;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function parseLines()
36+
{
37+
$handle = fopen($this->file, 'r');
38+
while (($line = fgets($handle)) !== false) {
39+
if (!$this->checkForFile($line)) {
40+
continue;
41+
}
42+
43+
$this->addError($line);
44+
}
45+
46+
return array_keys($this->errors);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getErrorsOnLine($file, $lineNumber)
53+
{
54+
$errors = [];
55+
if (isset($this->errors[$file][$lineNumber])) {
56+
$errors = $this->errors[$file][$lineNumber];
57+
}
58+
59+
return $errors;
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function handleNotFoundFile()
66+
{
67+
return true;
68+
}
69+
70+
71+
private function checkForFile($line)
72+
{
73+
return preg_match($this->lineMatch, $line);
74+
}
75+
76+
private function addError($line)
77+
{
78+
$matches = [];
79+
if (preg_match($this->lineMatch, $line, $matches)) {
80+
$this->errors
81+
[$matches['fileName']]
82+
[$matches['lineNumber']][] = trim($matches['message']);
83+
}
84+
}
85+
}

src/PhanTextLoader.php

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,84 +6,15 @@
66
* Used for parsing phan text output
77
* @package exussum12\CoverageChecker
88
*/
9-
class PhanTextLoader implements FileChecker
9+
class PhanTextLoader extends Generic implements FileChecker
1010
{
1111
protected $lineMatch = '#(?:\./)?(?P<fileName>.*?):(?P<lineNumber>[0-9]+)(?P<message>.*)#';
1212

13-
/**
14-
* @var string
15-
*/
16-
protected $file;
17-
18-
/**
19-
* @var array
20-
*/
21-
protected $errors = [];
22-
23-
/**
24-
* PhanJsonLoader constructor.
25-
* @param string $file the path to the file containing phan output
26-
*/
27-
public function __construct($file)
28-
{
29-
$this->file = $file;
30-
}
31-
32-
/**
33-
* {@inheritdoc}
34-
*/
35-
public function parseLines()
36-
{
37-
$handle = fopen($this->file, 'r');
38-
while (($line = fgets($handle)) !== false) {
39-
if (!$this->checkForFile($line)) {
40-
continue;
41-
}
42-
43-
$this->addError($line);
44-
}
45-
46-
return array_keys($this->errors);
47-
}
48-
49-
/**
50-
* {@inheritdoc}
51-
*/
52-
public function getErrorsOnLine($file, $lineNumber)
53-
{
54-
$errors = [];
55-
if (isset($this->errors[$file][$lineNumber])) {
56-
$errors = $this->errors[$file][$lineNumber];
57-
}
58-
59-
return $errors;
60-
}
61-
62-
/**
63-
* {@inheritdoc}
64-
*/
65-
public function handleNotFoundFile()
66-
{
67-
return true;
68-
}
69-
70-
/**
71-
* {@inheritdoc}
13+
/*
14+
* @inheritdoc
7215
*/
7316
public static function getDescription()
7417
{
7518
return 'Parse the default phan(static analysis) output';
7619
}
77-
78-
private function checkForFile($line)
79-
{
80-
return preg_match($this->lineMatch, $line);
81-
}
82-
83-
private function addError($line)
84-
{
85-
$matches = [];
86-
preg_match($this->lineMatch, $line, $matches);
87-
$this->errors[$matches['fileName']][$matches['lineNumber']][] = trim($matches['message']);
88-
}
8920
}

src/PylintLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Used for parsing reports in Pylint format
77
* @package exussum12\CoverageChecker
88
*/
9-
class PylintLoader extends PhanTextLoader
9+
class PylintLoader extends Generic implements FileChecker
1010
{
1111
protected $lineMatch = '#\./(?P<fileName>.*?):(?P<lineNumber>[0-9]+): \[.*?\](?P<message>.*)#';
1212

src/Runners/generic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
}
2929

3030
$checkerArray = [
31+
'buddy' => 'BuddyLoader',
3132
'checkstyle' => 'CheckstyleLoader',
3233
'clover' => 'CloverLoader',
3334
'codeclimate' => 'CodeClimateLoader',

0 commit comments

Comments
 (0)