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 |
||
9 | class Numeric |
||
10 | { |
||
11 | /** |
||
12 | * @param string $query |
||
13 | * @param array $parameters |
||
14 | * @param array $whereInParameters |
||
15 | * |
||
16 | * @return bool |
||
17 | */ |
||
18 | 3 | View Code Duplication | public function process(string &$query, array &$parameters, array $whereInParameters) |
38 | |||
39 | /** |
||
40 | * @param array $parameters |
||
41 | * @param int $key |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 1 | private function getInQueryPartial(array $parameters, int $key) |
|
55 | |||
56 | /** |
||
57 | * @param string $query |
||
58 | * @param array $partials |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | 1 | private function replaceQueryPartials(string $query, array $partials) |
|
86 | |||
87 | /** |
||
88 | * @param array $parameters |
||
89 | * @param array $partials |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | 1 | private function injectParameterPartials(array $parameters, array $partials) |
|
114 | } |
||
115 |
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.