Complex classes like SeoPage 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 SeoPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class SeoPage implements SeoPageInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $title; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $metas; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $htmlAttributes; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $separator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $headAttributes; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $langAlternates; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $oembedLinks; |
||
| 53 | |||
| 54 | /** @var array $links */ |
||
| 55 | protected $links; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $title |
||
| 59 | */ |
||
| 60 | public function __construct($title = '') |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | public function setTitle($title) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | public function addTitle($title) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function getTitle() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function getMetas() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function addMeta($type, $name, $content, array $extras = array()) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $type |
||
| 130 | * @param string $name |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function hasMeta($type, $name) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $type |
||
| 141 | * @param string $name |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function removeMeta($type, $name) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function setMetas(array $metadatas) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param mixed $meta |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private function normalize($meta) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function setHtmlAttributes(array $attributes) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function addHtmlAttributes($name, $value) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $name |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function removeHtmlAttributes($name) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | public function getHtmlAttributes() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $name |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function hasHtmlAttribute($name) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param array $attributes |
||
| 240 | * |
||
| 241 | * @return SeoPageInterface |
||
| 242 | */ |
||
| 243 | public function setHeadAttributes(array $attributes) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $name |
||
| 252 | * @param string $value |
||
| 253 | * |
||
| 254 | * @return SeoPageInterface |
||
| 255 | */ |
||
| 256 | public function addHeadAttribute($name, $value) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $name |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function removeHeadAttribute($name) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function getHeadAttributes() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $name |
||
| 285 | * |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | public function hasHeadAttribute($name) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function setLinkCanonical($link) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | public function getLinkCanonical() |
||
| 307 | { |
||
| 308 | $canonical = $this->getLink('canonical'); |
||
| 309 | |||
| 310 | if (!empty($canonical)) { |
||
| 311 | return $canonical[0]['href']; |
||
| 312 | } |
||
| 313 | |||
| 314 | return ''; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritdoc} |
||
| 319 | */ |
||
| 320 | public function removeLinkCanonical() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function setSeparator($separator) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritdoc} |
||
| 337 | */ |
||
| 338 | public function setLangAlternates(array $langAlternates) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function addLangAlternate($href, $hrefLang) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $href |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function removeLangAlternate($href) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $href |
||
| 369 | * |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function hasLangAlternate($href) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * {@inheritdoc} |
||
| 379 | */ |
||
| 380 | public function getLangAlternates() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param $title |
||
| 387 | * @param $link |
||
| 388 | * |
||
| 389 | * @return SeoPageInterface |
||
| 390 | */ |
||
| 391 | public function addOEmbedLink($title, $link) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | public function getOEmbedLinks() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * {@inheritdoc} |
||
| 408 | */ |
||
| 409 | public function addLink($type, array $attributes) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | public function setLink($type, array $attributes) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | public function getLink($type) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * {@inheritdoc} |
||
| 440 | */ |
||
| 441 | public function getLinks() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * {@inheritdoc} |
||
| 448 | */ |
||
| 449 | public function removeLink($type) |
||
| 459 | } |
||
| 460 |