Complex classes like Article 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 Article, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Article { |
||
| 16 | /** |
||
| 17 | * Language of the article |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $language; |
||
| 22 | |||
| 23 | /** @param string $language */ |
||
| 24 | public function setLanguage($language) { |
||
| 27 | |||
| 28 | /** @return string */ |
||
| 29 | public function getLanguage() { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * OpenGraph meta data |
||
| 35 | * |
||
| 36 | * @var string[] |
||
| 37 | */ |
||
| 38 | protected $openGraph; |
||
| 39 | |||
| 40 | /** @param string[] $openGraph */ |
||
| 41 | public function setOpenGraph($openGraph) { |
||
| 44 | |||
| 45 | /** @return string[] */ |
||
| 46 | public function getOpenGraph() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Title of the article |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $title; |
||
| 56 | |||
| 57 | /** @param string $title */ |
||
| 58 | public function setTitle($title) { |
||
| 61 | |||
| 62 | /** @return string */ |
||
| 63 | public function getTitle() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Stores the lovely, pure text from the article, stripped of html, formatting, etc... |
||
| 69 | * just raw text with paragraphs separated by newlines. This is probably what you want to use. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $cleanedArticleText; |
||
| 74 | |||
| 75 | /** @param string $cleanedArticleText */ |
||
| 76 | public function setCleanedArticleText($cleanedArticleText) { |
||
| 79 | |||
| 80 | /** @return string */ |
||
| 81 | public function getCleanedArticleText() { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Article with the originals HTML tags (<p>, <a>, ..) |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $htmlArticle; |
||
| 91 | |||
| 92 | /** @param string $htmlArticle */ |
||
| 93 | public function setHtmlArticle($htmlArticle) { |
||
| 96 | |||
| 97 | /** @return string */ |
||
| 98 | public function getHtmlArticle() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Meta description field in HTML source |
||
| 104 | * |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $metaDescription; |
||
| 108 | |||
| 109 | /** @param string $metaDescription */ |
||
| 110 | public function setMetaDescription($metaDescription) { |
||
| 113 | |||
| 114 | /** @return string */ |
||
| 115 | public function getMetaDescription() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Meta keywords field in the HTML source |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $metaKeywords; |
||
| 125 | |||
| 126 | /** @param string $metaKeywords */ |
||
| 127 | public function setMetaKeywords($metaKeywords) { |
||
| 130 | |||
| 131 | /** @return string */ |
||
| 132 | public function getMetaKeywords() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The canonical link of this article if found in the meta data |
||
| 138 | * |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | protected $canonicalLink; |
||
| 142 | |||
| 143 | /** @param string $canonicalLink */ |
||
| 144 | public function setCanonicalLink($canonicalLink) { |
||
| 147 | |||
| 148 | /** @return string */ |
||
| 149 | public function getCanonicalLink() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Domain of the article we're parsing |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | protected $domain; |
||
| 159 | |||
| 160 | /** @param string $domain */ |
||
| 161 | public function setDomain($domain) { |
||
| 164 | |||
| 165 | /** @return string */ |
||
| 166 | public function getDomain() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Top Element we think is a candidate for the main body of the article |
||
| 172 | * |
||
| 173 | * @var Element|null |
||
| 174 | */ |
||
| 175 | protected $topNode; |
||
| 176 | |||
| 177 | /** @param Element|null $topNode */ |
||
| 178 | public function setTopNode(Element $topNode = null) { |
||
| 181 | |||
| 182 | /** @return Element|null */ |
||
| 183 | public function getTopNode() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Top Image object that we think represents this article |
||
| 189 | * |
||
| 190 | * @var Image|null |
||
| 191 | */ |
||
| 192 | protected $topImage; |
||
| 193 | |||
| 194 | /** @param Image|null $topImage */ |
||
| 195 | public function setTopImage(Image $topImage = null) { |
||
| 198 | |||
| 199 | /** @return Image|null */ |
||
| 200 | public function getTopImage() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * All image candidates from article |
||
| 206 | * |
||
| 207 | * @var Image[] |
||
| 208 | */ |
||
| 209 | protected $allImages = []; |
||
| 210 | |||
| 211 | /** @param Image[] $allImages */ |
||
| 212 | public function setAllImages($allImages = []) { |
||
| 215 | |||
| 216 | /** @return Image[] */ |
||
| 217 | public function getAllImages() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Tags that may have been in the article, these are not meta keywords |
||
| 223 | * |
||
| 224 | * @var string[] |
||
| 225 | */ |
||
| 226 | protected $tags = []; |
||
| 227 | |||
| 228 | /** @param string[] $tags */ |
||
| 229 | public function setTags($tags) { |
||
| 232 | |||
| 233 | /** @return string[] */ |
||
| 234 | public function getTags() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * List of links in the article |
||
| 240 | * |
||
| 241 | * @var string[] |
||
| 242 | */ |
||
| 243 | protected $links = []; |
||
| 244 | |||
| 245 | /** @param string[] $links */ |
||
| 246 | public function setLinks($links) { |
||
| 249 | |||
| 250 | /** @return string[] */ |
||
| 251 | public function getLinks() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * List of any videos we found on the page like youtube, vimeo |
||
| 257 | * |
||
| 258 | * @var string[] |
||
| 259 | */ |
||
| 260 | protected $videos = []; |
||
| 261 | |||
| 262 | /** @param string[] $videos */ |
||
| 263 | public function setVideos($videos) { |
||
| 266 | |||
| 267 | /** @return string[] */ |
||
| 268 | public function getVideos() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Final URL that we're going to try and fetch content against, this would be expanded if any |
||
| 274 | * escaped fragments were found in the starting url |
||
| 275 | * |
||
| 276 | * @var string |
||
| 277 | */ |
||
| 278 | protected $finalUrl; |
||
| 279 | |||
| 280 | /** @param string $finalUrl */ |
||
| 281 | public function setFinalUrl($finalUrl) { |
||
| 284 | |||
| 285 | /** @return string */ |
||
| 286 | public function getFinalUrl() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * MD5 hash of the url to use for various identification tasks |
||
| 292 | * |
||
| 293 | * @var string |
||
| 294 | */ |
||
| 295 | protected $linkhash; |
||
| 296 | |||
| 297 | /** @param string $linkhash */ |
||
| 298 | public function setLinkhash($linkhash) { |
||
| 301 | |||
| 302 | /** @return string */ |
||
| 303 | public function getLinkhash() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Raw HTML straight from the network connection |
||
| 309 | * |
||
| 310 | * @var string |
||
| 311 | */ |
||
| 312 | protected $rawHtml; |
||
| 313 | |||
| 314 | /** @param string $rawHtml */ |
||
| 315 | public function setRawHtml($rawHtml) { |
||
| 318 | |||
| 319 | /** @return string */ |
||
| 320 | public function getRawHtml() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * DOM Document object |
||
| 326 | * |
||
| 327 | * @var Document |
||
| 328 | */ |
||
| 329 | protected $doc; |
||
| 330 | |||
| 331 | /** @param Document $doc */ |
||
| 332 | public function setDoc(Document $doc) { |
||
| 335 | |||
| 336 | /** @return Document */ |
||
| 337 | public function getDoc() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Original DOM document that contains a pure object from the original HTML without any cleaning |
||
| 343 | * options done on it |
||
| 344 | * |
||
| 345 | * @var Document |
||
| 346 | */ |
||
| 347 | protected $rawDoc; |
||
| 348 | |||
| 349 | /** @param Document $rawDoc */ |
||
| 350 | public function setRawDoc(Document $rawDoc) { |
||
| 353 | |||
| 354 | /** @return Document */ |
||
| 355 | public function getRawDoc() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Original psr7 response object |
||
| 361 | * |
||
| 362 | * @var \Psr\Http\Message\ResponseInterface |
||
| 363 | */ |
||
| 364 | protected $rawResponse; |
||
| 365 | |||
| 366 | /** @param \Psr\Http\Message\ResponseInterface|null $rawResponse */ |
||
| 367 | public function setRawResponse(\Psr\Http\Message\ResponseInterface $rawResponse) { |
||
| 370 | |||
| 371 | /** @return \Psr\Http\Message\ResponseInterface */ |
||
| 372 | public function getRawResponse() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Sometimes useful to try and know when the publish date of an article was |
||
| 378 | * |
||
| 379 | * @var \DateTime|null |
||
| 380 | */ |
||
| 381 | protected $publishDate; |
||
| 382 | |||
| 383 | /** @param \DateTime|null $publishDate */ |
||
| 384 | public function setPublishDate(\DateTime $publishDate = null) { |
||
| 387 | |||
| 388 | /** @return \DateTime|null */ |
||
| 389 | public function getPublishDate() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * A property bucket for consumers of goose to store custom data extractions. |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | protected $additionalData; |
||
| 399 | |||
| 400 | /** @param string $additionalData */ |
||
| 401 | public function setAdditionalData($additionalData) { |
||
| 404 | |||
| 405 | /** @return string */ |
||
| 406 | public function getAdditionalData() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Facebook Open Graph data that that is found in Article Meta tags |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | protected $openGraphData; |
||
| 416 | |||
| 417 | /** @param string $openGraphData */ |
||
| 418 | public function setOpenGraphData($openGraphData) { |
||
| 421 | |||
| 422 | /** @return string */ |
||
| 423 | public function getOpenGraphData() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Most popular words used in the lovely article |
||
| 429 | * |
||
| 430 | * @return string[] |
||
| 431 | */ |
||
| 432 | protected $popularWords = []; |
||
| 433 | |||
| 434 | /** @param string[] $popularWords */ |
||
| 435 | public function setPopularWords($popularWords) { |
||
| 438 | |||
| 439 | /** @return string[] */ |
||
| 440 | public function getPopularWords() { |
||
| 443 | } |