Completed
Pull Request — master (#14)
by Scott
02:44
created

PhanTextLoader::isValidLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
/**
5
 * Class PhanTextLoader
6
 * Used for parsing phan text output
7
 * @package exussum12\CoverageChecker
8
 */
9
class PhanTextLoader implements FileChecker
10
{
11
    private $lineMatch = '#\./(?P<fileName>.*?):(?P<lineNumber>[0-9]+)(?P<message>.*)#';
12
    /**
13
     * @var string
14
     */
15
    protected $file;
16
17
    /**
18
     * @var array
19
     */
20
    protected $errors = [];
21
22
    /**
23
     * PhanJsonLoader constructor.
24
     * @param string $file the path to the file containing phan output
25
     */
26
    public function __construct($file)
27
    {
28
        $this->file = $file;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getLines()
35
    {
36
        $handle = fopen($this->file, 'r');
37
        while (($line = fgets($handle)) !== false) {
38
            if (!$this->checkForFile($line)) {
39
                continue;
40
            }
41
42
            $this->addError($line);
43
        }
44
45
        return $this->errors;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function isValidLine($file, $lineNumber)
52
    {
53
        return empty($this->errors[$file][$lineNumber]);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function handleNotFoundFile()
60
    {
61
        return true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public static function getDescription()
68
    {
69
        return 'Parse the default phan(static analysis) output';
70
    }
71
72
    private function checkForFile($line)
73
    {
74
        return preg_match($this->lineMatch, $line);
75
    }
76
77
    private function addError($line)
78
    {
79
        $matches = [];
80
        preg_match($this->lineMatch, $line, $matches);
81
        $this->errors[$matches['fileName']][$matches['lineNumber']] = trim($matches['message']);
82
    }
83
}
84