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 | /** |
||
24 | * Generate header element |
||
25 | * @param string $h1 |
||
26 | * @param string|null $h2 |
||
27 | * @param string|null $imgUrl |
||
28 | * @param string|null $imgCaption |
||
29 | * @param array|null $menuArray array of arrays with pairs of url and content |
||
30 | * [ |
||
31 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
32 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
33 | * ] |
||
34 | * @return string |
||
35 | */ |
||
36 | 18 | public static function header($h1, $h2 = null, $imgUrl = null, |
|
46 | |||
47 | /** |
||
48 | * Generate image element |
||
49 | * @param string $imgUrl |
||
50 | * @param string|null $imgCaption |
||
51 | * @return string |
||
52 | */ |
||
53 | 18 | public static function img($imgUrl, $imgCaption = null) |
|
61 | |||
62 | /** |
||
63 | * Generate video element |
||
64 | * @param string $videoUrl |
||
65 | * @param string|null $videoCaption |
||
66 | * @param string $imgUrl |
||
67 | * @param string $type |
||
68 | * @return string |
||
69 | */ |
||
70 | 12 | public static function ownVideo($videoUrl, $videoCaption = null, $type = self::OWN_VIDEO_TYPE_MP4, $imgUrl = null) |
|
78 | |||
79 | /** |
||
80 | * Generate images gallery |
||
81 | * @param array $imagesArray Array of images urls |
||
82 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
83 | * @param string|null $header |
||
84 | * @return string |
||
85 | */ |
||
86 | 12 | public static function gallery(array $imagesArray, $header = null) |
|
96 | |||
97 | /** |
||
98 | * Generate share block |
||
99 | * @param array|null $networks Array of network names |
||
100 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
101 | * Can be empty, in this way all possible network types will be showed. |
||
102 | * @return string |
||
103 | */ |
||
104 | 12 | public static function share(array $networks = null) |
|
112 | |||
113 | /** |
||
114 | * Generate rating block |
||
115 | * @param float $currentRating |
||
116 | * @param float $maxRating |
||
117 | * @return string |
||
118 | * @throws \UnexpectedValueException |
||
119 | */ |
||
120 | 24 | public static function rating($currentRating, $maxRating) |
|
131 | |||
132 | /** |
||
133 | * Generate button |
||
134 | * @param string $text |
||
135 | * @param string $url |
||
136 | * @param string $phone Phone number in RFC-3966 format |
||
137 | * @param string|null $buttonColor Can be Text or HEX |
||
138 | * @param string|null $textColor Can be Text or HEX |
||
139 | * @param bool $isBoldText |
||
140 | * @param bool $isDisabled |
||
141 | * @return string |
||
142 | * @throws \UnexpectedValueException |
||
143 | */ |
||
144 | 24 | public static function button($text, $url = '', $phone = '', |
|
165 | |||
166 | /** |
||
167 | * Generate comment block |
||
168 | * @param string $url URL to comments page |
||
169 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
170 | * can has unlimited includes, example: |
||
171 | * [ |
||
172 | * [ |
||
173 | * 'author' => 'First Author Name', |
||
174 | * 'avatar' => 'http://example.com/user1.jpg', |
||
175 | * 'title' => 'Comment Title', |
||
176 | * 'subtitle' => '2017-12-10', |
||
177 | * 'content' => 'Somme comment text', |
||
178 | * 'comments' => [ |
||
179 | * [ |
||
180 | * 'author' => 'Third Author Name', |
||
181 | * 'avatar' => 'http://example.com/user3.jpg', |
||
182 | * 'title' => 'Comment Title', |
||
183 | * 'subtitle' => '2017-12-12', |
||
184 | * 'content' => 'Some answer text' |
||
185 | * ], |
||
186 | * [ |
||
187 | * 'author' => 'Another Author Name', |
||
188 | * 'avatar' => 'http://example.com/user4.jpg', |
||
189 | * 'title' => 'Comment Title', |
||
190 | * 'subtitle' => '2017-12-13', |
||
191 | * 'content' => 'Another answer text' |
||
192 | * ], |
||
193 | * ] |
||
194 | * ], |
||
195 | * [ |
||
196 | * 'author' => 'Second Author Name', |
||
197 | * 'avatar' => 'http://example.com/user2.jpg', |
||
198 | * 'title' => 'Comment Title', |
||
199 | * 'subtitle' => '2017-12-11', |
||
200 | * 'content' => 'Some comment text' |
||
201 | * ], |
||
202 | * ] |
||
203 | * @return string |
||
204 | */ |
||
205 | 6 | public static function comment($url, array $commentsArray) |
|
211 | |||
212 | /** |
||
213 | * Generate accordion |
||
214 | * @param array $accordionArray array accordion elements |
||
215 | * [ |
||
216 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
217 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
218 | * ] |
||
219 | * @return string |
||
220 | */ |
||
221 | 6 | public static function accordion(array $accordionArray) |
|
234 | |||
235 | /** |
||
236 | * Generate media slider |
||
237 | * @param array $itemsArray Array of items with data |
||
238 | * [ |
||
239 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
240 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
241 | * ['url' => 'http://example.com/image3.jpg'], |
||
242 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
243 | * ] |
||
244 | * @param string|null $header |
||
245 | * @param string $dataView |
||
246 | * @param string $dataItemView |
||
247 | * @return string |
||
248 | */ |
||
249 | 12 | public static function slider(array $itemsArray, $header = null, |
|
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 | 6 | public static function adBlockPosition($turboAdId) |
|
272 | |||
273 | /** |
||
274 | * Generate content block for media slider |
||
275 | * @param array $itemsArray Array of items with data |
||
276 | * [ |
||
277 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
278 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
279 | * ['url' => 'http://example.com/image3.jpg'], |
||
280 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
281 | * ] |
||
282 | * @return string |
||
283 | */ |
||
284 | 12 | private static function generateSliderItemsBlock(array $itemsArray) |
|
306 | |||
307 | 6 | private static function generateCommentBlock(array $commentsArray) |
|
337 | |||
338 | /** |
||
339 | * Generate header menu |
||
340 | * @param array $menuArray array of arrays with pairs of url and title |
||
341 | * [ |
||
342 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
343 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
344 | * ] |
||
345 | * @return string |
||
346 | */ |
||
347 | 6 | private static function generateMenu(array $menuArray) |
|
357 | } |