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 |
||
6 | trait OptionChecker |
||
7 | { |
||
8 | /** |
||
9 | * Checks an array so that their keys are ints |
||
10 | * |
||
11 | * @param array $options Options |
||
12 | * @param array $keys Keys to check |
||
13 | * @throw InvalidOptionException |
||
14 | */ |
||
15 | View Code Duplication | protected function checkOptionsInt(array $options, array $keys) |
|
23 | |||
24 | /** |
||
25 | * Checks an array so that their keys are strings |
||
26 | * |
||
27 | * @param array $options Options |
||
28 | * @param array $keys Keys to check |
||
29 | * @throw InvalidOptionException |
||
30 | */ |
||
31 | View Code Duplication | protected function checkOptionsString(array $options, array $keys) |
|
39 | |||
40 | /** |
||
41 | * Checks an array so that their keys are arrays |
||
42 | * |
||
43 | * @param array $options Options |
||
44 | * @param array $keys Keys to check |
||
45 | * @throw InvalidOptionException |
||
46 | */ |
||
47 | View Code Duplication | protected function checkOptionsArray(array $options, array $keys) |
|
55 | } |
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.