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 | 9 | 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 | 9 | 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 | 6 | 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 | 6 | public static function gallery(array $imagesArray, $header = null) |
|
| 80 | { |
||
| 81 | 6 | $galleryString = $header ? '<header>' . $header . '</header>' : ''; |
|
| 82 | |||
| 83 | 6 | foreach ($imagesArray as $image) { |
|
| 84 | 6 | $galleryString .= '<img src="' . $image . '" />'; |
|
| 85 | } |
||
| 86 | |||
| 87 | 6 | return '<div data-block="gallery">' . $galleryString . '</div>'; |
|
| 88 | } |
||
| 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 | 6 | 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 | * @throws \UnexpectedValueException |
||
| 112 | */ |
||
| 113 | 12 | public static function rating($currentRating, $maxRating) |
|
| 114 | { |
||
| 115 | 12 | if (($currentRating > $maxRating) || ($maxRating <= 0) || ($currentRating < 0)) { |
|
| 116 | 9 | throw new \UnexpectedValueException("Current rating can't be bigger than max value. And max value must be bigger than 0."); |
|
| 117 | } |
||
| 118 | |||
| 119 | return '<div itemscope="" itemtype="http://schema.org/Rating"> |
||
| 120 | 3 | <meta itemprop="ratingValue" content="' . $currentRating . '" /> |
|
| 121 | 3 | <meta itemprop="bestRating" content="' . $maxRating . '" /> |
|
| 122 | </div>'; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Generate button |
||
| 127 | * @param string $text |
||
| 128 | * @param string $url |
||
| 129 | * @param string $phone Phone number in RFC-3966 format |
||
| 130 | * @param string|null $buttonColor Can be Text or HEX |
||
| 131 | * @param string|null $textColor Can be Text or HEX |
||
| 132 | * @param bool $isBoldText |
||
| 133 | * @param bool $isDisabled |
||
| 134 | * @return string |
||
| 135 | * @throws \UnexpectedValueException |
||
| 136 | */ |
||
| 137 | 12 | public static function button($text, $url = '', $phone = '', |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Generate comment block |
||
| 161 | * @param string $url URL to comments page |
||
| 162 | * @param array $commentsArray multidimensional or one-dimensional array of comments, |
||
| 163 | * can has unlimited includes, example: |
||
| 164 | * [ |
||
| 165 | * [ |
||
| 166 | * 'author' => 'First Author Name', |
||
| 167 | * 'avatar' => 'http://example.com/user1.jpg', |
||
| 168 | * 'title' => 'Comment Title', |
||
| 169 | * 'subtitle' => '2017-12-10', |
||
| 170 | * 'content' => 'Somme comment text', |
||
| 171 | * 'comments' => [ |
||
| 172 | * [ |
||
| 173 | * 'author' => 'Third Author Name', |
||
| 174 | * 'avatar' => 'http://example.com/user3.jpg', |
||
| 175 | * 'title' => 'Comment Title', |
||
| 176 | * 'subtitle' => '2017-12-12', |
||
| 177 | * 'content' => 'Some answer text' |
||
| 178 | * ], |
||
| 179 | * [ |
||
| 180 | * 'author' => 'Another Author Name', |
||
| 181 | * 'avatar' => 'http://example.com/user4.jpg', |
||
| 182 | * 'title' => 'Comment Title', |
||
| 183 | * 'subtitle' => '2017-12-13', |
||
| 184 | * 'content' => 'Another answer text' |
||
| 185 | * ], |
||
| 186 | * ] |
||
| 187 | * ], |
||
| 188 | * [ |
||
| 189 | * 'author' => 'Second Author Name', |
||
| 190 | * 'avatar' => 'http://example.com/user2.jpg', |
||
| 191 | * 'title' => 'Comment Title', |
||
| 192 | * 'subtitle' => '2017-12-11', |
||
| 193 | * 'content' => 'Some comment text' |
||
| 194 | * ], |
||
| 195 | * ] |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | 3 | public static function comment($url, array $commentsArray) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Generate accordion |
||
| 207 | * @param array $accordionArray array accordion elements |
||
| 208 | * [ |
||
| 209 | * ['title' => 'Page title 1', 'text' => 'Text 1'], |
||
| 210 | * ['title' => 'Page title 2', 'text' => 'Text 2', 'expanded' => true], |
||
| 211 | * ] |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 3 | public static function accordion(array $accordionArray) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Generate media slider |
||
| 230 | * @param array $itemsArray Array of items with data |
||
| 231 | * [ |
||
| 232 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
| 233 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
| 234 | * ['url' => 'http://example.com/image3.jpg'], |
||
| 235 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
| 236 | * ] |
||
| 237 | * @param string|null $header |
||
| 238 | * @param string $dataView |
||
| 239 | * @param string $dataItemView |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public static function slider(array $itemsArray, $header = null, |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Generate Ad block position element |
||
| 256 | * @param string $turboAdId value of $turboAdId used in Channel() class |
||
| 257 | * @return string |
||
| 258 | * |
||
| 259 | * @see Channel::$adTurboAdId |
||
| 260 | */ |
||
| 261 | 3 | public static function adBlockPosition($turboAdId) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Generate content block for media slider |
||
| 268 | * @param array $itemsArray Array of items with data |
||
| 269 | * [ |
||
| 270 | * ['url' => 'http://example.com/image1.jpg', 'title' => 'Image title 1', 'link' => ''], |
||
| 271 | * ['url' => 'http://example.com/image2.jpg', 'title' => 'Image title 2', 'link' => ''], |
||
| 272 | * ['url' => 'http://example.com/image3.jpg'], |
||
| 273 | * ['href' => 'http://example.com/page1.html', 'title' => 'Link title 1', 'text' => 'Link text 1'] |
||
| 274 | * ] |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | private static function generateSliderItemsBlock(array $itemsArray) |
||
| 299 | |||
| 300 | 3 | private static function generateCommentBlock(array $commentsArray) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Generate header menu |
||
| 333 | * @param array $menuArray array of arrays with pairs of url and title |
||
| 334 | * [ |
||
| 335 | * ['url' => 'http://example/page1.html', 'title' => 'Page title 1'], |
||
| 336 | * ['url' => 'http://example/page2.html', 'title' => 'Page title 2'], |
||
| 337 | * ] |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 3 | private static function generateMenu(array $menuArray) |
|
| 350 | } |