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 Options extends Collection |
||
10 | { |
||
11 | /** |
||
12 | * Return a Yes/No options. |
||
13 | * |
||
14 | * @return Options |
||
15 | */ |
||
16 | View Code Duplication | public static function yesNo() |
|
25 | |||
26 | /** |
||
27 | * Get options from a model. |
||
28 | * |
||
29 | * @param mixed $model |
||
30 | * @param string $value |
||
31 | * @param string $key |
||
32 | * @return Collection |
||
33 | */ |
||
34 | public static function model($model, $value, $key = 'id') |
||
47 | |||
48 | /** |
||
49 | * Get options from a table. |
||
50 | * |
||
51 | * @param mixed $table |
||
52 | * @param string $value |
||
53 | * @param string $key |
||
54 | * @param \Closure $whereCallback |
||
55 | * @param string|null $key |
||
56 | * @return Collection |
||
57 | */ |
||
58 | public static function table($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null) |
||
70 | |||
71 | /** |
||
72 | * Return a True/False options. |
||
73 | * |
||
74 | * @return Options |
||
75 | */ |
||
76 | View Code Duplication | public static function trueFalse() |
|
85 | |||
86 | /** |
||
87 | * Push an item onto the end of the collection. |
||
88 | * |
||
89 | * @param mixed $value |
||
90 | * @param mixed $key |
||
91 | * @return Options |
||
92 | */ |
||
93 | public function append($value, $key) |
||
97 | |||
98 | /** |
||
99 | * Push an item onto the beginning of the collection. |
||
100 | * |
||
101 | * @param mixed $value |
||
102 | * @param mixed $key |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function prepend($value, $key = null) |
||
111 | } |
||
112 |
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.