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 |
||
13 | class Iso3166ConverterService extends AbstractService implements Iso3166ConverterServiceInterface |
||
14 | { |
||
15 | const ISO2_KEY = 'alpha2'; |
||
16 | const ISO3166_KEY = 'numeric'; |
||
17 | |||
18 | /** |
||
19 | * @param string $iso2CountryCode |
||
20 | * |
||
21 | * @return string|null |
||
22 | */ |
||
23 | View Code Duplication | public function iso2ToNumeric($iso2CountryCode) |
|
32 | |||
33 | /** |
||
34 | * @param string $iso3166CountryCode |
||
35 | * |
||
36 | * @return string|null |
||
37 | */ |
||
38 | View Code Duplication | public function numericToIso2($iso3166CountryCode) |
|
47 | |||
48 | /** |
||
49 | * @param string $value |
||
50 | * @param string $columnName |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | protected function searchArrayIndex($value, $columnName) |
||
58 | } |
||
59 |
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.