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 |
||
12 | class MeasureManager |
||
13 | { |
||
14 | /** |
||
15 | * @var array $config |
||
16 | */ |
||
17 | protected $config = []; |
||
18 | |||
19 | /** |
||
20 | * Set measure config |
||
21 | * |
||
22 | * @param array $config |
||
23 | */ |
||
24 | public function setMeasureConfig(array $config) |
||
28 | |||
29 | /** |
||
30 | * Get unit symbols for a measure family |
||
31 | * |
||
32 | * @param string $family the measure family |
||
33 | * |
||
34 | * @return array the measure symbols |
||
35 | */ |
||
36 | public function getUnitSymbolsForFamily($family) |
||
48 | |||
49 | /** |
||
50 | * Check if unit exists in the given family |
||
51 | * |
||
52 | * @param string $unit the unit to check |
||
53 | * @param string $family the measure family |
||
54 | * |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function unitExistsInFamily($unit, $family) |
||
61 | |||
62 | /** |
||
63 | * Get standard unit for a measure family |
||
64 | * |
||
65 | * @param string $family |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getStandardUnitForFamily($family) |
||
75 | |||
76 | /** |
||
77 | * Get the family config |
||
78 | * |
||
79 | * @param string $family |
||
80 | * |
||
81 | * @throws \InvalidArgumentException |
||
82 | * @return array |
||
83 | */ |
||
84 | View Code Duplication | protected function getFamilyConfig($family) |
|
94 | } |
||
95 |
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.