Complex classes like Refiller 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 Refiller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Refiller implements RefillerInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | const NAME = 'shikimori'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | const TITLE = 'Shikimori.org'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $supported_fields = [ |
||
| 34 | self::FIELD_DATE_END, |
||
| 35 | self::FIELD_DATE_PREMIERE, |
||
| 36 | self::FIELD_DURATION, |
||
| 37 | self::FIELD_EPISODES_NUMBER, |
||
| 38 | self::FIELD_GENRES, |
||
| 39 | self::FIELD_IMAGES, |
||
| 40 | self::FIELD_NAMES, |
||
| 41 | self::FIELD_STUDIO, |
||
| 42 | self::FIELD_SOURCES, |
||
| 43 | self::FIELD_SUMMARY, |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Browser |
||
| 48 | */ |
||
| 49 | private $browser; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Filler |
||
| 53 | */ |
||
| 54 | protected $filler; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Search |
||
| 58 | */ |
||
| 59 | protected $search; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Browser $browser |
||
| 63 | * @param Filler $filler |
||
| 64 | * @param Search $search |
||
| 65 | */ |
||
| 66 | public function __construct(Browser $browser, Filler $filler, Search $search) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function getName() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getTitle() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Is can refill item from source. |
||
| 91 | * |
||
| 92 | * @param Item $item |
||
| 93 | * @param string $field |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function isCanRefill(Item $item, $field) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Refill item field from source. |
||
| 104 | * |
||
| 105 | * @param Item $item |
||
| 106 | * @param string $field |
||
| 107 | * |
||
| 108 | * @return Item |
||
| 109 | */ |
||
| 110 | public function refill(Item $item, $field) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param Item $item |
||
| 174 | * @param string $field |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function isCanSearch(Item $item, $field) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Search items for refill. |
||
| 200 | * |
||
| 201 | * @param Item $item |
||
| 202 | * @param string $field |
||
| 203 | * |
||
| 204 | * @return ItemRefiller[] |
||
| 205 | */ |
||
| 206 | public function search(Item $item, $field) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Refill item field from search result. |
||
| 254 | * |
||
| 255 | * @param Item $item |
||
| 256 | * @param string $field |
||
| 257 | * @param array $data |
||
| 258 | * |
||
| 259 | * @return Item |
||
| 260 | */ |
||
| 261 | public function refillFromSearchResult(Item $item, $field, array $data) |
||
| 262 | { |
||
| 263 | if (!empty($data['url'])) { |
||
| 264 | $item->addSource((new Source())->setUrl($data['url'])); |
||
| 265 | $item = $this->refill($item, $field); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $item; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param Item $item |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getSourceForFill(Item $item) |
||
| 287 | } |
||
| 288 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: