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 |
||
5 | class Content |
||
6 | { |
||
7 | const SHARE_TYPE_FACEBOOK = 'facebook'; |
||
8 | const SHARE_TYPE_GOOGLE = 'google'; |
||
9 | const SHARE_TYPE_ODNOKLASSNIKI = 'odnoklassniki'; |
||
10 | const SHARE_TYPE_TELEGRAM = 'telegram'; |
||
11 | const SHARE_TYPE_TWITTER = 'twitter'; |
||
12 | const SHARE_TYPE_VKONTAKTE = 'vkontakte'; |
||
13 | |||
14 | const SLIDER_DATA_VIEW_SQUARE = 'square'; |
||
15 | const SLIDER_DATA_VIEW_PORTRAIT = 'portrait'; |
||
16 | const SLIDER_DATA_VIEW_LANDSCAPE = 'landscape'; |
||
17 | |||
18 | const SLIDER_DATA_ITEM_VIEW_COVER = 'cover'; |
||
19 | const SLIDER_DATA_ITEM_VIEW_CONTAIN = 'contain'; |
||
20 | |||
21 | const OWN_VIDEO_TYPE_MP4 = 'video/mp4'; |
||
22 | |||
23 | const ADDITIONAL_CONTENT_ORIENTATION_VERTICAL = 'vertical'; |
||
24 | const ADDITIONAL_CONTENT_ORIENTATION_HORIZONTAL = 'horizontal'; |
||
25 | |||
26 | const ADDITIONAL_CONTENT_THUMB_POSITION_LEFT = 'left'; |
||
27 | const ADDITIONAL_CONTENT_THUMB_POSITION_RIGHT = 'right'; |
||
28 | const ADDITIONAL_CONTENT_THUMB_POSITION_TOP = 'top'; |
||
29 | |||
30 | const ADDITIONAL_CONTENT_THUMB_RATIO_1_1 = '1x1'; |
||
31 | const ADDITIONAL_CONTENT_THUMB_RATIO_2_3 = '2x3'; |
||
32 | const ADDITIONAL_CONTENT_THUMB_RATIO_3_2 = '3x2'; |
||
33 | const ADDITIONAL_CONTENT_THUMB_RATIO_3_4 = '3x4'; |
||
34 | const ADDITIONAL_CONTENT_THUMB_RATIO_4_3 = '4x3'; |
||
35 | const ADDITIONAL_CONTENT_THUMB_RATIO_16_9 = '16x9'; |
||
36 | const ADDITIONAL_CONTENT_THUMB_RATIO_16_10 = '16x10'; |
||
37 | |||
38 | /** |
||
39 | * Generate header element |
||
40 | * @param string $h1 |
||
41 | * @param string|null $h2 |
||
42 | * @param string|null $imgUrl |
||
43 | * @param string|null $imgCaption |
||
44 | * @param array|null $menuArray array of arrays with pairs of url and content |
||
45 | * [ |
||
46 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
47 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
48 | * ] |
||
49 | * @return string |
||
50 | */ |
||
51 | 21 | public static function header($h1, $h2 = null, $imgUrl = null, |
|
61 | |||
62 | /** |
||
63 | * Generate image element |
||
64 | * @param string $imgUrl |
||
65 | * @param string|null $imgCaption |
||
66 | * @return string |
||
67 | */ |
||
68 | 21 | public static function img($imgUrl, $imgCaption = null) |
|
76 | |||
77 | /** |
||
78 | * Generate video element |
||
79 | * @param string $videoUrl |
||
80 | * @param string|null $videoCaption |
||
81 | * @param string $imgUrl |
||
82 | * @param string $type |
||
83 | * @return string |
||
84 | */ |
||
85 | 14 | public static function ownVideo($videoUrl, $videoCaption = null, $type = self::OWN_VIDEO_TYPE_MP4, $imgUrl = null) |
|
93 | |||
94 | /** |
||
95 | * Generate video element for external video |
||
96 | * @param string $videoUrl |
||
97 | * @param array $options Options Array with next variables: width, height, frameborder, allowfullscreen, |
||
98 | * referrerpolicy, sandbox, hd. Options example: |
||
99 | * [ |
||
100 | * 'width' => 640, |
||
101 | * 'height' => 480, |
||
102 | * 'frameborder' => 1, |
||
103 | * 'allowfullscreen' => 'true', |
||
104 | * 'referrerpolicy' => 'unsafe-url', |
||
105 | * 'sandbox' => 'allow-forms allow-modals', |
||
106 | * 'hd' => 3 |
||
107 | * ] |
||
108 | * @return string |
||
109 | */ |
||
110 | 14 | public static function externalVideo($videoUrl, array $options = []) |
|
122 | |||
123 | /** |
||
124 | * Generate images gallery |
||
125 | * @param array $imagesArray Array of images urls |
||
126 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
127 | * @param string|null $header |
||
128 | * @return string |
||
129 | */ |
||
130 | 14 | public static function gallery(array $imagesArray, $header = null) |
|
140 | |||
141 | /** |
||
142 | * Generate share block |
||
143 | * @param array|null $networks Array of network names |
||
144 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
145 | * Can be empty, in this way all possible network types will be showed. |
||
146 | * @return string |
||
147 | */ |
||
148 | 14 | public static function share(array $networks = null) |
|
156 | |||
157 | /** |
||
158 | * Generate rating block |
||
159 | * @param float $currentRating |
||
160 | * @param float $maxRating |
||
161 | * @return string |
||
162 | * @throws \UnexpectedValueException |
||
163 | */ |
||
164 | 28 | public static function rating($currentRating, $maxRating) |
|
175 | |||
176 | /** |
||
177 | * Generate button |
||
178 | * @param string $text |
||
179 | * @param string $url |
||
180 | * @param string $phone Phone number in RFC-3966 format |
||
181 | * @param string|null $buttonColor Can be Text or HEX |
||
182 | * @param string|null $textColor Can be Text or HEX |
||
183 | * @param bool $isBoldText |
||
184 | * @param bool $isDisabled |
||
185 | * @return string |
||
186 | * @throws \UnexpectedValueException |
||
187 | */ |
||
188 | 28 | public static function button($text, $url = '', $phone = '', |
|
209 | |||
210 | /** |
||
211 | * Generate comment block |
||
212 | * @param string $url URL to comments page |
||
213 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
214 | * can has unlimited includes, example: |
||
215 | * [ |
||
216 | * [ |
||
217 | * 'author' => 'First Author Name', |
||
218 | * 'avatar' => 'http://example.com/user1.jpg', |
||
219 | * 'title' => 'Comment Title', |
||
220 | * 'subtitle' => '2017-12-10', |
||
221 | * 'content' => 'Somme comment text', |
||
222 | * 'comments' => [ |
||
223 | * [ |
||
224 | * 'author' => 'Third Author Name', |
||
225 | * 'avatar' => 'http://example.com/user3.jpg', |
||
226 | * 'title' => 'Comment Title', |
||
227 | * 'subtitle' => '2017-12-12', |
||
228 | * 'content' => 'Some answer text' |
||
229 | * ], |
||
230 | * [ |
||
231 | * 'author' => 'Another Author Name', |
||
232 | * 'avatar' => 'http://example.com/user4.jpg', |
||
233 | * 'title' => 'Comment Title', |
||
234 | * 'subtitle' => '2017-12-13', |
||
235 | * 'content' => 'Another answer text' |
||
236 | * ], |
||
237 | * ] |
||
238 | * ], |
||
239 | * [ |
||
240 | * 'author' => 'Second Author Name', |
||
241 | * 'avatar' => 'http://example.com/user2.jpg', |
||
242 | * 'title' => 'Comment Title', |
||
243 | * 'subtitle' => '2017-12-11', |
||
244 | * 'content' => 'Some comment text' |
||
245 | * ], |
||
246 | * ] |
||
247 | * @return string |
||
248 | */ |
||
249 | 7 | public static function comment($url, array $commentsArray) |
|
255 | |||
256 | /** |
||
257 | * Generate accordion |
||
258 | * @param array $accordionArray array accordion elements |
||
259 | * [ |
||
260 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
261 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
262 | * ] |
||
263 | * @return string |
||
264 | */ |
||
265 | 7 | public static function accordion(array $accordionArray) |
|
278 | |||
279 | /** |
||
280 | * Generate block with additional content |
||
281 | * @param array $itemsArray Array of items with data |
||
282 | * [ |
||
283 | * [ |
||
284 | * 'href' => 'http://example.com/page1.html', |
||
285 | * 'title' => 'Item title 1', |
||
286 | * 'description' => 'Item description', |
||
287 | * 'thumb' => 'http://example/image1.jpg', |
||
288 | * 'thumb_position' => Content::ADDITIONAL_CONTENT_THUMB_POSITION_LEFT, |
||
289 | * 'thumb_ratio' => Content::ADDITIONAL_CONTENT_THUMB_RATIO_1_1 |
||
290 | * ], |
||
291 | * [ |
||
292 | * 'href' => 'http://example.com/page2.html', |
||
293 | * 'title' => 'Item title 2' |
||
294 | * ], |
||
295 | * ] |
||
296 | * @param string|null $title |
||
297 | * @param string|null $orientation |
||
298 | * @return string |
||
299 | * @throws \Exception |
||
300 | */ |
||
301 | 28 | public static function additionalContent(array $itemsArray, $title = null, $orientation = null) |
|
312 | |||
313 | /** |
||
314 | * Generate media slider |
||
315 | * @param array $itemsArray Array of items with data |
||
316 | * [ |
||
317 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
318 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
319 | * ['url' => 'http://example.com/image3.jpg'], |
||
320 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
321 | * ] |
||
322 | * @param string|null $header |
||
323 | * @param string $dataView |
||
324 | * @param string $dataItemView |
||
325 | * @return string |
||
326 | */ |
||
327 | 14 | public static function slider(array $itemsArray, $header = null, |
|
338 | |||
339 | /** |
||
340 | * Generate Ad block position element |
||
341 | * @param string $turboAdId value of $turboAdId used in Channel() class |
||
342 | * @return string |
||
343 | * |
||
344 | * @see Channel::$adTurboAdId |
||
345 | */ |
||
346 | 7 | public static function adBlockPosition($turboAdId) |
|
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 | 14 | public static function searchInput($searchUrl, $placeholder = '') |
|
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 | 14 | private static function generateSliderItemsBlock(array $itemsArray) |
|
397 | |||
398 | 7 | private static function generateCommentBlock(array $commentsArray) |
|
428 | |||
429 | /** |
||
430 | * Generate header menu |
||
431 | * @param array $menuArray array of arrays with pairs of url and title |
||
432 | * [ |
||
433 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
434 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
435 | * ] |
||
436 | * @return string |
||
437 | */ |
||
438 | 7 | private static function generateMenu(array $menuArray) |
|
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 | 28 | private static function generateAdditionalContentItemsList(array $itemsArray) |
|
493 | } |