Complex classes like Attachment 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 Attachment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | final class Attachment implements JsonSerializable |
||
16 | { |
||
17 | /** |
||
18 | * A plain-text summary of the attachment. |
||
19 | * This text will be used in clients that don't show formatted text (eg. IRC, mobile notifications). |
||
20 | * It should not contain any markup. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private $fallback; |
||
25 | |||
26 | /** |
||
27 | * This is the main text in a message attachment, and can contain standard message markup. |
||
28 | * The content will automatically collapse if it contains 700+ characters or 5+ linebreaks, |
||
29 | * and will display a "Show more..." link to expand the content. |
||
30 | * |
||
31 | * @var string|null |
||
32 | */ |
||
33 | private $text = null; |
||
34 | |||
35 | /** |
||
36 | * This value is used to color the border along the left side of the message attachment. |
||
37 | * |
||
38 | * @var Colour|null |
||
39 | */ |
||
40 | private $colour = null; |
||
41 | |||
42 | /** |
||
43 | * This is optional text that appears above the message attachment block. |
||
44 | * |
||
45 | * @var string|null |
||
46 | */ |
||
47 | private $pretext = null; |
||
48 | |||
49 | /** |
||
50 | * The author parameters will display a small section at the top of a message attachment. |
||
51 | * |
||
52 | * @var Author|null |
||
53 | */ |
||
54 | private $author = null; |
||
55 | |||
56 | /** |
||
57 | * The title is displayed as larger, bold text near the top of a message attachment. |
||
58 | * By passing a valid URL in the link parameter (optional), the title will be hyperlinked. |
||
59 | * |
||
60 | * @var Title|null |
||
61 | */ |
||
62 | private $title = null; |
||
63 | |||
64 | /** |
||
65 | * Will be displayed in a table inside the message attachment. |
||
66 | * |
||
67 | * @var Field[] |
||
68 | */ |
||
69 | private $fields = []; |
||
70 | |||
71 | /** |
||
72 | * A valid URL to an image file that will be displayed inside a message attachment. |
||
73 | * We currently support the following formats: GIF, JPEG, PNG, and BMP. |
||
74 | * Large images will be resized to a maximum width of 400px or a maximum height of 500px, |
||
75 | * while still maintaining the original aspect ratio. |
||
76 | * |
||
77 | * @var Url|null |
||
78 | */ |
||
79 | private $image = null; |
||
80 | |||
81 | /** |
||
82 | * A valid URL to an image file that will be displayed as a thumbnail on the right side of a message attachment. |
||
83 | * We currently support the following formats: GIF, JPEG, PNG, and BMP. |
||
84 | * The thumbnail's longest dimension will be scaled down to 75px while maintaining the aspect ratio of the image. |
||
85 | * The filesize of the image must also be less than 500 KB. |
||
86 | * |
||
87 | * @var Url|null |
||
88 | */ |
||
89 | private $thumbnail = null; |
||
90 | |||
91 | /** |
||
92 | * By default bot message text will be formatted, but attachments are not. |
||
93 | * Attachments need to enable the markdown manually. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | private $markdownIn = []; |
||
98 | |||
99 | /** |
||
100 | * Attachment constructor. |
||
101 | * I opted for setters since there are so many optional parameters. |
||
102 | * A new instance would look be messy if you only needed the last optional parameter. |
||
103 | * |
||
104 | * @param string $fallback |
||
105 | */ |
||
106 | 10 | public function __construct($fallback) |
|
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | 10 | public function getFallback() |
|
122 | |||
123 | /** |
||
124 | * @param string $text |
||
125 | * |
||
126 | * @return self |
||
127 | */ |
||
128 | 1 | public function setText($text) |
|
134 | |||
135 | /** |
||
136 | * @return bool |
||
137 | */ |
||
138 | 10 | public function hasText() |
|
142 | |||
143 | /** |
||
144 | * @return string|null |
||
145 | */ |
||
146 | 1 | public function getText() |
|
150 | |||
151 | /** |
||
152 | * @param Colour $colour |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | 1 | public function setColour(Colour $colour) |
|
162 | |||
163 | /** |
||
164 | * @return bool |
||
165 | */ |
||
166 | 10 | public function hasColour() |
|
170 | |||
171 | /** |
||
172 | * @return Colour|null |
||
173 | */ |
||
174 | 1 | public function getColour() |
|
178 | |||
179 | /** |
||
180 | * @param string $pretext |
||
181 | * |
||
182 | * @return self |
||
183 | */ |
||
184 | 1 | public function setPretext($pretext) |
|
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | 10 | public function hasPretext() |
|
198 | |||
199 | /** |
||
200 | * @return string|null |
||
201 | */ |
||
202 | 1 | public function getPretext() |
|
206 | |||
207 | /** |
||
208 | * @param Author $author |
||
209 | * |
||
210 | * @return self |
||
211 | */ |
||
212 | 1 | public function setAuthor(Author $author) |
|
218 | |||
219 | /** |
||
220 | * @return bool |
||
221 | */ |
||
222 | 10 | public function hasAuthor() |
|
226 | |||
227 | /** |
||
228 | * @return Author|null |
||
229 | */ |
||
230 | 1 | public function getAuthor() |
|
234 | |||
235 | /** |
||
236 | * @param Title $title |
||
237 | * |
||
238 | * @return self |
||
239 | */ |
||
240 | 1 | public function setTitle(Title $title) |
|
246 | |||
247 | /** |
||
248 | * @return bool |
||
249 | */ |
||
250 | 10 | public function hasTitle() |
|
254 | |||
255 | /** |
||
256 | * @return Title|null |
||
257 | */ |
||
258 | 1 | public function getTitle() |
|
262 | |||
263 | /** |
||
264 | * @param Field $field |
||
265 | */ |
||
266 | 1 | public function addField(Field $field) |
|
270 | |||
271 | /** |
||
272 | * @param Field[] $fields |
||
273 | */ |
||
274 | 1 | public function addFields(array $fields) |
|
280 | |||
281 | /** |
||
282 | * @return bool |
||
283 | */ |
||
284 | 10 | public function hasFields() |
|
288 | |||
289 | /** |
||
290 | * @return Field[] |
||
291 | */ |
||
292 | 1 | public function getFields() |
|
296 | |||
297 | /** |
||
298 | * @param Url $image |
||
299 | * |
||
300 | * @return self |
||
301 | */ |
||
302 | 1 | public function setImage(Url $image) |
|
308 | |||
309 | /** |
||
310 | * @return bool |
||
311 | */ |
||
312 | 10 | public function hasImage() |
|
316 | |||
317 | /** |
||
318 | * @return Url|null |
||
319 | */ |
||
320 | 1 | public function getImage() |
|
324 | |||
325 | /** |
||
326 | * @param Url $thumbnail |
||
327 | * |
||
328 | * @return self |
||
329 | */ |
||
330 | 1 | public function setThumbnail(Url $thumbnail) |
|
336 | |||
337 | /** |
||
338 | * @return bool |
||
339 | */ |
||
340 | 10 | public function hasThumbnail() |
|
344 | |||
345 | /** |
||
346 | * @return Url|null |
||
347 | */ |
||
348 | 1 | public function getThumbnail() |
|
352 | |||
353 | /** |
||
354 | * Enable markdown in the pretext. |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | 1 | public function enableMarkdownForPretext() |
|
364 | |||
365 | /** |
||
366 | * Enable markdown in the text. |
||
367 | * |
||
368 | * @return $this |
||
369 | */ |
||
370 | 1 | public function enableMarkdownForText() |
|
376 | |||
377 | /** |
||
378 | * Enable markdown for the value in the fields. |
||
379 | * |
||
380 | * @return $this |
||
381 | */ |
||
382 | 1 | public function enableMarkdownForFieldValues() |
|
388 | |||
389 | /** |
||
390 | * @inheritDoc |
||
391 | */ |
||
392 | 10 | public function jsonSerialize() |
|
396 | |||
397 | /** |
||
398 | * Get the attachment in the format that slack requires. |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | 10 | public function get() |
|
420 | |||
421 | /** |
||
422 | * If set, add the colour to the attachment. |
||
423 | * |
||
424 | * @param array $attachment |
||
425 | * |
||
426 | * @return array |
||
427 | */ |
||
428 | 10 | private function addColourToAttachment(array $attachment) |
|
438 | |||
439 | /** |
||
440 | * If set, add the text to the attachment. |
||
441 | * |
||
442 | * @param array $attachment |
||
443 | * |
||
444 | * @return array |
||
445 | */ |
||
446 | 10 | private function addTextToAttachment(array $attachment) |
|
456 | |||
457 | /** |
||
458 | * If set, add the pretext to the attachment. |
||
459 | * |
||
460 | * @param array $attachment |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | 10 | private function addPretextToAttachment(array $attachment) |
|
474 | |||
475 | /** |
||
476 | * If set, add the author to the attachment. |
||
477 | * |
||
478 | * @param array $attachment |
||
479 | * |
||
480 | * @return array |
||
481 | */ |
||
482 | 10 | private function addAuthorToAttachment(array $attachment) |
|
500 | |||
501 | /** |
||
502 | * If set, add the title to the attachment. |
||
503 | * |
||
504 | * @param array $attachment |
||
505 | * |
||
506 | * @return array |
||
507 | */ |
||
508 | 10 | private function addTitleToAttachment(array $attachment) |
|
522 | |||
523 | /** |
||
524 | * If set, add the fields to the attachment. |
||
525 | * |
||
526 | * @param array $attachment |
||
527 | * |
||
528 | * @return array |
||
529 | */ |
||
530 | 10 | private function addFieldsToAttachment(array $attachment) |
|
540 | |||
541 | /** |
||
542 | * If set, add the image to the attachment. |
||
543 | * |
||
544 | * @param array $attachment |
||
545 | * |
||
546 | * @return array |
||
547 | */ |
||
548 | 10 | private function addImageToAttachment(array $attachment) |
|
558 | |||
559 | /** |
||
560 | * If set, add the thumbnail to the attachment. |
||
561 | * |
||
562 | * @param array $attachment |
||
563 | * |
||
564 | * @return array |
||
565 | */ |
||
566 | 10 | private function addThumbnailToAttachment(array $attachment) |
|
576 | |||
577 | /** |
||
578 | * If set, add the fields to the attachment. |
||
579 | * |
||
580 | * @param array $attachment |
||
581 | * |
||
582 | * @return array |
||
583 | */ |
||
584 | 10 | private function addMarkdownSettingsToAttachment(array $attachment) |
|
594 | } |
||
595 |