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 |
||
19 | class ModelHelper extends Base |
||
20 | { |
||
21 | /** |
||
22 | * Remove keys from an array. |
||
23 | * |
||
24 | * @param array $values Input array |
||
25 | * @param string[] $keys List of keys to remove |
||
26 | */ |
||
27 | public function removeFields(array &$values, array $keys) |
||
35 | |||
36 | /** |
||
37 | * Remove keys from an array if empty. |
||
38 | * |
||
39 | * @param array $values Input array |
||
40 | * @param string[] $keys List of keys to remove |
||
41 | */ |
||
42 | View Code Duplication | public function removeEmptyFields(array &$values, array $keys) |
|
50 | |||
51 | /** |
||
52 | * Force fields to be at 0 if empty. |
||
53 | * |
||
54 | * @param array $values Input array |
||
55 | * @param string[] $keys List of keys |
||
56 | */ |
||
57 | View Code Duplication | public function resetFields(array &$values, array $keys) |
|
65 | |||
66 | /** |
||
67 | * Force some fields to be integer. |
||
68 | * |
||
69 | * @param array $values Input array |
||
70 | * @param string[] $keys List of keys |
||
71 | */ |
||
72 | View Code Duplication | public function convertIntegerFields(array &$values, array $keys) |
|
80 | |||
81 | /** |
||
82 | * Force some fields to be null if empty. |
||
83 | * |
||
84 | * @param array $values Input array |
||
85 | * @param string[] $keys List of keys |
||
86 | */ |
||
87 | View Code Duplication | public function convertNullFields(array &$values, array $keys) |
|
95 | } |
||
96 |
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.