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 TemperatureConverter extends AbstractMathematicalConverter |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var Unit $celsius Static instance for conversions |
||
21 | */ |
||
22 | public static $celsius; |
||
23 | |||
24 | /** |
||
25 | * @var Unit $fahrenheit Static instance for conversions |
||
26 | */ |
||
27 | public static $fahrenheit; |
||
28 | |||
29 | /** |
||
30 | * @var Unit $kelvin Static instance for conversions |
||
31 | */ |
||
32 | public static $kelvin; |
||
33 | |||
34 | /** |
||
35 | * LengthConverter constructor. |
||
36 | */ |
||
37 | 5 | public function __construct() |
|
43 | |||
44 | /** |
||
45 | * @return string Name of the converter |
||
46 | */ |
||
47 | 4 | public function getName(): string |
|
51 | |||
52 | /** |
||
53 | * @param ConvertibleValue $cv The Convertible to be normalized |
||
54 | */ |
||
55 | 5 | View Code Duplication | protected function normalize(ConvertibleValue $cv) |
75 | |||
76 | /** |
||
77 | * @param ConvertibleValue $from The convertible to be converted |
||
78 | * @param Unit $to Unit to which is to be converted |
||
79 | */ |
||
80 | 5 | View Code Duplication | protected function convertTo(ConvertibleValue $from, Unit $to) |
100 | |||
101 | 5 | public function getBaseUnit(): Unit |
|
105 | |||
106 | } |
||
107 |
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.