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 | * @var Closure |
||
66 | */ |
||
67 | protected static $initCallback; |
||
68 | |||
69 | /** |
||
70 | * Show constructor. |
||
71 | * |
||
72 | * @param Model $model |
||
73 | * @param mixed $builder |
||
74 | */ |
||
75 | public function __construct($model, $builder = null) |
||
87 | |||
88 | /** |
||
89 | * Initialize with user pre-defined default disables, etc. |
||
90 | * |
||
91 | * @param Closure $callback |
||
92 | */ |
||
93 | public static function init(Closure $callback = null) |
||
97 | |||
98 | /** |
||
99 | * Initialize the contents to show. |
||
100 | */ |
||
101 | protected function initContents() |
||
106 | |||
107 | /** |
||
108 | * Initialize panel. |
||
109 | */ |
||
110 | protected function initPanel() |
||
114 | |||
115 | /** |
||
116 | * Get panel instance. |
||
117 | * |
||
118 | * @return Panel |
||
119 | */ |
||
120 | public function panel() |
||
124 | |||
125 | /** |
||
126 | * Add a model field to show. |
||
127 | * |
||
128 | * @param string $name |
||
129 | * @param string $label |
||
130 | * |
||
131 | * @return Field |
||
132 | */ |
||
133 | public function field($name, $label = '') |
||
137 | |||
138 | /** |
||
139 | * Add multiple fields. |
||
140 | * |
||
141 | * @param array $fields |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function fields(array $fields = []) |
||
157 | |||
158 | /** |
||
159 | * Show all fields. |
||
160 | * |
||
161 | * @return Show |
||
162 | */ |
||
163 | public function all() |
||
169 | |||
170 | /** |
||
171 | * Add a relation to show. |
||
172 | * |
||
173 | * @param string $name |
||
174 | * @param string|\Closure $label |
||
175 | * @param null|\Closure $builder |
||
176 | * |
||
177 | * @return Relation |
||
178 | */ |
||
179 | public function relation($name, $label, $builder = null) |
||
188 | |||
189 | /** |
||
190 | * Add a model field to show. |
||
191 | * |
||
192 | * @param string $name |
||
193 | * @param string $label |
||
194 | * |
||
195 | * @return Field |
||
196 | */ |
||
197 | protected function addField($name, $label = '') |
||
209 | |||
210 | /** |
||
211 | * Add a relation panel to show. |
||
212 | * |
||
213 | * @param string $name |
||
214 | * @param \Closure $builder |
||
215 | * @param string $label |
||
216 | * |
||
217 | * @return Relation |
||
218 | */ |
||
219 | protected function addRelation($name, $builder, $label = '') |
||
231 | |||
232 | /** |
||
233 | * Overwrite existing field. |
||
234 | * |
||
235 | * @param string $name |
||
236 | */ |
||
237 | protected function overwriteExistingField($name) |
||
249 | |||
250 | /** |
||
251 | * Overwrite existing relation. |
||
252 | * |
||
253 | * @param string $name |
||
254 | */ |
||
255 | protected function overwriteExistingRelation($name) |
||
267 | |||
268 | /** |
||
269 | * Show a divider. |
||
270 | */ |
||
271 | public function divider() |
||
275 | |||
276 | /** |
||
277 | * Set resource path. |
||
278 | * |
||
279 | * @param string $resource |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function setResource($resource) |
||
289 | |||
290 | /** |
||
291 | * Get resource path. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getResourcePath() |
||
308 | |||
309 | /** |
||
310 | * Set the model instance. |
||
311 | * |
||
312 | * @param Model $model |
||
313 | * |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function setModel($model) |
||
322 | |||
323 | /** |
||
324 | * Get the model instance being queried. |
||
325 | * |
||
326 | * @return Model |
||
327 | */ |
||
328 | public function getModel() |
||
332 | |||
333 | /** |
||
334 | * Add field and relation dynamically. |
||
335 | * |
||
336 | * @param string $method |
||
337 | * @param array $arguments |
||
338 | * |
||
339 | * @return bool|mixed |
||
340 | */ |
||
341 | public function __call($method, $arguments = []) |
||
359 | |||
360 | /** |
||
361 | * Handle the get mutator field. |
||
362 | * |
||
363 | * @param string $method |
||
364 | * @param string $label |
||
365 | * |
||
366 | * @return bool|Field |
||
367 | */ |
||
368 | protected function handleGetMutatorField($method, $label) |
||
380 | |||
381 | /** |
||
382 | * Handle relation field. |
||
383 | * |
||
384 | * @param string $method |
||
385 | * @param array $arguments |
||
386 | * |
||
387 | * @return $this|bool|Relation|Field |
||
388 | */ |
||
389 | protected function handleRelationField($method, $arguments) |
||
438 | |||
439 | protected function showRelationAsField($relation = '', $label = '') |
||
443 | |||
444 | /** |
||
445 | * Handle model field. |
||
446 | * |
||
447 | * @param string $method |
||
448 | * @param string $label |
||
449 | * |
||
450 | * @return bool|Field |
||
451 | */ |
||
452 | protected function handleModelField($method, $label) |
||
460 | |||
461 | /** |
||
462 | * Render the show panels. |
||
463 | * |
||
464 | * @return string |
||
465 | */ |
||
466 | public function render() |
||
490 | } |
||
491 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.