Complex classes like MetatagTrait 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 MetatagTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait MetatagTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Translation|string|null |
||
| 16 | */ |
||
| 17 | protected $metaTitle; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Translation|string|null |
||
| 21 | */ |
||
| 22 | protected $metaDescription; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Translation|string|null |
||
| 26 | */ |
||
| 27 | protected $metaImage; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Translation|string|null |
||
| 31 | */ |
||
| 32 | protected $metaAuthor; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Translation|string|null |
||
| 36 | */ |
||
| 37 | protected $metaTags; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string $facebookAppId |
||
| 41 | */ |
||
| 42 | protected $facebookAppId; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Translation|string|null |
||
| 46 | */ |
||
| 47 | protected $opengraphTitle; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Translation|string|null |
||
| 51 | */ |
||
| 52 | protected $opengraphSiteName; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Translation|string|null |
||
| 56 | */ |
||
| 57 | protected $opengraphDescription; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $opengraphType; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Translation|string|null |
||
| 66 | */ |
||
| 67 | protected $opengraphImage; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Translation|string|null |
||
| 71 | */ |
||
| 72 | protected $opengraphAuthor; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Translation|string|null |
||
| 76 | */ |
||
| 77 | protected $opengraphPublisher; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Translation|string|null |
||
| 81 | */ |
||
| 82 | protected $opengraphTags; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Translation|string|null |
||
| 86 | */ |
||
| 87 | protected $twitterCardImage; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var Translation|string|null |
||
| 91 | */ |
||
| 92 | protected $twitterCardType; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | abstract public function canonicalUrl(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return Translation|string|null |
||
| 101 | */ |
||
| 102 | abstract public function defaultMetaTitle(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return Translation|string|null |
||
| 106 | */ |
||
| 107 | abstract public function defaultMetaDescription(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return Translation|string|null |
||
| 111 | */ |
||
| 112 | abstract public function defaultMetaImage(); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param mixed $title The meta tile (localized). |
||
| 116 | * @return self |
||
| 117 | */ |
||
| 118 | public function setMetaTitle($title) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return Translation|string|null |
||
| 126 | */ |
||
| 127 | public function metaTitle() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param mixed $description The meta description (localized). |
||
| 134 | * @return self |
||
| 135 | */ |
||
| 136 | public function setMetaDescription($description) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return Translation|string|null |
||
| 144 | */ |
||
| 145 | public function metaDescription() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param mixed $image The meta image (localized). |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function setMetaImage($image) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return Translation|string|null |
||
| 162 | */ |
||
| 163 | public function metaImage() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param mixed $author The meta author (localized). |
||
| 170 | * @return self |
||
| 171 | */ |
||
| 172 | public function setMetaAuthor($author) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return Translation|string|null |
||
| 180 | */ |
||
| 181 | public function metaAuthor() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $tags Comma separated list of tags. |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function setMetaTags($tags) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function metaTags() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $appId The facebook App ID (numeric string). |
||
| 206 | * @return self |
||
| 207 | */ |
||
| 208 | public function setFacebookAppId($appId) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function facebookAppId() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param mixed $title The opengraph title (localized). |
||
| 224 | * @return self |
||
| 225 | */ |
||
| 226 | public function setOpengraphTitle($title) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the opengraph title. |
||
| 234 | * |
||
| 235 | * If not expilicitely defined, use the meta title as opengraph title. |
||
| 236 | * |
||
| 237 | * @return Translation|string|null |
||
| 238 | */ |
||
| 239 | public function opengraphTitle() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param mixed $siteName The site name (localized). |
||
| 249 | * @return self |
||
| 250 | */ |
||
| 251 | public function setOpengraphSiteName($siteName) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return Translation|string|null |
||
| 259 | */ |
||
| 260 | public function opengraphSiteName() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param mixed $description The opengraph description (localized). |
||
| 267 | * @return self |
||
| 268 | */ |
||
| 269 | public function setOpengraphDescription($description) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return Translation|string|null |
||
| 277 | */ |
||
| 278 | public function opengraphDescription() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $type The opengraph type. |
||
| 288 | * @return self |
||
| 289 | */ |
||
| 290 | public function setOpengraphType($type) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public function opengraphType() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param mixed $image The opengraph image (localized). |
||
| 306 | * @return self |
||
| 307 | */ |
||
| 308 | public function setOpengraphImage($image) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return Translation|string|null |
||
| 316 | */ |
||
| 317 | public function opengraphImage() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param mixed $author The opengraph author (localized). |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function setOpengraphAuthor($author) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return Translation|string|null |
||
| 337 | */ |
||
| 338 | public function opengraphAuthor() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param mixed $publisher The opengraph publisher (localized). |
||
| 348 | * @return self |
||
| 349 | */ |
||
| 350 | public function setOpengraphPublisher($publisher) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return Translation|string|null |
||
| 358 | */ |
||
| 359 | public function opengraphPublisher() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $type The twitter card type. |
||
| 369 | * @return self |
||
| 370 | */ |
||
| 371 | public function setTwitterCardType($type) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Retrieve the object's {@link https://dev.twitter.com/cards/types card type}, |
||
| 379 | * for the "twitter:card" meta-property. |
||
| 380 | * |
||
| 381 | * @return string|null |
||
| 382 | */ |
||
| 383 | public function twitterCardType() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param mixed $image The twitter card image (localized). |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | public function setTwitterCardImage($image) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Retrieve the URL to the object's social image for the "twitter:image" meta-property. |
||
| 400 | * |
||
| 401 | * @return string|null |
||
| 402 | */ |
||
| 403 | public function twitterCardImage() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Generates the default metatags for the current object. |
||
| 410 | * Prevents some problem where the defaultMetaTag method |
||
| 411 | * content isn't set at the moment of setting the meta. |
||
| 412 | * Should be called on preSave and preUpdate of the object. |
||
| 413 | * |
||
| 414 | * @return self $this. |
||
| 415 | */ |
||
| 416 | public function generateDefaultMetaTags() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Check if the meta is empty. Method exists |
||
| 432 | * because at this point we don't really know |
||
| 433 | * what's in the meta. |
||
| 434 | * Possible param type: |
||
| 435 | * - [ lang => value, lang => value ] |
||
| 436 | * - Translation |
||
| 437 | * - null |
||
| 438 | * |
||
| 439 | * @param mixed $meta Current meta value. |
||
| 440 | * @return boolean Empty or not. |
||
| 441 | */ |
||
| 442 | public function isEmptyMeta($meta) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return Translator |
||
| 470 | */ |
||
| 471 | abstract protected function translator(); |
||
| 472 | } |
||
| 473 |