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 |
||
11 | class Select extends EditableColumn implements ColumnEditableInterface |
||
12 | { |
||
13 | use SelectOptionsFromModel; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $view = 'column.editable.select'; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $options = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $optionList = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $exclude = []; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $sortable = true; |
||
39 | |||
40 | /** |
||
41 | * Text constructor. |
||
42 | * |
||
43 | * @param $name |
||
44 | * @param $label |
||
45 | */ |
||
46 | View Code Duplication | public function __construct($name, $label = null, $options = []) |
|
56 | |||
57 | /** |
||
58 | * @param bool $sortable |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function setSortable($sortable) |
||
68 | |||
69 | /** |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function isSortable() |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | View Code Duplication | public function getOptions() |
|
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | */ |
||
99 | public function mutateOptions() |
||
111 | |||
112 | /** |
||
113 | * @param $key |
||
114 | * @return mixed|null |
||
115 | */ |
||
116 | public function getOptionName($value) |
||
126 | |||
127 | /** |
||
128 | * @param array |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function setOptions(array $options) |
||
138 | |||
139 | /** |
||
140 | * @param array $values |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function setEnum(array $values) |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | public function toArray() |
||
159 | |||
160 | /** |
||
161 | * @param Request $request |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function save(Request $request) |
||
183 | } |
||
184 |
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.