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 | * Generate header element |
||
42 | * @param string $h1 |
||
43 | * @param string|null $h2 |
||
44 | * @param string|null $imgUrl |
||
45 | * @param string|null $imgCaption |
||
46 | * @param array|null $menuArray array of arrays with pairs of url and content |
||
47 | * [ |
||
48 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
49 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
50 | * ] |
||
51 | * @return string |
||
52 | */ |
||
53 | 9 | public static function header(string $h1, string $h2 = null, string $imgUrl = null, |
|
63 | |||
64 | /** |
||
65 | * Generate image element |
||
66 | * @param string $imgUrl |
||
67 | * @param string|null $imgCaption |
||
68 | * @return string |
||
69 | */ |
||
70 | 9 | public static function img(string $imgUrl, string $imgCaption = null): string |
|
78 | |||
79 | /** |
||
80 | * Generate video element |
||
81 | * @param string $videoUrl |
||
82 | * @param string|null $videoCaption |
||
83 | * @param string $imgUrl |
||
84 | * @param string $type |
||
85 | * @return string |
||
86 | */ |
||
87 | 6 | public static function ownVideo(string $videoUrl, string $videoCaption = null, string $type = self::OWN_VIDEO_TYPE_MP4, string $imgUrl = null): string |
|
95 | |||
96 | /** |
||
97 | * Generate video element for external video |
||
98 | * @param string $videoUrl |
||
99 | * @param array $options Options Array with next variables: width, height, frameborder, allowfullscreen, |
||
100 | * referrerpolicy, sandbox, hd. Options example: |
||
101 | * [ |
||
102 | * 'width' => 640, |
||
103 | * 'height' => 480, |
||
104 | * 'frameborder' => 1, |
||
105 | * 'allowfullscreen' => 'true', |
||
106 | * 'referrerpolicy' => 'unsafe-url', |
||
107 | * 'sandbox' => 'allow-forms allow-modals', |
||
108 | * 'hd' => 3 |
||
109 | * ] |
||
110 | * @return string |
||
111 | */ |
||
112 | 6 | public static function externalVideo(string $videoUrl, array $options = []): string |
|
124 | |||
125 | /** |
||
126 | * Generate images gallery |
||
127 | * @param array $imagesArray Array of images urls |
||
128 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
129 | * @param string|null $header |
||
130 | * @return string |
||
131 | */ |
||
132 | 6 | public static function gallery(array $imagesArray, string $header = null): string |
|
142 | |||
143 | /** |
||
144 | * Generate media slider |
||
145 | * @param array $itemsArray Array of items with data |
||
146 | * [ |
||
147 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
148 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
149 | * ['url' => 'http://example.com/image3.jpg'], |
||
150 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
151 | * ] |
||
152 | * @param string|null $header |
||
153 | * @param string $dataView |
||
154 | * @param string $dataItemView |
||
155 | * @return string |
||
156 | */ |
||
157 | 6 | public static function slider(array $itemsArray, string $header = null, |
|
168 | |||
169 | /** |
||
170 | * Generate share block |
||
171 | * @param array|null $networks Array of network names |
||
172 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
173 | * Can be empty, in this way all possible network types will be showed. |
||
174 | * @return string |
||
175 | */ |
||
176 | 6 | public static function share(array $networks = null): string |
|
184 | |||
185 | /** |
||
186 | * Generate rating block |
||
187 | * @param float $currentRating |
||
188 | * @param float $maxRating |
||
189 | * @return string |
||
190 | */ |
||
191 | 12 | public static function rating(float $currentRating, float $maxRating): string |
|
202 | |||
203 | /** |
||
204 | * Generate button |
||
205 | * @param string $text |
||
206 | * @param string $url |
||
207 | * @param string $phone Phone number in RFC-3966 format |
||
208 | * @param string|null $buttonColor Can be Text or HEX |
||
209 | * @param string|null $textColor Can be Text or HEX |
||
210 | * @param bool $isBoldText |
||
211 | * @param bool $isDisabled |
||
212 | * @return string |
||
213 | */ |
||
214 | 12 | public static function button(string $text, string $url = '', string $phone = '', |
|
235 | |||
236 | /** |
||
237 | * Generate comment block |
||
238 | * @param string $url URL to comments page |
||
239 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
240 | * can has unlimited includes, example: |
||
241 | * [ |
||
242 | * [ |
||
243 | * 'author' => 'First Author Name', |
||
244 | * 'avatar' => 'http://example.com/user1.jpg', |
||
245 | * 'title' => 'Comment Title', |
||
246 | * 'subtitle' => '2017-12-10', |
||
247 | * 'content' => 'Somme comment text', |
||
248 | * 'comments' => [ |
||
249 | * [ |
||
250 | * 'author' => 'Third Author Name', |
||
251 | * 'avatar' => 'http://example.com/user3.jpg', |
||
252 | * 'title' => 'Comment Title', |
||
253 | * 'subtitle' => '2017-12-12', |
||
254 | * 'content' => 'Some answer text' |
||
255 | * ], |
||
256 | * [ |
||
257 | * 'author' => 'Another Author Name', |
||
258 | * 'avatar' => 'http://example.com/user4.jpg', |
||
259 | * 'title' => 'Comment Title', |
||
260 | * 'subtitle' => '2017-12-13', |
||
261 | * 'content' => 'Another answer text' |
||
262 | * ], |
||
263 | * ] |
||
264 | * ], |
||
265 | * [ |
||
266 | * 'author' => 'Second Author Name', |
||
267 | * 'avatar' => 'http://example.com/user2.jpg', |
||
268 | * 'title' => 'Comment Title', |
||
269 | * 'subtitle' => '2017-12-11', |
||
270 | * 'content' => 'Some comment text' |
||
271 | * ], |
||
272 | * ] |
||
273 | * @return string |
||
274 | */ |
||
275 | 3 | public static function comment(string $url, array $commentsArray): string |
|
281 | |||
282 | /** |
||
283 | * Generate accordion |
||
284 | * @param array $accordionArray array accordion elements |
||
285 | * [ |
||
286 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
287 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
288 | * ] |
||
289 | * @return string |
||
290 | */ |
||
291 | 3 | public static function accordion(array $accordionArray): string |
|
304 | |||
305 | /** |
||
306 | * Generate Ad block position element |
||
307 | * @param string $turboAdId value of $turboAdId used in Channel() class |
||
308 | * @return string |
||
309 | * |
||
310 | * @see Channel::$adTurboAdId |
||
311 | */ |
||
312 | 3 | public static function adBlockPosition(string $turboAdId): string |
|
316 | |||
317 | /** |
||
318 | * Generate block with additional content |
||
319 | * @param array $itemsArray Array of items with data |
||
320 | * [ |
||
321 | * [ |
||
322 | * 'href' => 'http://example.com/page1.html', |
||
323 | * 'title' => 'Item title 1', |
||
324 | * 'description' => 'Item description', |
||
325 | * 'thumb' => 'http://example/image1.jpg', |
||
326 | * 'thumb_position' => Content::ADDITIONAL_CONTENT_THUMB_POSITION_LEFT, |
||
327 | * 'thumb_ratio' => Content::ADDITIONAL_CONTENT_THUMB_RATIO_1_1 |
||
328 | * ], |
||
329 | * [ |
||
330 | * 'href' => 'http://example.com/page2.html', |
||
331 | * 'title' => 'Item title 2' |
||
332 | * ], |
||
333 | * ] |
||
334 | * @param string|null $title |
||
335 | * @param string|null $orientation |
||
336 | * @return string |
||
337 | * @throws \Exception |
||
338 | */ |
||
339 | 12 | public static function additionalContent(array $itemsArray, string $title = null, string $orientation = null): string |
|
350 | |||
351 | /** |
||
352 | * Generate search input |
||
353 | * @param string $searchUrl Search engine URL in format https://example.com/search/{text} |
||
354 | * @param string $placeholder |
||
355 | * @return string |
||
356 | */ |
||
357 | 6 | public static function searchInput(string $searchUrl, string $placeholder = ''): string |
|
363 | |||
364 | /** |
||
365 | * Generate content block for media slider |
||
366 | * @param array $itemsArray Array of items with data |
||
367 | * [ |
||
368 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
369 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
370 | * ['url' => 'http://example.com/image3.jpg'], |
||
371 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
372 | * ] |
||
373 | * @return string |
||
374 | */ |
||
375 | 6 | private static function generateSliderItemsBlock(array $itemsArray): string |
|
397 | |||
398 | 3 | private static function generateCommentBlock(array $commentsArray): string |
|
428 | |||
429 | /** |
||
430 | * Generate header menu |
||
431 | * @param array $menuArray array of arrays with pairs of url and title |
||
432 | * [ |
||
433 | * ['url' => 'http://example.com/page1.html', 'title' => 'Page title 1'], |
||
434 | * ['url' => 'http://example.com/page2.html', 'title' => 'Page title 2'], |
||
435 | * ] |
||
436 | * @return string |
||
437 | */ |
||
438 | 3 | private static function generateMenu(array $menuArray): string |
|
448 | |||
449 | /** |
||
450 | * Generate additional content items list |
||
451 | * @param array $itemsArray array of arrays with data of additional content items |
||
452 | * [ |
||
453 | * [ |
||
454 | * 'href' => 'http://example.com/page1.html', |
||
455 | * 'title' => 'Item title 1', |
||
456 | * 'description' => 'Item description', |
||
457 | * 'thumb' => 'http://example/image1.jpg', |
||
458 | * 'thumb_position' => Content::ADDITIONAL_CONTENT_THUMB_POSITION_LEFT, |
||
459 | * 'thumb_ratio' => Content::ADDITIONAL_CONTENT_THUMB_RATIO_1_1 |
||
460 | * ], |
||
461 | * [ |
||
462 | * 'href' => 'http://example.com/page2.html', |
||
463 | * 'title' => 'Item title 2' |
||
464 | * ], |
||
465 | * ] |
||
466 | * @return string |
||
467 | * @throws \Exception |
||
468 | */ |
||
469 | 12 | private static function generateAdditionalContentItemsList(array $itemsArray): string |
|
493 | } |