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 OWN_VIDEO_TYPE_MP4 = 'video/mp4'; |
||
15 | |||
16 | /** |
||
17 | * Generate header element |
||
18 | * @param string $h1 |
||
19 | * @param string|null $h2 |
||
20 | * @param string|null $imgUrl |
||
21 | * @param string|null $imgCaption |
||
22 | * @param array|null $menuArray array of arrays with pairs of url and content |
||
23 | * [ |
||
24 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
25 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
26 | * ] |
||
27 | * @return string |
||
28 | */ |
||
29 | 18 | public static function header($h1, $h2 = null, $imgUrl = null, |
|
39 | |||
40 | /** |
||
41 | * Generate image element |
||
42 | * @param string $imgUrl |
||
43 | * @param string|null $imgCaption |
||
44 | * @return string |
||
45 | */ |
||
46 | 18 | public static function img($imgUrl, $imgCaption = null) |
|
54 | |||
55 | /** |
||
56 | * Generate video element |
||
57 | * @param string $videoUrl |
||
58 | * @param string|null $videoCaption |
||
59 | * @param string $imgUrl |
||
60 | * @param string $type |
||
61 | * @return string |
||
62 | */ |
||
63 | 12 | public static function ownVideo($videoUrl, $videoCaption = null, $type = self::OWN_VIDEO_TYPE_MP4, $imgUrl = null) |
|
71 | |||
72 | /** |
||
73 | * Generate images gallery |
||
74 | * @param array $imagesArray Array of images urls |
||
75 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
76 | * @param string|null $header |
||
77 | * @return string |
||
78 | */ |
||
79 | 12 | public static function gallery(array $imagesArray, $header = null) |
|
89 | |||
90 | /** |
||
91 | * Generate share block |
||
92 | * @param array|null $networks Array of network names |
||
93 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
94 | * Can be empty, in this way all possible network types will be showed. |
||
95 | * @return string |
||
96 | */ |
||
97 | 12 | public static function share(array $networks = null) |
|
105 | |||
106 | /** |
||
107 | * Generate rating block |
||
108 | * @param float $currentRating |
||
109 | * @param float $maxRating |
||
110 | * @return string |
||
111 | */ |
||
112 | 24 | public static function rating($currentRating, $maxRating) |
|
123 | |||
124 | /** |
||
125 | * Generate button |
||
126 | * @param string $text |
||
127 | * @param string $url |
||
128 | * @param string $phone Phone number in RFC-3966 format |
||
129 | * @param string|null $buttonColor Can be Text or HEX |
||
130 | * @param string|null $textColor Can be Text or HEX |
||
131 | * @param bool $isBoldText |
||
132 | * @param bool $isDisabled |
||
133 | * @return string |
||
134 | */ |
||
135 | 24 | public static function button($text, $url = '', $phone = '', |
|
156 | |||
157 | /** |
||
158 | * Generate comment block |
||
159 | * @param string $url URL to comments page |
||
160 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
161 | * can has unlimited includes, example: |
||
162 | * [ |
||
163 | * [ |
||
164 | * 'author' => 'First Author Name', |
||
165 | * 'avatar' => 'http://example.com/user1.jpg', |
||
166 | * 'title' => 'Comment Title', |
||
167 | * 'subtitle' => '2017-12-10', |
||
168 | * 'content' => 'Somme comment text', |
||
169 | * 'comments' => [ |
||
170 | * [ |
||
171 | * 'author' => 'Third Author Name', |
||
172 | * 'avatar' => 'http://example.com/user3.jpg', |
||
173 | * 'title' => 'Comment Title', |
||
174 | * 'subtitle' => '2017-12-12', |
||
175 | * 'content' => 'Some answer text' |
||
176 | * ], |
||
177 | * [ |
||
178 | * 'author' => 'Another Author Name', |
||
179 | * 'avatar' => 'http://example.com/user4.jpg', |
||
180 | * 'title' => 'Comment Title', |
||
181 | * 'subtitle' => '2017-12-13', |
||
182 | * 'content' => 'Another answer text' |
||
183 | * ], |
||
184 | * ] |
||
185 | * ], |
||
186 | * [ |
||
187 | * 'author' => 'Second Author Name', |
||
188 | * 'avatar' => 'http://example.com/user2.jpg', |
||
189 | * 'title' => 'Comment Title', |
||
190 | * 'subtitle' => '2017-12-11', |
||
191 | * 'content' => 'Some comment text' |
||
192 | * ], |
||
193 | * ] |
||
194 | * @return string |
||
195 | */ |
||
196 | 6 | public static function comment($url, array $commentsArray) |
|
202 | |||
203 | /** |
||
204 | * Generate accordion |
||
205 | * @param array $accordionArray array accordion elements |
||
206 | * [ |
||
207 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
208 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
209 | * ] |
||
210 | * @return string |
||
211 | */ |
||
212 | 6 | public static function accordion(array $accordionArray) |
|
225 | |||
226 | 6 | private static function generateCommentBlock(array $commentsArray) |
|
256 | |||
257 | /** |
||
258 | * Generate header menu |
||
259 | * @param array $menuArray array of arrays with pairs of url and title |
||
260 | * [ |
||
261 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
262 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
263 | * ] |
||
264 | * @return string |
||
265 | */ |
||
266 | 6 | private static function generateMenu(array $menuArray) |
|
276 | } |