Complex classes like Project 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 Project, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Project extends Model |
||
12 | { |
||
13 | use Sluggable, SoftDeletes; |
||
14 | |||
15 | /** |
||
16 | * The table associated with the model. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $table = 'projects'; |
||
21 | |||
22 | /** |
||
23 | * The attributes that are mass assignable. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $fillable = [ |
||
28 | 'name', 'slug', 'type', 'visible', 'order', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Properties to always eager load. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $with = ['blocks', 'images', 'links']; |
||
37 | |||
38 | /** |
||
39 | * The attributes that should be casted to native types. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $casts = [ |
||
44 | 'visible' => 'boolean', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Fields that are dates. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
53 | |||
54 | /** |
||
55 | * Get the route key for the model. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getRouteKeyName() |
||
63 | |||
64 | /** |
||
65 | * Bootstrap model. |
||
66 | */ |
||
67 | public static function boot() |
||
79 | |||
80 | /** |
||
81 | * Return all visible projects. |
||
82 | * |
||
83 | * @param bool $group If true, group projects by 'type'. |
||
84 | * @param bool $order If true, order projects by 'order'. |
||
85 | * |
||
86 | * @return \Illuminate\Support\Collection |
||
87 | */ |
||
88 | public static function allVisible($group = true, $order = true) |
||
94 | |||
95 | /** |
||
96 | * Return all hidden projects. |
||
97 | * |
||
98 | * @param bool $group If true, group projects by 'type'. |
||
99 | * @param bool $order If true, order projects by 'order'. |
||
100 | * |
||
101 | * @return \Illuminate\Support\Collection |
||
102 | */ |
||
103 | public static function allHidden($group = true, $order = true) |
||
109 | |||
110 | /** |
||
111 | * Return all projects grouped by 'type'. |
||
112 | * |
||
113 | * @param bool $order If true, order projects by 'order'. |
||
114 | * |
||
115 | * @return \Illuminate\Support\Collection |
||
116 | */ |
||
117 | public static function allGrouped($order = true) |
||
123 | |||
124 | /** |
||
125 | * Return all projects ordered by 'order'. |
||
126 | * |
||
127 | * @return \Illuminate\Support\Collection |
||
128 | */ |
||
129 | public static function allOrdered() |
||
135 | |||
136 | /** |
||
137 | * Order and group query, return results. |
||
138 | * |
||
139 | * @param Builder $query Query to be ordered. |
||
140 | * @param bool $group If true, group projects by 'type'. |
||
141 | * @param bool $order If true, order projects by 'order'. |
||
142 | * |
||
143 | * @return \Illuminate\Support\Collection |
||
144 | */ |
||
145 | protected static function orderAndGroupQuery($query, $group, $order) |
||
165 | |||
166 | /** |
||
167 | * Get all projects with given block name. |
||
168 | * |
||
169 | * @param string $blockName Name of block. |
||
170 | * |
||
171 | * @return \Illuminate\Support\Collection |
||
172 | */ |
||
173 | public static function hasBlockNamed($blockName) |
||
177 | |||
178 | /** |
||
179 | * Get all projects with given image name. |
||
180 | * |
||
181 | * @param string $imageName Name of image. |
||
182 | * |
||
183 | * @return \Illuminate\Support\Collection |
||
184 | */ |
||
185 | public static function hasImageNamed($imageName) |
||
189 | |||
190 | /** |
||
191 | * Get all projects with given link name. |
||
192 | * |
||
193 | * @param string $linkName Name of link. |
||
194 | * |
||
195 | * @return \Illuminate\Support\Collection |
||
196 | */ |
||
197 | public static function hasLinkNamed($linkName) |
||
201 | |||
202 | /** |
||
203 | * Get all projects with relationship on table that has given name. |
||
204 | * |
||
205 | * @param string $table Name of table relationship is on. |
||
206 | * @param string $name Relationship name. |
||
207 | * |
||
208 | * @return \Illuminate\Support\Collection |
||
209 | */ |
||
210 | protected static function hasRelationshipNamed($table, $name) |
||
217 | |||
218 | /** |
||
219 | * A project has many text blocks. |
||
220 | * |
||
221 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
222 | */ |
||
223 | public function blocks() |
||
227 | |||
228 | /** |
||
229 | * A project has many images. |
||
230 | * |
||
231 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
232 | */ |
||
233 | public function images() |
||
237 | |||
238 | /** |
||
239 | * A project has many links. |
||
240 | * |
||
241 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
242 | */ |
||
243 | public function links() |
||
247 | |||
248 | /** |
||
249 | * Return the project id. |
||
250 | * |
||
251 | * @return int |
||
252 | */ |
||
253 | public function id() |
||
257 | |||
258 | /** |
||
259 | * Return the project name. |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function name() |
||
267 | |||
268 | /** |
||
269 | * Return the project type. |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public function type() |
||
277 | |||
278 | /** |
||
279 | * Return the project slug. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function slug() |
||
287 | |||
288 | /** |
||
289 | * Return the project order value. |
||
290 | * |
||
291 | * @return int |
||
292 | */ |
||
293 | public function order() |
||
297 | |||
298 | /** |
||
299 | * Return true if project has blocks. |
||
300 | * |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function hasBlocks() |
||
307 | |||
308 | /** |
||
309 | * Get a text block by name, if exists. |
||
310 | * |
||
311 | * @param string $name Name of text block to get. |
||
312 | * |
||
313 | * @return Larafolio\Models\TextBlock |
||
314 | */ |
||
315 | public function block($name) |
||
319 | |||
320 | /** |
||
321 | * Get block text by block name, if block exists. |
||
322 | * |
||
323 | * @param string $name Name of text block to get. |
||
324 | * @param bool $formatted If true, return formmated text. |
||
325 | * |
||
326 | * @return string|null |
||
327 | */ |
||
328 | public function blockText($name, $formatted = true) |
||
340 | |||
341 | /** |
||
342 | * Get formatted text of block named description or first block. |
||
343 | * |
||
344 | * @return Larafolio\Models\TextBlock |
||
345 | */ |
||
346 | public function getProjectBlock() |
||
356 | |||
357 | /** |
||
358 | * Get formatted text of block named description or first block. |
||
359 | * |
||
360 | * @param bool $formatted If true, return formatted text. |
||
361 | * |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getProjectBlockText($formatted = true) |
||
376 | |||
377 | /** |
||
378 | * Return true if project has images. |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | public function hasImages() |
||
386 | |||
387 | /** |
||
388 | * Get image by name, if exists. |
||
389 | * |
||
390 | * @param string $name Name of image to get. |
||
391 | * |
||
392 | * @return Larafolio\Models\Image|null |
||
393 | */ |
||
394 | public function image($name) |
||
398 | |||
399 | /** |
||
400 | * Get image url for given size. |
||
401 | * |
||
402 | * @param string $name Name of image to get url for. |
||
403 | * @param string $size Size of image. |
||
404 | * |
||
405 | * @return string|null |
||
406 | */ |
||
407 | public function imageUrl($name, $size = 'medium') |
||
415 | |||
416 | /** |
||
417 | * Get caption for image. |
||
418 | * |
||
419 | * @param string $name Name of image to get caption for. |
||
420 | * |
||
421 | * @return string|null |
||
422 | */ |
||
423 | public function imageCaption($name) |
||
431 | |||
432 | /** |
||
433 | * Get alt for image. |
||
434 | * |
||
435 | * @param string $name Name of image to get caption for. |
||
436 | * |
||
437 | * @return string|null |
||
438 | */ |
||
439 | public function imageAlt($name) |
||
447 | |||
448 | /** |
||
449 | * Get url of small image with project name or first image in collection. |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getProjectImage() |
||
463 | |||
464 | /** |
||
465 | * Get url of small image with project name or first image in collection. |
||
466 | * |
||
467 | * @param string $size The size of the image, name of image cache filter. |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | public function getProjectImageUrl($size = 'small') |
||
481 | |||
482 | /** |
||
483 | * Return true if project has links. |
||
484 | * |
||
485 | * @return bool |
||
486 | */ |
||
487 | public function hasLinks() |
||
491 | |||
492 | /** |
||
493 | * Get link by name, if exists. |
||
494 | * |
||
495 | * @param string $name Name of link to get. |
||
496 | * |
||
497 | * @return Larafolio\Models\Link|null |
||
498 | */ |
||
499 | public function link($name) |
||
503 | |||
504 | /** |
||
505 | * Get link url. |
||
506 | * |
||
507 | * @param string $name Name of link. |
||
508 | * |
||
509 | * @return string|null |
||
510 | */ |
||
511 | public function linkUrl($name) |
||
519 | |||
520 | /** |
||
521 | * Get link text. |
||
522 | * |
||
523 | * @param string $name Name of link. |
||
524 | * |
||
525 | * @return string|null |
||
526 | */ |
||
527 | public function linkText($name) |
||
535 | |||
536 | /** |
||
537 | * Get a model from a relationship by model name. |
||
538 | * |
||
539 | * @param string $relationship Name of relationship. |
||
540 | * @param string $name Name of model to get. |
||
541 | * |
||
542 | * @return \Illuminate\Database\Eloquent\Model|null |
||
543 | */ |
||
544 | protected function getFromRelationshipByName($relationship, $name) |
||
554 | |||
555 | /** |
||
556 | * Get blocks sorted by order. |
||
557 | * |
||
558 | * @param \Builder $query Query builder. |
||
559 | * @param string $slug Project slug. |
||
560 | * |
||
561 | * @return \Builder |
||
562 | */ |
||
563 | public function scopeWithBlocks($query, $slug) |
||
568 | |||
569 | /** |
||
570 | * Get full project info (blocks and links sorted by order). |
||
571 | * |
||
572 | * @param \Builder $query Query builder. |
||
573 | * @param string $slug Project slug. |
||
574 | * |
||
575 | * @return \Builder |
||
576 | */ |
||
577 | public function scopeFull($query, $slug) |
||
583 | |||
584 | /** |
||
585 | * Return images with all props needed for javascript. |
||
586 | * |
||
587 | * @return Collection |
||
588 | */ |
||
589 | public function imagesWithProps() |
||
596 | |||
597 | /** |
||
598 | * Order given relationship by order value. |
||
599 | * |
||
600 | * @param \Builder $query Query builder. |
||
601 | * @param string $relationship Name of relationship to order. |
||
602 | * |
||
603 | * @return \Builder |
||
604 | */ |
||
605 | public function scopeOrderRelationship($query, $relationship) |
||
611 | |||
612 | /** |
||
613 | * Return project properties to be passed to js. |
||
614 | * |
||
615 | * @return array |
||
616 | */ |
||
617 | public function generateProps() |
||
626 | } |
||
627 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: