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 |
||
| 12 | abstract class ContentfulModel extends Model |
||
| 13 | { |
||
| 14 | use Localable; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * {@inheritdoc} |
||
| 18 | */ |
||
| 19 | protected $primaryKey = 'contentful_id'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The content-type ID. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $contentType = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | protected $keyType = 'string'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public $incrementing = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * ContentfulModel constructor. |
||
| 40 | * |
||
| 41 | * @param array $attributes |
||
| 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 |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Return a collection of related models for base Contentful ID. |
||
| 277 | * |
||
| 278 | * @param string $contentfulId |
||
| 279 | * @param string $contentfulType |
||
| 280 | * @return \Illuminate\Support\Collection |
||
| 281 | */ |
||
| 282 | protected function contentfulRelatedEntries(string $contentfulId, string $contentfulType = ''): Collection |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return Contentful link ID. |
||
| 317 | * |
||
| 318 | * @param mixed $link |
||
| 319 | * @return string|null |
||
| 320 | */ |
||
| 321 | protected function contentfulLinkId($link): ?string |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return model Contentful content-type. |
||
| 340 | * |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | public function getContentType(): string |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return ID attribute. |
||
| 350 | * |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | public function getIdAttribute() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Magical extended toArray(). |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | public function toArray() |
||
| 375 | } |
||
| 376 |