Complex classes like Feed 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 Feed, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Feed extends AbstractTemplate |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $title = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $url = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $description = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $language = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $charset = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $category = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $pubdate = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $webmaster = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $generator = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $copyright = ''; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $lastbuild = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | private $editor = ''; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | private $ttl = 60; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $image_title = ''; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $image_url = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $image_link = ''; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var int |
||
| 111 | */ |
||
| 112 | private $image_width = 200; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | private $image_height = 50; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $items = array(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * init - called by parent::_construct |
||
| 126 | * |
||
| 127 | * @return void |
||
| 128 | */ |
||
| 129 | protected function init() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Render the feed and display it directly |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | protected function render() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * setCategory |
||
| 181 | * |
||
| 182 | * @param string $category feed category |
||
| 183 | * |
||
| 184 | * @return Feed |
||
| 185 | */ |
||
| 186 | public function setCategory($category) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * getCategory |
||
| 194 | * |
||
| 195 | * @return string feed category |
||
| 196 | */ |
||
| 197 | public function getCategory() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * setCharset |
||
| 204 | * |
||
| 205 | * @param string $charset feed character set |
||
| 206 | * |
||
| 207 | * @return Feed |
||
| 208 | */ |
||
| 209 | public function setCharset($charset) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * getCharset |
||
| 217 | * |
||
| 218 | * @return string feed character set |
||
| 219 | */ |
||
| 220 | public function getCharset() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * setCopyright |
||
| 227 | * |
||
| 228 | * @param string $copyright feed copyright |
||
| 229 | * |
||
| 230 | * @return Feed |
||
| 231 | */ |
||
| 232 | public function setCopyright($copyright) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * getCopyright |
||
| 240 | * |
||
| 241 | * @return string feed copyright |
||
| 242 | */ |
||
| 243 | public function getCopyright() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * setDescription |
||
| 250 | * |
||
| 251 | * @param string $description feed description |
||
| 252 | * |
||
| 253 | * @return Feed |
||
| 254 | */ |
||
| 255 | public function setDescription($description) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * getDescription |
||
| 263 | * |
||
| 264 | * @return string feed description |
||
| 265 | */ |
||
| 266 | public function getDescription() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * setEditor |
||
| 273 | * |
||
| 274 | * @param string $editor editor of feed |
||
| 275 | * |
||
| 276 | * @return Feed |
||
| 277 | */ |
||
| 278 | public function setEditor($editor) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * getEditor |
||
| 286 | * |
||
| 287 | * @return string editor of feed |
||
| 288 | */ |
||
| 289 | public function getEditor() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * setGenerator |
||
| 296 | * |
||
| 297 | * @param string $generator feed generator |
||
| 298 | * |
||
| 299 | * @return Feed |
||
| 300 | */ |
||
| 301 | public function setGenerator($generator) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * getGenerator |
||
| 309 | * |
||
| 310 | * @return string feed generator |
||
| 311 | */ |
||
| 312 | public function getGenerator() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * setImageHeight |
||
| 319 | * |
||
| 320 | * @param int $image_height height of feed image |
||
| 321 | * |
||
| 322 | * @return Feed |
||
| 323 | */ |
||
| 324 | public function setImageHeight($image_height) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * getImageHeight |
||
| 332 | * |
||
| 333 | * @return int height of feed image |
||
| 334 | */ |
||
| 335 | public function getImageHeight() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * setImageLink |
||
| 342 | * |
||
| 343 | * @param string $image_link feed image link |
||
| 344 | * |
||
| 345 | * @return Feed |
||
| 346 | */ |
||
| 347 | public function setImageLink($image_link) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * getImageLink |
||
| 355 | * |
||
| 356 | * @return string feed image link |
||
| 357 | */ |
||
| 358 | public function getImageLink() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * setImageTitle |
||
| 365 | * |
||
| 366 | * @param string $image_title feed image title |
||
| 367 | * |
||
| 368 | * @return Feed |
||
| 369 | */ |
||
| 370 | public function setImageTitle($image_title) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * getImageTitle |
||
| 378 | * |
||
| 379 | * @return string feed image title |
||
| 380 | */ |
||
| 381 | public function getImageTitle() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * setImageUrl |
||
| 388 | * |
||
| 389 | * @param string $image_url url of feed image |
||
| 390 | * |
||
| 391 | * @return Feed |
||
| 392 | */ |
||
| 393 | public function setImageUrl($image_url) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * getImageUrl |
||
| 401 | * |
||
| 402 | * @return string url of feed image |
||
| 403 | */ |
||
| 404 | public function getImageUrl() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * setImageWidth |
||
| 411 | * |
||
| 412 | * @param int $image_width width of feed image |
||
| 413 | * |
||
| 414 | * @return Feed |
||
| 415 | */ |
||
| 416 | public function setImageWidth($image_width) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * getImageWidth |
||
| 424 | * |
||
| 425 | * @return int width of feed image |
||
| 426 | */ |
||
| 427 | public function getImageWidth() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * setItems |
||
| 434 | * |
||
| 435 | * @param array $items feed items |
||
| 436 | * |
||
| 437 | * @return Feed |
||
| 438 | */ |
||
| 439 | public function setItems($items) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * getItems |
||
| 447 | * |
||
| 448 | * @return array feed items |
||
| 449 | */ |
||
| 450 | public function getItems() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * setLanguage |
||
| 457 | * |
||
| 458 | * @param string $language feed language |
||
| 459 | * |
||
| 460 | * @return Feed |
||
| 461 | */ |
||
| 462 | public function setLanguage($language) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * getLanguage |
||
| 470 | * |
||
| 471 | * @return string feed language |
||
| 472 | */ |
||
| 473 | public function getLanguage() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * setLastbuild |
||
| 480 | * |
||
| 481 | * @param string $lastbuild last build time |
||
| 482 | * |
||
| 483 | * @return Feed |
||
| 484 | */ |
||
| 485 | public function setLastbuild($lastbuild) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * getLastbuild |
||
| 493 | * |
||
| 494 | * @return string last build time |
||
| 495 | */ |
||
| 496 | public function getLastbuild() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * setPubdate |
||
| 503 | * |
||
| 504 | * @param string $pubdate publish date |
||
| 505 | * |
||
| 506 | * @return Feed |
||
| 507 | */ |
||
| 508 | public function setPubdate($pubdate) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * getPubdate |
||
| 516 | * |
||
| 517 | * @return string publish date |
||
| 518 | */ |
||
| 519 | public function getPubdate() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * setTitle |
||
| 526 | * |
||
| 527 | * @param string $title feed title |
||
| 528 | * |
||
| 529 | * @return Feed |
||
| 530 | */ |
||
| 531 | public function setTitle($title) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * getTitle |
||
| 539 | * |
||
| 540 | * @return string feed title |
||
| 541 | */ |
||
| 542 | public function getTitle() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * setTtl |
||
| 549 | * |
||
| 550 | * @param int $ttl feed time to live |
||
| 551 | * |
||
| 552 | * @return Feed |
||
| 553 | */ |
||
| 554 | public function setTtl($ttl) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * getTtl |
||
| 562 | * |
||
| 563 | * @return int feed time to live |
||
| 564 | */ |
||
| 565 | public function getTtl() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * set url |
||
| 572 | * |
||
| 573 | * @param string $url feed site url |
||
| 574 | * |
||
| 575 | * @return Feed |
||
| 576 | */ |
||
| 577 | public function setUrl($url) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * getUrl |
||
| 585 | * |
||
| 586 | * @return string feed site url |
||
| 587 | */ |
||
| 588 | public function getUrl() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * setWebmaster |
||
| 595 | * |
||
| 596 | * @param string $webmaster feed site webmaster |
||
| 597 | * |
||
| 598 | * @return Feed |
||
| 599 | */ |
||
| 600 | public function setWebmaster($webmaster) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * getWebmaster |
||
| 608 | * |
||
| 609 | * @return string feed site webmaster |
||
| 610 | */ |
||
| 611 | public function getWebmaster() |
||
| 615 | } |
||
| 616 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state