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 |
||
19 | class SeoPage implements SeoPageInterface, PageWithStructuredData |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $title; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $metas; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $htmlAttributes; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $linkCanonical; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $separator; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $headAttributes; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $langAlternates; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $oembedLinks; |
||
60 | |||
61 | /** |
||
62 | * A JSON-LD string, which can be served as structured data. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $structuredData; |
||
67 | |||
68 | /** |
||
69 | * @param string $title |
||
70 | */ |
||
71 | public function __construct($title = '') |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function setTitle($title) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function addTitle($title) |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function getTitle() |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function getMetas() |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function addMeta($type, $name, $content, array $extras = []) |
||
140 | |||
141 | /** |
||
142 | * @param string $type |
||
143 | * @param string $name |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function hasMeta($type, $name) |
||
151 | |||
152 | /** |
||
153 | * @param string $type |
||
154 | * @param string $name |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function removeMeta($type, $name) |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function setMetas(array $metadatas) |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function setHtmlAttributes(array $attributes) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function addHtmlAttributes($name, $value) |
||
206 | |||
207 | /** |
||
208 | * @param string $name |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function removeHtmlAttributes($name) |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function getHtmlAttributes() |
||
226 | |||
227 | /** |
||
228 | * @param string $name |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function hasHtmlAttribute($name) |
||
236 | |||
237 | /** |
||
238 | * @param array $attributes |
||
239 | * |
||
240 | * @return SeoPageInterface |
||
241 | */ |
||
242 | public function setHeadAttributes(array $attributes) |
||
248 | |||
249 | /** |
||
250 | * @param string $name |
||
251 | * @param string $value |
||
252 | * |
||
253 | * @return SeoPageInterface |
||
254 | */ |
||
255 | public function addHeadAttribute($name, $value) |
||
261 | |||
262 | /** |
||
263 | * @param string $name |
||
264 | * |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function removeHeadAttribute($name) |
||
273 | |||
274 | /** |
||
275 | * @return array |
||
276 | */ |
||
277 | public function getHeadAttributes() |
||
281 | |||
282 | /** |
||
283 | * @param string $name |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function hasHeadAttribute($name) |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function setLinkCanonical($link) |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function getLinkCanonical() |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | public function removeLinkCanonical() |
||
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | public function setSeparator($separator) |
||
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | public function setLangAlternates(array $langAlternates) |
||
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | public function addLangAlternate($href, $hrefLang) |
||
347 | |||
348 | /** |
||
349 | * @param string $href |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function removeLangAlternate($href) |
||
359 | |||
360 | /** |
||
361 | * @param string $href |
||
362 | * |
||
363 | * @return $this |
||
364 | */ |
||
365 | public function hasLangAlternate($href) |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function getLangAlternates() |
||
377 | |||
378 | /** |
||
379 | * @param $title |
||
380 | * @param $link |
||
381 | * |
||
382 | * @return SeoPageInterface |
||
383 | */ |
||
384 | public function addOEmbedLink($title, $link) |
||
390 | |||
391 | /** |
||
392 | * @return array |
||
393 | */ |
||
394 | public function getOEmbedLinks() |
||
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | public function getStructuredData() |
||
403 | { |
||
404 | return $this->structuredData; |
||
405 | } |
||
406 | |||
407 | public function setStructuredData($structuredData) |
||
408 | { |
||
409 | $this->structuredData = $structuredData; |
||
410 | |||
411 | return $this; |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @param mixed $meta |
||
416 | * |
||
417 | * @return array |
||
418 | */ |
||
419 | private function normalize($meta) |
||
420 | { |
||
421 | if (\is_string($meta)) { |
||
422 | return [$meta, []]; |
||
423 | } |
||
424 | |||
425 | return $meta; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @param mixed $meta |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | private function normalize($meta) |
||
441 | } |
||
442 |