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

CodeClimateLoader::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
/**
5
 * Class CodeClimateLoader
6
 * Used for parsing reports in CodeClimate format
7
 * @package exussum12\CoverageChecker
8
 */
9
class CodeClimateLoader implements FileChecker
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $file;
15
16
    /**
17
     * @var array
18
     */
19
    protected $errors = [];
20
21
    /**
22
     * PhpMdLoader constructor.
23
     * @param string $file the path to the phan json file
24
     */
25
    public function __construct($file)
26
    {
27
        $json = file_get_contents($file);
28
        $this->file = json_decode($json);
29
        if (json_last_error() == JSON_ERROR_SYNTAX) {
30
            //try again with array syntax ...
31
           $this->file = json_decode('[' . $json . ']');
32
        }
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getLines()
39
    {
40
        foreach ($this->file as $line) {
0 ignored issues
show
Bug introduced by
The expression $this->file of type string is not traversable.
Loading history...
41
            $this->addError($line);
42
        }
43
44
        return $this->errors;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function isValidLine($file, $lineNumber)
51
    {
52
        return empty($this->errors[$file][$lineNumber]);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function handleNotFoundFile()
59
    {
60
        return true;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public static function getDescription()
67
    {
68
        return 'Parse codeclimate output';
69
    }
70
71
    private function addError($line)
72
    {
73
        $trim = './';
74
        $fileName = substr($line->location->path, strlen($trim));
75
        $start = $line->location->lines->begin;
76
        $end = $line->location->lines->end;
77
        $message = $line->description;
78
79
        for($lineNumber = $start; $lineNumber <= $end; $lineNumber++) {
80
            $this->errors[$fileName][$lineNumber] = $message;
81
        }
82
    }
83
}
84