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:
Complex classes like Show often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Show, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Show implements Renderable |
||
23 | { |
||
24 | /** |
||
25 | * The Eloquent model to show. |
||
26 | * |
||
27 | * @var Model |
||
28 | */ |
||
29 | protected $model; |
||
30 | |||
31 | /** |
||
32 | * Show panel builder. |
||
33 | * |
||
34 | * @var callable |
||
35 | */ |
||
36 | protected $builder; |
||
37 | |||
38 | /** |
||
39 | * Resource path for this show page. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $resource; |
||
44 | |||
45 | /** |
||
46 | * Fields to be show. |
||
47 | * |
||
48 | * @var Collection |
||
49 | */ |
||
50 | protected $fields; |
||
51 | |||
52 | /** |
||
53 | * Relations to be show. |
||
54 | * |
||
55 | * @var Collection |
||
56 | */ |
||
57 | protected $relations; |
||
58 | |||
59 | /** |
||
60 | * @var Panel |
||
61 | */ |
||
62 | protected $panel; |
||
63 | |||
64 | /** |
||
65 | * Show constructor. |
||
66 | * |
||
67 | * @param Model $model |
||
68 | * @param mixed $builder |
||
69 | */ |
||
70 | public function __construct($model, $builder = null) |
||
78 | |||
79 | /** |
||
80 | * Initialize the contents to show. |
||
81 | */ |
||
82 | protected function initContents() |
||
87 | |||
88 | /** |
||
89 | * Initialize panel |
||
90 | */ |
||
91 | protected function initPanel() |
||
95 | |||
96 | /** |
||
97 | * Get panel instance. |
||
98 | * |
||
99 | * @return Panel |
||
100 | */ |
||
101 | public function panel() |
||
105 | |||
106 | /** |
||
107 | * Add a model field to show. |
||
108 | * |
||
109 | * @param string $name |
||
110 | * @param string $label |
||
111 | * |
||
112 | * @return Field |
||
113 | */ |
||
114 | public function field($name, $label = '') |
||
118 | |||
119 | /** |
||
120 | * Add multiple fields. |
||
121 | * |
||
122 | * @param array $fields |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function fields(array $fields = []) |
||
138 | |||
139 | /** |
||
140 | * Show all fields. |
||
141 | * |
||
142 | * @return Show |
||
143 | */ |
||
144 | public function all() |
||
150 | |||
151 | /** |
||
152 | * Add a relation to show. |
||
153 | * |
||
154 | * @param string $name |
||
155 | * @param string|\Closure $label |
||
156 | * @param null|\Closure $builder |
||
157 | * |
||
158 | * @return Relation |
||
159 | */ |
||
160 | public function relation($name, $label, $builder = null) |
||
169 | |||
170 | /** |
||
171 | * Add a model field to show. |
||
172 | * |
||
173 | * @param string $name |
||
174 | * @param string $label |
||
175 | * |
||
176 | * @return Field |
||
177 | */ |
||
178 | protected function addField($name, $label = '') |
||
190 | |||
191 | /** |
||
192 | * Add a relation panel to show. |
||
193 | * |
||
194 | * @param string $name |
||
195 | * @param \Closure $builder |
||
196 | * @param string $label |
||
197 | * |
||
198 | * @return Relation |
||
199 | */ |
||
200 | protected function addRelation($name, $builder, $label = '') |
||
212 | |||
213 | /** |
||
214 | * Overwrite existing field. |
||
215 | * |
||
216 | * @param string $name |
||
217 | */ |
||
218 | protected function overwriteExistingField($name) |
||
230 | |||
231 | /** |
||
232 | * Overwrite existing relation. |
||
233 | * |
||
234 | * @param string $name |
||
235 | */ |
||
236 | protected function overwriteExistingRelation($name) |
||
248 | |||
249 | /** |
||
250 | * Show a divider. |
||
251 | */ |
||
252 | public function divider() |
||
256 | |||
257 | /** |
||
258 | * Set resource path. |
||
259 | * |
||
260 | * @param string $resource |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function setResource($resource) |
||
270 | |||
271 | /** |
||
272 | * Get resource path. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getResourcePath() |
||
289 | |||
290 | /** |
||
291 | * Set the model instance. |
||
292 | * |
||
293 | * @param Model $model |
||
294 | * |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function setModel($model) |
||
303 | |||
304 | /** |
||
305 | * Get the model instance being queried. |
||
306 | * |
||
307 | * @return Model |
||
308 | */ |
||
309 | public function getModel() |
||
313 | |||
314 | /** |
||
315 | * Add field and relation dynamically. |
||
316 | * |
||
317 | * @param string $method |
||
318 | * @param array $arguments |
||
319 | * |
||
320 | * @return bool|mixed |
||
321 | */ |
||
322 | public function __call($method, $arguments = []) |
||
340 | |||
341 | /** |
||
342 | * Handle the get mutator field. |
||
343 | * |
||
344 | * @param string $method |
||
345 | * @param string $label |
||
346 | * |
||
347 | * @return bool|Field |
||
348 | */ |
||
349 | protected function handleGetMutatorField($method, $label) |
||
361 | |||
362 | /** |
||
363 | * Handle relation field. |
||
364 | * |
||
365 | * @param string $method |
||
366 | * @param array $arguments |
||
367 | * |
||
368 | * @return $this|bool|Relation|Field |
||
369 | */ |
||
370 | protected function handleRelationField($method, $arguments) |
||
419 | |||
420 | protected function showRelationAsField($relation = '', $label = '') |
||
424 | |||
425 | /** |
||
426 | * Handle model field. |
||
427 | * |
||
428 | * @param string $method |
||
429 | * @param string $label |
||
430 | * |
||
431 | * @return bool|Field |
||
432 | */ |
||
433 | protected function handleModelField($method, $label) |
||
441 | |||
442 | /** |
||
443 | * Render the show panels. |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | public function render() |
||
471 | } |
||
472 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.