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 |
||
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) { |
||
|
|||
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) |
|
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() |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | public static function getDescription() |
||
70 | |||
71 | private function addError($line) |
||
83 | |||
84 | private function convertToJson($codeClimateFormat) |
||
85 | { |
||
86 | $codeClimateFormat = str_replace("\0", ',', $codeClimateFormat); |
||
90 | } |
||
91 |