Passed
Pull Request — master (#26)
by Scott
02:11
created

CodeClimateLoader::parseLines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
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
     * @param string $file the path to the codeclimate file
23
     */
24
    public function __construct($file)
25
    {
26
        $json = $this->convertToJson(file_get_contents($file));
27
        $this->file = json_decode($json);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function parseLines()
34
    {
35
        foreach ($this->file as $line) {
0 ignored issues
show
Bug introduced by
The expression $this->file of type string is not traversable.
Loading history...
36
            $this->addError($line);
37
        }
38
39
        return array_keys($this->errors);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 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...
46
    {
47
        $errors = [];
48
        if (isset($this->errors[$file][$lineNumber])) {
49
            $errors = $this->errors[$file][$lineNumber];
50
        }
51
52
        return $errors;
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
    private function convertToJson($codeClimateFormat)
85
    {
86
        $codeClimateFormat = str_replace("\0", ',', $codeClimateFormat);
87
88
        return '[' . $codeClimateFormat . ']';
89
    }
90
}
91