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 |
||
10 | class Tools implements Renderable |
||
11 | { |
||
12 | /** |
||
13 | * The panel that holds this tool. |
||
14 | * |
||
15 | * @var Panel |
||
16 | */ |
||
17 | protected $panel; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $resource; |
||
23 | |||
24 | /** |
||
25 | * Default tools. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $tools = ['delete', 'edit', 'list']; |
||
30 | |||
31 | /** |
||
32 | * Tools should be appends to default tools. |
||
33 | * |
||
34 | * @var Collection |
||
35 | */ |
||
36 | protected $appends; |
||
37 | |||
38 | /** |
||
39 | * Tools should be prepends to default tools. |
||
40 | * |
||
41 | * @var Collection |
||
42 | */ |
||
43 | protected $prepends; |
||
44 | |||
45 | /** |
||
46 | * Tools constructor. |
||
47 | * |
||
48 | * @param Panel $panel |
||
49 | */ |
||
50 | public function __construct(Panel $panel) |
||
57 | |||
58 | /** |
||
59 | * Append a tools. |
||
60 | * |
||
61 | * @param mixed $tool |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function append($tool) |
||
71 | |||
72 | /** |
||
73 | * Prepend a tool. |
||
74 | * |
||
75 | * @param mixed $tool |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function prepend($tool) |
||
85 | |||
86 | /** |
||
87 | * Get resource path. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getResource() |
||
99 | |||
100 | /** |
||
101 | * Disable `list` tool. |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function disableList() |
||
111 | |||
112 | /** |
||
113 | * Disable `delete` tool. |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function disableDelete() |
||
123 | |||
124 | /** |
||
125 | * Disable `edit` tool. |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function disableEdit() |
||
135 | |||
136 | /** |
||
137 | * Get request path for resource list. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function getListPath() |
||
145 | |||
146 | /** |
||
147 | * Get request path for edit. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | protected function getEditPath() |
||
157 | |||
158 | /** |
||
159 | * Get request path for delete. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function getDeletePath() |
||
169 | |||
170 | /** |
||
171 | * Render `list` tool. |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | protected function renderList() |
||
187 | |||
188 | /** |
||
189 | * Render `edit` tool. |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function renderEdit() |
||
205 | |||
206 | /** |
||
207 | * Render `delete` tool. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | View Code Duplication | protected function renderDelete() |
|
269 | |||
270 | /** |
||
271 | * Render custom tools. |
||
272 | * |
||
273 | * @param Collection $tools |
||
274 | * @return mixed |
||
275 | */ |
||
276 | View Code Duplication | protected function renderCustomTools($tools) |
|
290 | |||
291 | /** |
||
292 | * Render tools. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | View Code Duplication | public function render() |
|
307 | } |
||
308 |
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.