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 | * @var string |
||
| 23 | */ |
||
| 24 | protected $contentType = null; |
||
| 25 | |||
| 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 = []) |
||
| 67 | |||
| 68 | protected function initContentType() |
||
| 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 | /** |
||
| 111 | * Return Contentful Asset for given link (sys or ID). |
||
| 112 | * |
||
| 113 | * @param array|string|null $link |
||
| 114 | * @return \Distilleries\Contentful\Models\Asset|null |
||
| 115 | */ |
||
| 116 | protected function contentfulAsset($link): ?Asset |
||
| 133 | |||
| 134 | protected function getAndSetPayloadContentfulEntries(string $payload, array $links, $query = null): Collection |
||
| 148 | |||
| 149 | protected function getAndSetPayloadContentfulEntry(string $payload, array $links, $query = null): ?ContentfulModel |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return Contentful Entry for given link (sys or ID). |
||
| 166 | * |
||
| 167 | * @param array|string|null $link |
||
| 168 | * @param callback|null $query |
||
| 169 | * @return \Distilleries\Contentful\Models\Base\ContentfulModel|null |
||
| 170 | */ |
||
| 171 | protected function contentfulEntry($link, $query = null): ?ContentfulModel |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Return Contentful Entries for given ID. |
||
| 187 | * |
||
| 188 | * @param array $links |
||
| 189 | * @param callback|null $query |
||
| 190 | * @return \Illuminate\Support\Collection |
||
| 191 | */ |
||
| 192 | protected function contentfulEntries(array $links, $query = null): Collection |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Return a collection of related models for base Contentful ID. |
||
| 254 | * |
||
| 255 | * @param string $contentfulId |
||
| 256 | * @param string $contentfulType |
||
| 257 | * @return \Illuminate\Support\Collection |
||
| 258 | */ |
||
| 259 | protected function contentfulRelatedEntries(string $contentfulId, string $contentfulType = ''): Collection |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return Contentful link ID. |
||
| 301 | * |
||
| 302 | * @param mixed $link |
||
| 303 | * @return string|null |
||
| 304 | */ |
||
| 305 | protected function contentfulLinkId($link): ?string |
||
| 324 | |||
| 325 | public function getContentType(): string |
||
| 329 | |||
| 330 | public function getIdAttribute() |
||
| 334 | |||
| 335 | public function toArray() |
||
| 347 | |||
| 348 | } |
||
| 349 |
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.