Complex classes like Filler 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 Filler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Filler extends FillerPlugin |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | const NAME = 'shikimori'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | const TITLE = 'Shikimori.org'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Path to item. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const FILL_URL = '/animes/#ID#'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * RegExp for get item id. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | const REG_ITEM_ID = '#/animes/z?(?<id>\d+)\-#'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Path to item screenshots. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | const FILL_IMAGES_URL = '/animes/#ID#/screenshots'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * World-art item url. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | const WORLD_ART_URL = 'http://www.world-art.ru/animation/animation.php?id=#ID#'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * MyAnimeList item url. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | const MY_ANIME_LIST_URL = 'http://myanimelist.net/anime/#ID#'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * AniDB item url. |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | const ANI_DB_URL = 'http://anidb.net/perl-bin/animedb.pl?show=anime&aid=#ID#'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Browser |
||
| 81 | */ |
||
| 82 | private $browser; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Registry |
||
| 86 | */ |
||
| 87 | private $doctrine; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var Downloader |
||
| 91 | */ |
||
| 92 | private $downloader; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $locale; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Browser $browser |
||
| 101 | * @param Registry $doctrine |
||
| 102 | * @param Downloader $downloader |
||
| 103 | * @param string $locale |
||
| 104 | */ |
||
| 105 | public function __construct(Browser $browser, Registry $doctrine, Downloader $downloader, $locale) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getName() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getTitle() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return FillerForm |
||
| 131 | */ |
||
| 132 | public function getForm() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param ItemInterface $item |
||
| 139 | * |
||
| 140 | * @return ItemInterface |
||
| 141 | */ |
||
| 142 | public function buildMenu(ItemInterface $item) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Fill item from source. |
||
| 150 | * |
||
| 151 | * @param array $data |
||
| 152 | * |
||
| 153 | * @return Item|null |
||
| 154 | */ |
||
| 155 | public function fill(array $data) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param Item $item |
||
| 198 | * @param array $body |
||
| 199 | * |
||
| 200 | * @return Item |
||
| 201 | */ |
||
| 202 | public function setSources(Item $item, $body) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param Item $item |
||
| 223 | * @param array $body |
||
| 224 | * |
||
| 225 | * @return Item |
||
| 226 | */ |
||
| 227 | public function setNames(Item $item, $body) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param Item $item |
||
| 272 | * @param array $body |
||
| 273 | * |
||
| 274 | * @return Item |
||
| 275 | */ |
||
| 276 | public function setCover(Item $item, $body) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param Item $item |
||
| 294 | * @param array $body |
||
| 295 | * |
||
| 296 | * @return Item |
||
| 297 | */ |
||
| 298 | public function setType(Item $item, $body) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param Item $item |
||
| 318 | * @param array $body |
||
| 319 | * |
||
| 320 | * @return Item |
||
| 321 | */ |
||
| 322 | public function setGenres(Item $item, $body) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param Item $item |
||
| 349 | * @param array $body |
||
| 350 | * |
||
| 351 | * @return Item |
||
| 352 | */ |
||
| 353 | public function setStudio(Item $item, $body) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param Item $item |
||
| 379 | * @param array $body |
||
| 380 | * |
||
| 381 | * @return Item |
||
| 382 | */ |
||
| 383 | public function setImages(Item $item, $body) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $url |
||
| 402 | * @param EntityInterface $entity |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | protected function uploadImage($url, EntityInterface $entity) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $url |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | public function isSupportedUrl($url) |
||
| 420 | } |
||
| 421 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.