Test Failed
Pull Request — master (#14)
by Scott
02:01
created

CodeClimateLoader::convertToJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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 = $this->convertToJson(file_get_contents($file));
28
        $this->file = json_decode($json);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getLines()
35
    {
36
        foreach ($this->file as $line) {
0 ignored issues
show
Bug introduced by
The expression $this->file of type string is not traversable.
Loading history...
37
            $this->addError($line);
38
        }
39
40
        return $this->errors;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function isValidLine($file, $lineNumber)
47
    {
48
        return empty($this->errors[$file][$lineNumber]);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function handleNotFoundFile()
55
    {
56
        return true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public static function getDescription()
63
    {
64
        return 'Parse codeclimate output';
65
    }
66
67
    private function addError($line)
68
    {
69
        $trim = './';
70
        $fileName = substr($line->location->path, strlen($trim));
71
        $start = $line->location->lines->begin;
72
        $end = $line->location->lines->end;
73
        $message = $line->description;
74
75
        for($lineNumber = $start; $lineNumber <= $end; $lineNumber++) {
76
            $this->errors[$fileName][$lineNumber] = $message;
77
        }
78
    }
79
80
    private function convertToJson($codeClimateFormat)
81
    {
82
        $codeClimateFormat = str_replace("\0", ',', $codeClimateFormat);
83
84
        return $codeClimateFormat;
85
    }
86
}
87