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 | /** |
||
| 110 | * Return Contentful Asset for given link (sys or ID). |
||
| 111 | * |
||
| 112 | * @param array|string|null $link |
||
| 113 | * @return \Distilleries\Contentful\Models\Asset|null |
||
| 114 | */ |
||
| 115 | protected function contentfulAsset($link): ?Asset |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return payload of related Contentful entries. |
||
| 134 | * |
||
| 135 | * @param string $payload |
||
| 136 | * @param array $links |
||
| 137 | * @param mixed $query |
||
| 138 | * @return \Illuminate\Support\Collection |
||
| 139 | */ |
||
| 140 | protected function getAndSetPayloadContentfulEntries(string $payload, array $links, $query = null): Collection |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return payload of related Contentful entry. |
||
| 155 | * |
||
| 156 | * @param string $payload |
||
| 157 | * @param array $links |
||
| 158 | * @param mixed $query |
||
| 159 | * @return \Distilleries\Contentful\Models\Base\ContentfulModel|null |
||
| 160 | */ |
||
| 161 | protected function getAndSetPayloadContentfulEntry(string $payload, array $links, $query = null): ?ContentfulModel |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return Contentful Entry for given link (sys or ID). |
||
| 176 | * |
||
| 177 | * @param array|string|null $link |
||
| 178 | * @param mixed $query |
||
| 179 | * @return \Distilleries\Contentful\Models\Base\ContentfulModel|null |
||
| 180 | */ |
||
| 181 | protected function contentfulEntry($link, $query = null): ?ContentfulModel |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return Contentful Entries for given ID. |
||
| 196 | * |
||
| 197 | * @param array $links |
||
| 198 | * @param mixed $query |
||
| 199 | * @return \Illuminate\Support\Collection |
||
| 200 | */ |
||
| 201 | protected function contentfulEntries(array $links, $query = null): Collection |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Return a collection of related models for base Contentful ID. |
||
| 253 | * |
||
| 254 | * @param string $contentfulId |
||
| 255 | * @param string $contentfulType |
||
| 256 | * @return \Illuminate\Support\Collection |
||
| 257 | */ |
||
| 258 | protected function contentfulRelatedEntries(string $contentfulId, string $contentfulType = ''): Collection |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Return Contentful link ID. |
||
| 295 | * |
||
| 296 | * @param mixed $link |
||
| 297 | * @return string|null |
||
| 298 | */ |
||
| 299 | protected function contentfulLinkId($link): ?string |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return model Contentful content-type. |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | public function getContentType(): string |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Return ID attribute. |
||
| 328 | * |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | public function getIdAttribute() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Magical extended toArray(). |
||
| 338 | * |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function toArray() |
||
| 353 | } |
||
| 354 |
Adding a
@returnannotation 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.