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 video element for external video |
||
| 81 | * @param string $videoUrl |
||
| 82 | * @param array $options Options Array with next variables: width, height, frameborder, allowfullscreen, |
||
| 83 | * referrerpolicy, sandbox, hd. Options example: |
||
| 84 | * [ |
||
| 85 | * 'width' => 640, |
||
| 86 | * 'height' => 480, |
||
| 87 | * 'frameborder' => 1, |
||
| 88 | * 'allowfullscreen' => 'true', |
||
| 89 | * 'referrerpolicy' => 'unsafe-url', |
||
| 90 | * 'sandbox' => 'allow-forms allow-modals', |
||
| 91 | * 'hd' => 3 |
||
| 92 | * ] |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 12 | public static function externalVideo($videoUrl, array $options = []) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Generate images gallery |
||
| 110 | * @param array $imagesArray Array of images urls |
||
| 111 | * ['http://example.com/image1.jpg', 'http://example.com/image2.jpg'] |
||
| 112 | * @param string|null $header |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 12 | public static function gallery(array $imagesArray, $header = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Generate share block |
||
| 128 | * @param array|null $networks Array of network names |
||
| 129 | * [Content::SHARE_TYPE_GOOGLE, Content::SHARE_TYPE_TWITTER] |
||
| 130 | * Can be empty, in this way all possible network types will be showed. |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 12 | public static function share(array $networks = null) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Generate rating block |
||
| 144 | * @param float $currentRating |
||
| 145 | * @param float $maxRating |
||
| 146 | * @return string |
||
| 147 | * @throws \UnexpectedValueException |
||
| 148 | */ |
||
| 149 | 24 | public static function rating($currentRating, $maxRating) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Generate button |
||
| 163 | * @param string $text |
||
| 164 | * @param string $url |
||
| 165 | * @param string $phone Phone number in RFC-3966 format |
||
| 166 | * @param string|null $buttonColor Can be Text or HEX |
||
| 167 | * @param string|null $textColor Can be Text or HEX |
||
| 168 | * @param bool $isBoldText |
||
| 169 | * @param bool $isDisabled |
||
| 170 | * @return string |
||
| 171 | * @throws \UnexpectedValueException |
||
| 172 | */ |
||
| 173 | 24 | public static function button($text, $url = '', $phone = '', |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Generate comment block |
||
| 197 | * @param string $url URL to comments page |
||
| 198 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
| 199 | * can has unlimited includes, example: |
||
| 200 | * [ |
||
| 201 | * [ |
||
| 202 | * 'author' => 'First Author Name', |
||
| 203 | * 'avatar' => 'http://example.com/user1.jpg', |
||
| 204 | * 'title' => 'Comment Title', |
||
| 205 | * 'subtitle' => '2017-12-10', |
||
| 206 | * 'content' => 'Somme comment text', |
||
| 207 | * 'comments' => [ |
||
| 208 | * [ |
||
| 209 | * 'author' => 'Third Author Name', |
||
| 210 | * 'avatar' => 'http://example.com/user3.jpg', |
||
| 211 | * 'title' => 'Comment Title', |
||
| 212 | * 'subtitle' => '2017-12-12', |
||
| 213 | * 'content' => 'Some answer text' |
||
| 214 | * ], |
||
| 215 | * [ |
||
| 216 | * 'author' => 'Another Author Name', |
||
| 217 | * 'avatar' => 'http://example.com/user4.jpg', |
||
| 218 | * 'title' => 'Comment Title', |
||
| 219 | * 'subtitle' => '2017-12-13', |
||
| 220 | * 'content' => 'Another answer text' |
||
| 221 | * ], |
||
| 222 | * ] |
||
| 223 | * ], |
||
| 224 | * [ |
||
| 225 | * 'author' => 'Second Author Name', |
||
| 226 | * 'avatar' => 'http://example.com/user2.jpg', |
||
| 227 | * 'title' => 'Comment Title', |
||
| 228 | * 'subtitle' => '2017-12-11', |
||
| 229 | * 'content' => 'Some comment text' |
||
| 230 | * ], |
||
| 231 | * ] |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 6 | public static function comment($url, array $commentsArray) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Generate accordion |
||
| 243 | * @param array $accordionArray array accordion elements |
||
| 244 | * [ |
||
| 245 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
| 246 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
| 247 | * ] |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | 6 | public static function accordion(array $accordionArray) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Generate media slider |
||
| 266 | * @param array $itemsArray Array of items with data |
||
| 267 | * [ |
||
| 268 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
| 269 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
| 270 | * ['url' => 'http://example.com/image3.jpg'], |
||
| 271 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
| 272 | * ] |
||
| 273 | * @param string|null $header |
||
| 274 | * @param string $dataView |
||
| 275 | * @param string $dataItemView |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 12 | public static function slider(array $itemsArray, $header = null, |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Generate Ad block position element |
||
| 292 | * @param string $turboAdId value of $turboAdId used in Channel() class |
||
| 293 | * @return string |
||
| 294 | * |
||
| 295 | * @see Channel::$adTurboAdId |
||
| 296 | */ |
||
| 297 | 6 | public static function adBlockPosition($turboAdId) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Generate content block for media slider |
||
| 304 | * @param array $itemsArray Array of items with data |
||
| 305 | * [ |
||
| 306 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
| 307 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
| 308 | * ['url' => 'http://example.com/image3.jpg'], |
||
| 309 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
| 310 | * ] |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | 12 | private static function generateSliderItemsBlock(array $itemsArray) |
|
| 335 | |||
| 336 | 6 | private static function generateCommentBlock(array $commentsArray) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Generate header menu |
||
| 369 | * @param array $menuArray array of arrays with pairs of url and title |
||
| 370 | * [ |
||
| 371 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
| 372 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
| 373 | * ] |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | 6 | private static function generateMenu(array $menuArray) |
|
| 386 | } |