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 | /** |
||
26 | * Generate header element |
||
27 | * @param string $h1 |
||
28 | * @param string|null $h2 |
||
29 | * @param string|null $imgUrl |
||
30 | * @param string|null $imgCaption |
||
31 | * @param array|null $menuArray array of arrays with pairs of url and content |
||
32 | * [ |
||
33 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
34 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
35 | * ] |
||
36 | * @return string |
||
37 | */ |
||
38 | 9 | public static function header(string $h1, string $h2 = null, string $imgUrl = null, |
|
48 | |||
49 | /** |
||
50 | * Generate image element |
||
51 | * @param string $imgUrl |
||
52 | * @param string|null $imgCaption |
||
53 | * @return string |
||
54 | */ |
||
55 | 9 | public static function img(string $imgUrl, string $imgCaption = null): string |
|
63 | |||
64 | /** |
||
65 | * Generate video element |
||
66 | * @param string $videoUrl |
||
67 | * @param string|null $videoCaption |
||
68 | * @param string $imgUrl |
||
69 | * @param string $type |
||
70 | * @return string |
||
71 | */ |
||
72 | 6 | public static function ownVideo(string $videoUrl, string $videoCaption = null, string $type = self::OWN_VIDEO_TYPE_MP4, string $imgUrl = null): string |
|
80 | |||
81 | /** |
||
82 | * Generate images gallery |
||
83 | * @param array $imagesArray Array of images urls |
||
84 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
85 | * @param string|null $header |
||
86 | * @return string |
||
87 | */ |
||
88 | 6 | public static function gallery(array $imagesArray, string $header = null): string |
|
98 | |||
99 | /** |
||
100 | * Generate media slider |
||
101 | * @param array $itemsArray Array of items with data |
||
102 | * [ |
||
103 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
104 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
105 | * ['url' => 'http://example.com/image3.jpg'], |
||
106 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
107 | * ] |
||
108 | * @param string|null $header |
||
109 | * @param string $dataView |
||
110 | * @param string $dataItemView |
||
111 | * @return string |
||
112 | */ |
||
113 | 6 | public static function slider(array $itemsArray, string $header = null, |
|
124 | |||
125 | /** |
||
126 | * Generate share block |
||
127 | * @param array|null $networks Array of network names |
||
128 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
129 | * Can be empty, in this way all possible network types will be showed. |
||
130 | * @return string |
||
131 | */ |
||
132 | 6 | public static function share(array $networks = null): string |
|
140 | |||
141 | /** |
||
142 | * Generate rating block |
||
143 | * @param float $currentRating |
||
144 | * @param float $maxRating |
||
145 | * @return string |
||
146 | */ |
||
147 | 12 | public static function rating(float $currentRating, float $maxRating): string |
|
158 | |||
159 | /** |
||
160 | * Generate button |
||
161 | * @param string $text |
||
162 | * @param string $url |
||
163 | * @param string $phone Phone number in RFC-3966 format |
||
164 | * @param string|null $buttonColor Can be Text or HEX |
||
165 | * @param string|null $textColor Can be Text or HEX |
||
166 | * @param bool $isBoldText |
||
167 | * @param bool $isDisabled |
||
168 | * @return string |
||
169 | */ |
||
170 | 12 | public static function button(string $text, string $url = '', string $phone = '', |
|
191 | |||
192 | /** |
||
193 | * Generate comment block |
||
194 | * @param string $url URL to comments page |
||
195 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
196 | * can has unlimited includes, example: |
||
197 | * [ |
||
198 | * [ |
||
199 | * 'author' => 'First Author Name', |
||
200 | * 'avatar' => 'http://example.com/user1.jpg', |
||
201 | * 'title' => 'Comment Title', |
||
202 | * 'subtitle' => '2017-12-10', |
||
203 | * 'content' => 'Somme comment text', |
||
204 | * 'comments' => [ |
||
205 | * [ |
||
206 | * 'author' => 'Third Author Name', |
||
207 | * 'avatar' => 'http://example.com/user3.jpg', |
||
208 | * 'title' => 'Comment Title', |
||
209 | * 'subtitle' => '2017-12-12', |
||
210 | * 'content' => 'Some answer text' |
||
211 | * ], |
||
212 | * [ |
||
213 | * 'author' => 'Another Author Name', |
||
214 | * 'avatar' => 'http://example.com/user4.jpg', |
||
215 | * 'title' => 'Comment Title', |
||
216 | * 'subtitle' => '2017-12-13', |
||
217 | * 'content' => 'Another answer text' |
||
218 | * ], |
||
219 | * ] |
||
220 | * ], |
||
221 | * [ |
||
222 | * 'author' => 'Second Author Name', |
||
223 | * 'avatar' => 'http://example.com/user2.jpg', |
||
224 | * 'title' => 'Comment Title', |
||
225 | * 'subtitle' => '2017-12-11', |
||
226 | * 'content' => 'Some comment text' |
||
227 | * ], |
||
228 | * ] |
||
229 | * @return string |
||
230 | */ |
||
231 | 3 | public static function comment(string $url, array $commentsArray): string |
|
237 | |||
238 | /** |
||
239 | * Generate accordion |
||
240 | * @param array $accordionArray array accordion elements |
||
241 | * [ |
||
242 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
243 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
244 | * ] |
||
245 | * @return string |
||
246 | */ |
||
247 | 3 | public static function accordion(array $accordionArray): string |
|
260 | |||
261 | /** |
||
262 | * Generate Ad block position element |
||
263 | * @param string $turboAdId value of $turboAdId used in Channel() class |
||
264 | * @return string |
||
265 | * |
||
266 | * @see Channel::$adTurboAdId |
||
267 | */ |
||
268 | 3 | public static function adBlockPosition(string $turboAdId): string |
|
272 | |||
273 | 6 | private static function generateSliderItemsBlock(array $itemsArray) |
|
274 | { |
||
275 | 6 | $sliderString = ''; |
|
276 | |||
277 | 6 | foreach ($itemsArray as $item) { |
|
278 | 6 | $sliderString .= '<figure>'; |
|
279 | |||
280 | 6 | if (isset($item['title'])) { |
|
281 | 6 | $sliderString .= '<figcaption>' . $item['title'] . '</figcaption>'; |
|
282 | } |
||
283 | |||
284 | 6 | if (isset($item['url'])) { |
|
285 | 6 | $sliderString .= '<img src="' . $item['url'] . '" />'; |
|
286 | 6 | } elseif (isset($item['href'])) { |
|
287 | 6 | $sliderString .= '<a href="' . $item['href'] . '">' . $item['text'] . '</a>'; |
|
288 | } |
||
289 | |||
290 | 6 | $sliderString .= '</figure>'; |
|
291 | } |
||
292 | |||
293 | 6 | return $sliderString; |
|
294 | } |
||
295 | |||
296 | 3 | private static function generateCommentBlock(array $commentsArray) |
|
326 | |||
327 | /** |
||
328 | * Generate header menu |
||
329 | * @param array $menuArray array of arrays with pairs of url and title |
||
330 | * [ |
||
331 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
332 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
333 | * ] |
||
334 | * @return string |
||
335 | */ |
||
336 | 3 | private static function generateMenu(array $menuArray) |
|
346 | } |