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 |
||
32 | class StringInjector extends AbstractInjector |
||
33 | { |
||
34 | /** |
||
35 | * @var array $data The data collected from the DB |
||
36 | */ |
||
37 | protected $data; |
||
38 | |||
39 | /** |
||
40 | * Delimiter for the extracted string |
||
41 | * |
||
42 | * @const string STRING_DELIMETER |
||
43 | */ |
||
44 | const STRING_DELIMETER = ' '; |
||
45 | |||
46 | /** |
||
47 | * Will init the injector's datasource |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | View Code Duplication | public function init() |
|
67 | |||
68 | /** |
||
69 | * We will return a string containing all data entries delimitered by the configured delimiter |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function extract() |
||
84 | } |
||
85 |
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.