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

CodeClimateLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 10.98 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 9
loc 82
rs 10
c 1
b 0
f 0
wmc 10
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handleNotFoundFile() 0 4 1
A getDescription() 0 4 1
A addError() 0 12 2
A __construct() 0 5 1
A parseLines() 0 8 2
A getErrorsOnLine() 9 9 2
A convertToJson() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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