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 |
||
7 | final class MoneyFormat implements MoneyFormatInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $decimalsSeparator; |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $thousandsSeparator; |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $extraPrecision; |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $decorationType; |
||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $precision; |
||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $decorationSpace; |
||
33 | |||
34 | private function __construct( |
||
49 | |||
50 | public static function default(): MoneyFormatInterface |
||
54 | |||
55 | View Code Duplication | public static function fromParameters( |
|
68 | |||
69 | View Code Duplication | public static function fromParametersWithPrecision( |
|
85 | |||
86 | View Code Duplication | public static function fromParametersWithExtraPrecision( |
|
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getDecimalsSeparator(): string |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getThousandsSeparator(): string |
||
117 | |||
118 | /** |
||
119 | * @return int |
||
120 | */ |
||
121 | public function getExtraPrecision(): int |
||
125 | |||
126 | /** |
||
127 | * @return int |
||
128 | */ |
||
129 | public function getDecorationType(): int |
||
133 | |||
134 | /** |
||
135 | * @return int|null |
||
136 | */ |
||
137 | public function getPrecision() |
||
141 | |||
142 | public function getDecorationSpace(): int |
||
146 | } |
||
147 |
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.