Completed
Push — master ( 4da4bd...5ab290 )
by Scott
14s
created

PhanTextLoader::parseLines()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
rs 9.4285
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
    protected $lineMatch = '#(?:\./)?(?P<fileName>.*?):(?P<lineNumber>[0-9]+)(?P<message>.*)#';
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 View Code Duplication
    public function getErrorsOnLine($file, $lineNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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}
72
     */
73
    public static function getDescription()
74
    {
75
        return 'Parse the default phan(static analysis) output';
76
    }
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
    }
89
}
90