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 |
||
14 | View Code Duplication | class ArrayNode extends Node implements IArrayNode { |
|
|
|||
15 | /** |
||
16 | * ArrayNode constructor. |
||
17 | * |
||
18 | * @param IConfigSchema $schema |
||
19 | * @param string $key |
||
20 | * @param string $message |
||
21 | */ |
||
22 | public function __construct(IConfigSchema $schema, $key, $message = null) { |
||
30 | |||
31 | /** |
||
32 | * @param int $min |
||
33 | * |
||
34 | * @return IArrayNode |
||
35 | */ |
||
36 | public function min($min) { |
||
41 | |||
42 | /** |
||
43 | * @param int $max |
||
44 | * |
||
45 | * @return IArrayNode |
||
46 | */ |
||
47 | public function max($max) { |
||
52 | |||
53 | /** |
||
54 | * @param int $length |
||
55 | * |
||
56 | * @return IArrayNode |
||
57 | */ |
||
58 | public function length($length) { |
||
63 | |||
64 | /** |
||
65 | * @param array $values |
||
66 | * |
||
67 | * @return IArrayNode |
||
68 | */ |
||
69 | public function allowed(array $values) { |
||
74 | |||
75 | /** |
||
76 | * @param array $values |
||
77 | * |
||
78 | * @return IArrayNode |
||
79 | */ |
||
80 | public function forbidden(array $values) { |
||
85 | } |
||
86 |
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.