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 |
||
16 | class MeasureConverter |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $config; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $family; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param array $config Configuration parameters |
||
32 | */ |
||
33 | public function __construct($config = []) |
||
34 | { |
||
35 | $this->config = $config['measures_config']; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Set a family for the converter |
||
40 | * @param string $family |
||
41 | * |
||
42 | * @throws UnknownFamilyMeasureException |
||
43 | * @return MeasureConverter |
||
44 | * |
||
45 | */ |
||
46 | View Code Duplication | public function setFamily($family) |
|
|
|||
47 | { |
||
48 | if (!isset($this->config[$family])) { |
||
49 | throw new UnknownFamilyMeasureException(); |
||
50 | } |
||
51 | |||
52 | $this->family = $family; |
||
53 | |||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Convert a value from a base measure to a final measure |
||
59 | * @param string $baseUnit Base unit for value |
||
60 | * @param string $finalUnit Result unit for value |
||
61 | * @param double $value Value to convert |
||
62 | * |
||
63 | * @return double |
||
64 | */ |
||
65 | public function convert($baseUnit, $finalUnit, $value) |
||
66 | { |
||
67 | $standardValue = $this->convertBaseToStandard($baseUnit, $value); |
||
68 | |||
69 | $result = $this->convertStandardToResult($finalUnit, $standardValue); |
||
70 | |||
71 | return $result; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Convert a value in a base unit to the standard unit |
||
76 | * @param string $baseUnit Base unit for value |
||
77 | * @param double $value Value to convert |
||
78 | * |
||
79 | * @throws UnknownOperatorException |
||
80 | * @throws UnknownMeasureException |
||
81 | * @return double |
||
82 | * |
||
83 | */ |
||
84 | View Code Duplication | public function convertBaseToStandard($baseUnit, $value) |
|
106 | |||
107 | /** |
||
108 | * Apply operation between value and operand by using operator |
||
109 | * |
||
110 | * @param double $value Value to convert |
||
111 | * @param string $operator Operator to apply |
||
112 | * @param double $operand Operand to use |
||
113 | * |
||
114 | * @return double |
||
115 | */ |
||
116 | View Code Duplication | protected function applyOperation($value, $operator, $operand) |
|
141 | |||
142 | /** |
||
143 | * Convert a value in a standard unit to a final unit |
||
144 | * @param string $finalUnit Final unit for value |
||
145 | * @param double $value Value to convert |
||
146 | * |
||
147 | * @throws UnknownOperatorException |
||
148 | * @throws UnknownMeasureException |
||
149 | * @return double |
||
150 | * |
||
151 | */ |
||
152 | View Code Duplication | public function convertStandardToResult($finalUnit, $value) |
|
175 | |||
176 | /** |
||
177 | * Apply reversed operation between value and operand by using operator |
||
178 | * |
||
179 | * @param double $value Value to convert |
||
180 | * @param string $operator Operator to apply |
||
181 | * @param double $operand Operand to use |
||
182 | * |
||
183 | * @return double |
||
184 | */ |
||
185 | View Code Duplication | protected function applyReversedOperation($value, $operator, $operand) |
|
210 | } |
||
211 |
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.