Complex classes like ContentfulModel 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 ContentfulModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | abstract class ContentfulModel extends Model |
||
12 | { |
||
13 | use Localable; |
||
14 | |||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | protected $primaryKey = 'contentful_id'; |
||
19 | |||
20 | /** |
||
21 | * The content-type ID. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $contentType = null; |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | protected $keyType = 'string'; |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public $incrementing = false; |
||
36 | |||
37 | /** |
||
38 | * ContentfulModel constructor. |
||
39 | * |
||
40 | * @param array $attributes |
||
41 | * @return void |
||
|
|||
42 | */ |
||
43 | public function __construct(array $attributes = []) |
||
44 | { |
||
45 | // Override fillable |
||
46 | foreach ($this->defaultFillable() as $defaultFillable) { |
||
47 | if (!in_array($defaultFillable, $this->fillable)) { |
||
48 | $this->fillable[] = $defaultFillable; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | // Override casts |
||
53 | foreach ($this->defaultCasts() as $field => $type) { |
||
54 | if (!isset($this->casts[$field])) { |
||
55 | $this->casts[$field] = $type; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | $this->initContentType(); |
||
60 | |||
61 | parent::__construct($attributes); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Init model content-type if needed. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | protected function initContentType() |
||
70 | { |
||
71 | if (empty($this->contentType)) { |
||
72 | $this->contentType = lcfirst(class_basename(get_class($this))); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Return default fillable fields. |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function defaultFillable(): array |
||
92 | |||
93 | /** |
||
94 | * Return default casted fields. |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | public function defaultCasts(): array |
||
104 | |||
105 | // -------------------------------------------------------------------------------- |
||
106 | // -------------------------------------------------------------------------------- |
||
107 | // -------------------------------------------------------------------------------- |
||
108 | |||
109 | protected function getAndSetPayloadContentfulAsset(string $payload, $link, $query = null): ?Asset |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Return Contentful Asset for given link (sys or ID). |
||
126 | * |
||
127 | * @param array|string|null $link |
||
128 | * @param callback|null $query |
||
129 | * @return \Distilleries\Contentful\Models\Asset|null |
||
130 | */ |
||
131 | protected function contentfulAsset($link, $query = null): ?Asset |
||
152 | |||
153 | /** |
||
154 | * Return payload of related Contentful entries. |
||
155 | * |
||
156 | * @param string $payload |
||
157 | * @param array $links |
||
158 | * @param mixed $query |
||
159 | * @return \Illuminate\Support\Collection |
||
160 | */ |
||
161 | protected function getAndSetPayloadContentfulEntries(string $payload, array $links, $query = null): Collection |
||
174 | |||
175 | /** |
||
176 | * Return payload of related Contentful entry. |
||
177 | * |
||
178 | * @param string $payload |
||
179 | * @param array $links |
||
180 | * @param mixed $query |
||
181 | * @return \Distilleries\Contentful\Models\Base\ContentfulModel|null |
||
182 | */ |
||
183 | protected function getAndSetPayloadContentfulEntry(string $payload, array $links, $query = null): ?ContentfulModel |
||
196 | |||
197 | /** |
||
198 | * Return Contentful Entry for given link (sys or ID). |
||
199 | * |
||
200 | * @param array|string|null $link |
||
201 | * @param mixed $query |
||
202 | * @return \Distilleries\Contentful\Models\Base\ContentfulModel|null |
||
203 | */ |
||
204 | protected function contentfulEntry($link, $query = null): ?ContentfulModel |
||
216 | |||
217 | /** |
||
218 | * Return Contentful Entries for given ID. |
||
219 | * |
||
220 | * @param array $links |
||
221 | * @param mixed $query |
||
222 | * @return \Illuminate\Support\Collection |
||
223 | */ |
||
224 | protected function contentfulEntries(array $links, $query = null): Collection |
||
273 | |||
274 | /** |
||
275 | * Return a collection of related models for base Contentful ID. |
||
276 | * |
||
277 | * @param string $contentfulId |
||
278 | * @param string $contentfulType |
||
279 | * @return \Illuminate\Support\Collection |
||
280 | */ |
||
281 | protected function contentfulRelatedEntries(string $contentfulId, string $contentfulType = ''): Collection |
||
316 | |||
317 | /** |
||
318 | * Return Contentful link ID. |
||
319 | * |
||
320 | * @param mixed $link |
||
321 | * @return string|null |
||
322 | */ |
||
323 | protected function contentfulLinkId($link): ?string |
||
339 | |||
340 | /** |
||
341 | * Return model Contentful content-type. |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | public function getContentType(): string |
||
349 | |||
350 | /** |
||
351 | * Return ID attribute. |
||
352 | * |
||
353 | * @return mixed |
||
354 | */ |
||
355 | public function getIdAttribute() |
||
359 | |||
360 | /** |
||
361 | * Magical extended toArray(). |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | public function toArray() |
||
377 | } |
||
378 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.