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 | * @return Translation|string|null |
||
234 | */ |
||
235 | public function opengraphTitle() |
||
239 | |||
240 | /** |
||
241 | * @param mixed $siteName The site name (localized). |
||
242 | * @return self |
||
243 | */ |
||
244 | public function setOpengraphSiteName($siteName) |
||
249 | |||
250 | /** |
||
251 | * @return Translation|string|null |
||
252 | */ |
||
253 | public function opengraphSiteName() |
||
257 | |||
258 | /** |
||
259 | * @param mixed $description The opengraph description (localized). |
||
260 | * @return self |
||
261 | */ |
||
262 | public function setOpengraphDescription($description) |
||
267 | |||
268 | /** |
||
269 | * @return Translation|string|null |
||
270 | */ |
||
271 | public function opengraphDescription() |
||
275 | |||
276 | /** |
||
277 | * @param string $type The opengraph type. |
||
278 | * @return self |
||
279 | */ |
||
280 | public function setOpengraphType($type) |
||
285 | |||
286 | /** |
||
287 | * @return string |
||
288 | */ |
||
289 | public function opengraphType() |
||
293 | |||
294 | /** |
||
295 | * @param mixed $image The opengraph image (localized). |
||
296 | * @return self |
||
297 | */ |
||
298 | public function setOpengraphImage($image) |
||
303 | |||
304 | /** |
||
305 | * @return Translation|string|null |
||
306 | */ |
||
307 | public function opengraphImage() |
||
311 | |||
312 | /** |
||
313 | * @param mixed $author The opengraph author (localized). |
||
314 | * @return self |
||
315 | */ |
||
316 | public function setOpengraphAuthor($author) |
||
321 | |||
322 | /** |
||
323 | * @return Translation|string|null |
||
324 | */ |
||
325 | public function opengraphAuthor() |
||
329 | |||
330 | /** |
||
331 | * @param mixed $publisher The opengraph publisher (localized). |
||
332 | * @return self |
||
333 | */ |
||
334 | public function setOpengraphPublisher($publisher) |
||
339 | |||
340 | /** |
||
341 | * @return Translation|string|null |
||
342 | */ |
||
343 | public function opengraphPublisher() |
||
347 | |||
348 | /** |
||
349 | * @param string $type The twitter card type. |
||
350 | * @return self |
||
351 | */ |
||
352 | public function setTwitterCardType($type) |
||
357 | |||
358 | /** |
||
359 | * Retrieve the object's {@link https://dev.twitter.com/cards/types card type}, |
||
360 | * for the "twitter:card" meta-property. |
||
361 | * |
||
362 | * @return string|null |
||
363 | */ |
||
364 | public function twitterCardType() |
||
368 | |||
369 | /** |
||
370 | * @param mixed $image The twitter card image (localized). |
||
371 | * @return self |
||
372 | */ |
||
373 | public function setTwitterCardImage($image) |
||
378 | |||
379 | /** |
||
380 | * Retrieve the URL to the object's social image for the "twitter:image" meta-property. |
||
381 | * |
||
382 | * @return string|null |
||
383 | */ |
||
384 | public function twitterCardImage() |
||
388 | |||
389 | /** |
||
390 | * Generates the default metatags for the current object. |
||
391 | * Prevents some problem where the defaultMetaTag method |
||
392 | * content isn't set at the moment of setting the meta. |
||
393 | * Should be called on preSave and preUpdate of the object. |
||
394 | * |
||
395 | * @return self $this. |
||
396 | */ |
||
397 | public function generateDefaultMetaTags() |
||
410 | |||
411 | /** |
||
412 | * Check if the meta is empty. Method exists |
||
413 | * because at this point we don't really know |
||
414 | * what's in the meta. |
||
415 | * Possible param type: |
||
416 | * - [ lang => value, lang => value ] |
||
417 | * - Translation |
||
418 | * - null |
||
419 | * |
||
420 | * @param mixed $meta Current meta value. |
||
421 | * @return boolean Empty or not. |
||
422 | */ |
||
423 | public function isEmptyMeta($meta) |
||
448 | |||
449 | /** |
||
450 | * @return Translator |
||
451 | */ |
||
452 | abstract protected function translator(); |
||
453 | } |
||
454 |