Complex classes like Content 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 Content, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Content |
||
8 | { |
||
9 | const SHARE_TYPE_FACEBOOK = 'facebook'; |
||
10 | const SHARE_TYPE_GOOGLE = 'google'; |
||
11 | const SHARE_TYPE_ODNOKLASSNIKI = 'odnoklassniki'; |
||
12 | const SHARE_TYPE_TELEGRAM = 'telegram'; |
||
13 | const SHARE_TYPE_TWITTER = 'twitter'; |
||
14 | const SHARE_TYPE_VKONTAKTE = 'vkontakte'; |
||
15 | |||
16 | const SLIDER_DATA_VIEW_SQUARE = 'square'; |
||
17 | const SLIDER_DATA_VIEW_PORTRAIT = 'portrait'; |
||
18 | const SLIDER_DATA_VIEW_LANDSCAPE = 'landscape'; |
||
19 | |||
20 | const SLIDER_DATA_ITEM_VIEW_COVER = 'cover'; |
||
21 | const SLIDER_DATA_ITEM_VIEW_CONTAIN = 'contain'; |
||
22 | |||
23 | const OWN_VIDEO_TYPE_MP4 = 'video/mp4'; |
||
24 | |||
25 | const ADDITIONAL_CONTENT_ORIENTATION_VERTICAL = 'vertical'; |
||
26 | const ADDITIONAL_CONTENT_ORIENTATION_HORIZONTAL = 'horizontal'; |
||
27 | |||
28 | const ADDITIONAL_CONTENT_THUMB_POSITION_LEFT = 'left'; |
||
29 | const ADDITIONAL_CONTENT_THUMB_POSITION_RIGHT = 'right'; |
||
30 | const ADDITIONAL_CONTENT_THUMB_POSITION_TOP = 'top'; |
||
31 | |||
32 | const ADDITIONAL_CONTENT_THUMB_RATIO_1_1 = '1x1'; |
||
33 | const ADDITIONAL_CONTENT_THUMB_RATIO_2_3 = '2x3'; |
||
34 | const ADDITIONAL_CONTENT_THUMB_RATIO_3_2 = '3x2'; |
||
35 | const ADDITIONAL_CONTENT_THUMB_RATIO_3_4 = '3x4'; |
||
36 | const ADDITIONAL_CONTENT_THUMB_RATIO_4_3 = '4x3'; |
||
37 | const ADDITIONAL_CONTENT_THUMB_RATIO_16_9 = '16x9'; |
||
38 | const ADDITIONAL_CONTENT_THUMB_RATIO_16_10 = '16x10'; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Generate header element |
||
43 | * @param string $h1 |
||
44 | * @param string|null $h2 |
||
45 | * @param string|null $imgUrl |
||
46 | * @param string|null $imgCaption |
||
47 | * @param array|null $menuArray array of arrays with pairs of url and content |
||
48 | * [ |
||
49 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
50 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
51 | * ] |
||
52 | * @return string |
||
53 | */ |
||
54 | 12 | public static function header(string $h1, string $h2 = null, string $imgUrl = null, |
|
64 | |||
65 | /** |
||
66 | * Generate image element |
||
67 | * @param string $imgUrl |
||
68 | * @param string|null $imgCaption |
||
69 | * @return string |
||
70 | */ |
||
71 | 12 | public static function img(string $imgUrl, string $imgCaption = null): string |
|
79 | |||
80 | /** |
||
81 | * Generate video element |
||
82 | * @param string $videoUrl |
||
83 | * @param string|null $videoCaption |
||
84 | * @param string $imgUrl |
||
85 | * @param string $type |
||
86 | * @return string |
||
87 | */ |
||
88 | 8 | public static function ownVideo(string $videoUrl, string $videoCaption = null, string $type = self::OWN_VIDEO_TYPE_MP4, string $imgUrl = null): string |
|
96 | |||
97 | /** |
||
98 | * Generate video element for external video |
||
99 | * @param string $videoUrl |
||
100 | * @param array $options Options Array with next variables: width, height, frameborder, allowfullscreen, |
||
101 | * referrerpolicy, sandbox, hd. Options example: |
||
102 | * [ |
||
103 | * 'width' => 640, |
||
104 | * 'height' => 480, |
||
105 | * 'frameborder' => 1, |
||
106 | * 'allowfullscreen' => 'true', |
||
107 | * 'referrerpolicy' => 'unsafe-url', |
||
108 | * 'sandbox' => 'allow-forms allow-modals', |
||
109 | * 'hd' => 3 |
||
110 | * ] |
||
111 | * @return string |
||
112 | */ |
||
113 | 8 | public static function externalVideo(string $videoUrl, array $options = []): string |
|
125 | |||
126 | /** |
||
127 | * Generate images gallery |
||
128 | * @param array $imagesArray Array of images urls |
||
129 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
130 | * @param string|null $header |
||
131 | * @return string |
||
132 | */ |
||
133 | 8 | public static function gallery(array $imagesArray, string $header = null): string |
|
143 | |||
144 | /** |
||
145 | * Generate media slider |
||
146 | * @param array $itemsArray Array of items with data |
||
147 | * [ |
||
148 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
149 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
150 | * ['url' => 'http://example.com/image3.jpg'], |
||
151 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
152 | * ] |
||
153 | * @param string|null $header |
||
154 | * @param string $dataView |
||
155 | * @param string $dataItemView |
||
156 | * @return string |
||
157 | */ |
||
158 | 8 | public static function slider(array $itemsArray, string $header = null, |
|
169 | |||
170 | /** |
||
171 | * Generate share block |
||
172 | * @param array|null $networks Array of network names |
||
173 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
174 | * Can be empty, in this way all possible network types will be showed. |
||
175 | * @return string |
||
176 | */ |
||
177 | 8 | public static function share(array $networks = null): string |
|
185 | |||
186 | /** |
||
187 | * Generate rating block |
||
188 | * @param float $currentRating |
||
189 | * @param float $maxRating |
||
190 | * @return string |
||
191 | */ |
||
192 | 16 | public static function rating(float $currentRating, float $maxRating): string |
|
203 | |||
204 | /** |
||
205 | * Generate button |
||
206 | * @param string $text |
||
207 | * @param string $url |
||
208 | * @param string $phone Phone number in RFC-3966 format |
||
209 | * @param string|null $buttonColor Can be Text or HEX |
||
210 | * @param string|null $textColor Can be Text or HEX |
||
211 | * @param bool $isBoldText |
||
212 | * @param bool $isDisabled |
||
213 | * @return string |
||
214 | */ |
||
215 | 16 | public static function button(string $text, string $url = '', string $phone = '', |
|
236 | |||
237 | /** |
||
238 | * Generate comment block |
||
239 | * @param string $url URL to comments page |
||
240 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
241 | * can has unlimited includes, example: |
||
242 | * [ |
||
243 | * [ |
||
244 | * 'author' => 'First Author Name', |
||
245 | * 'avatar' => 'http://example.com/user1.jpg', |
||
246 | * 'title' => 'Comment Title', |
||
247 | * 'subtitle' => '2017-12-10', |
||
248 | * 'content' => 'Somme comment text', |
||
249 | * 'comments' => [ |
||
250 | * [ |
||
251 | * 'author' => 'Third Author Name', |
||
252 | * 'avatar' => 'http://example.com/user3.jpg', |
||
253 | * 'title' => 'Comment Title', |
||
254 | * 'subtitle' => '2017-12-12', |
||
255 | * 'content' => 'Some answer text' |
||
256 | * ], |
||
257 | * [ |
||
258 | * 'author' => 'Another Author Name', |
||
259 | * 'avatar' => 'http://example.com/user4.jpg', |
||
260 | * 'title' => 'Comment Title', |
||
261 | * 'subtitle' => '2017-12-13', |
||
262 | * 'content' => 'Another answer text' |
||
263 | * ], |
||
264 | * ] |
||
265 | * ], |
||
266 | * [ |
||
267 | * 'author' => 'Second Author Name', |
||
268 | * 'avatar' => 'http://example.com/user2.jpg', |
||
269 | * 'title' => 'Comment Title', |
||
270 | * 'subtitle' => '2017-12-11', |
||
271 | * 'content' => 'Some comment text' |
||
272 | * ], |
||
273 | * ] |
||
274 | * @return string |
||
275 | */ |
||
276 | 4 | public static function comment(string $url, array $commentsArray): string |
|
282 | |||
283 | /** |
||
284 | * Generate accordion |
||
285 | * @param array $accordionArray array accordion elements |
||
286 | * [ |
||
287 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
288 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
289 | * ] |
||
290 | * @return string |
||
291 | */ |
||
292 | 4 | public static function accordion(array $accordionArray): string |
|
305 | |||
306 | /** |
||
307 | * Generate Ad block position element |
||
308 | * @param string $turboAdId value of $turboAdId used in Channel() class |
||
309 | * @return string |
||
310 | * |
||
311 | * @see Channel::$adTurboAdId |
||
312 | */ |
||
313 | 4 | public static function adBlockPosition(string $turboAdId): string |
|
317 | |||
318 | /** |
||
319 | * Generate block with additional content |
||
320 | * @param array $itemsArray Array of items with data |
||
321 | * [ |
||
322 | * [ |
||
323 | * 'href' => 'http://example.com/page1.html', |
||
324 | * 'title' => 'Item title 1', |
||
325 | * 'description' => 'Item description', |
||
326 | * 'thumb' => 'http://example/image1.jpg', |
||
327 | * 'thumb_position' => Content::ADDITIONAL_CONTENT_THUMB_POSITION_LEFT, |
||
328 | * 'thumb_ratio' => Content::ADDITIONAL_CONTENT_THUMB_RATIO_1_1 |
||
329 | * ], |
||
330 | * [ |
||
331 | * 'href' => 'http://example.com/page2.html', |
||
332 | * 'title' => 'Item title 2' |
||
333 | * ], |
||
334 | * ] |
||
335 | * @param string|null $title |
||
336 | * @param string|null $orientation |
||
337 | * @return string |
||
338 | * @throws \Exception |
||
339 | */ |
||
340 | 16 | public static function additionalContent(array $itemsArray, string $title = null, string $orientation = null): string |
|
351 | |||
352 | /** |
||
353 | * Generate inline callback form |
||
354 | * @param string $recipientEmail |
||
355 | * @param string|null $companyName |
||
356 | * @param string|null $linkToAgreement |
||
357 | * @return string |
||
358 | * @throws \Exception |
||
359 | */ |
||
360 | 16 | public static function inlineCallbackForm(string $recipientEmail, string $companyName = null, string $linkToAgreement = null): string |
|
373 | |||
374 | /** |
||
375 | * Generate modal callback form |
||
376 | * @param string $recipientEmail |
||
377 | * @param string $buttonText |
||
378 | * @param string|null $companyName |
||
379 | * @param string|null $linkToAgreement |
||
380 | * @param string|null $buttonColor Can be Text or HEX |
||
381 | * @param string|null $textColor Can be Text or HEX |
||
382 | * @param bool $isBoldText |
||
383 | * @param bool $isDisabled |
||
384 | * @return string |
||
385 | * @throws \Exception |
||
386 | */ |
||
387 | 16 | public static function modalCallbackForm(string $recipientEmail, string $buttonText, string $companyName = null, string $linkToAgreement = null, |
|
406 | |||
407 | /** |
||
408 | * Generate search input |
||
409 | * @param string $searchUrl Search engine URL in format https://example.com/search/{text} |
||
410 | * @param string $placeholder |
||
411 | * @return string |
||
412 | */ |
||
413 | 8 | public static function searchInput(string $searchUrl, string $placeholder = ''): string |
|
419 | |||
420 | /** |
||
421 | * Generate content block for media slider |
||
422 | * @param array $itemsArray Array of items with data |
||
423 | * [ |
||
424 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
425 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
426 | * ['url' => 'http://example.com/image3.jpg'], |
||
427 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
428 | * ] |
||
429 | * @return string |
||
430 | */ |
||
431 | 8 | private static function generateSliderItemsBlock(array $itemsArray): string |
|
453 | |||
454 | 4 | private static function generateCommentBlock(array $commentsArray): string |
|
484 | |||
485 | /** |
||
486 | * Generate header menu |
||
487 | * @param array $menuArray array of arrays with pairs of url and title |
||
488 | * [ |
||
489 | * ['url' => 'http://example.com/page1.html', 'title' => 'Page title 1'], |
||
490 | * ['url' => 'http://example.com/page2.html', 'title' => 'Page title 2'], |
||
491 | * ] |
||
492 | * @return string |
||
493 | */ |
||
494 | 4 | private static function generateMenu(array $menuArray): string |
|
504 | |||
505 | /** |
||
506 | * Generate additional content items list |
||
507 | * @param array $itemsArray array of arrays with data of additional content items |
||
508 | * [ |
||
509 | * [ |
||
510 | * 'href' => 'http://example.com/page1.html', |
||
511 | * 'title' => 'Item title 1', |
||
512 | * 'description' => 'Item description', |
||
513 | * 'thumb' => 'http://example/image1.jpg', |
||
514 | * 'thumb_position' => Content::ADDITIONAL_CONTENT_THUMB_POSITION_LEFT, |
||
515 | * 'thumb_ratio' => Content::ADDITIONAL_CONTENT_THUMB_RATIO_1_1 |
||
516 | * ], |
||
517 | * [ |
||
518 | * 'href' => 'http://example.com/page2.html', |
||
519 | * 'title' => 'Item title 2' |
||
520 | * ], |
||
521 | * ] |
||
522 | * @return string |
||
523 | * @throws \Exception |
||
524 | */ |
||
525 | 16 | private static function generateAdditionalContentItemsList(array $itemsArray): string |
|
549 | } |