| Total Complexity | 41 |
| Total Lines | 321 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PeopleModel 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.
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 PeopleModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class PeopleModel extends MainModel |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Id of the people. |
||
| 15 | * |
||
| 16 | * @var string|int |
||
| 17 | */ |
||
| 18 | private $_id; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Biodata area. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $_biodata; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default constructor. |
||
| 29 | * |
||
| 30 | * @param string|int $id |
||
| 31 | * @param string $parserArea |
||
| 32 | * |
||
| 33 | * @return void |
||
| 34 | */ |
||
| 35 | public function __construct($id, $parserArea = '#contentWrapper') |
||
| 36 | { |
||
| 37 | $this->_id = $id; |
||
| 38 | $this->_url = $this->_myAnimeListUrl.'/people/'.$id; |
||
| 39 | $this->_parserArea = $parserArea; |
||
| 40 | |||
| 41 | parent::errorCheck($this); |
||
| 42 | |||
| 43 | if (!$this->_error) |
||
| 44 | $this->setBiodata(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Default call. |
||
| 49 | * |
||
| 50 | * @param string $method |
||
| 51 | * @param array $arguments |
||
| 52 | * |
||
| 53 | * @return array|string|int |
||
| 54 | */ |
||
| 55 | public function __call($method, $arguments) |
||
| 56 | { |
||
| 57 | if ($this->_error) |
||
| 58 | return $this->_error; |
||
| 59 | return call_user_func_array([$this, $method], $arguments); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get people id. |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | private function getId() |
||
| 68 | { |
||
| 69 | return $this->_id; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get people name. |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | private function getName() |
||
| 78 | { |
||
| 79 | return $this->_parser->find('h1', 0)->plaintext; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get people image. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | private function getImage() |
||
| 88 | { |
||
| 89 | $image = $this->_parser->find('#content table tr', 0)->find('td', 0)->find('img', 0); |
||
| 90 | return $image ? $image->src : ''; |
||
|
|
|||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set people biodata. |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | private function setBiodata() |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get people biodata. |
||
| 114 | * |
||
| 115 | * @param string $type Biodata type |
||
| 116 | * |
||
| 117 | * @return string|array |
||
| 118 | */ |
||
| 119 | private function getBiodata($type) |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get people more information. |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | private function getMore() |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get people voice actor list. |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | private function getVa() |
||
| 187 | { |
||
| 188 | $va = []; |
||
| 189 | $va_index = 0; |
||
| 190 | $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling(); |
||
| 191 | $va_area = $html->find('.normal_header', 0)->next_sibling(); |
||
| 192 | if ($va_area->tag == 'table') { |
||
| 193 | if ($va_area->find('tr')) { |
||
| 194 | foreach ($va_area->find('tr') as $each_va) { |
||
| 195 | |||
| 196 | // anime |
||
| 197 | $anime_image_area = $each_va->find('td', 0); |
||
| 198 | $anime_area = $each_va->find('td', 1); |
||
| 199 | |||
| 200 | $va[$va_index]['anime']['image'] = $this->getAnimeImage($anime_image_area); |
||
| 201 | $va[$va_index]['anime']['id'] = $this->getAnimeId($anime_area); |
||
| 202 | $va[$va_index]['anime']['title'] = $this->getAnimeTitle($anime_area); |
||
| 203 | |||
| 204 | // character |
||
| 205 | $character_image_area = $each_va->find('td', 3); |
||
| 206 | $character_area = $each_va->find('td', 2); |
||
| 207 | |||
| 208 | $va[$va_index]['character']['image'] = $this->getAnimeImage($character_image_area); |
||
| 209 | $va[$va_index]['character']['id'] = $this->getAnimeId($character_area); |
||
| 210 | $va[$va_index]['character']['name'] = $this->getAnimeTitle($character_area); |
||
| 211 | $va[$va_index]['character']['role'] = $this->getAnimeRole($character_area); |
||
| 212 | |||
| 213 | $va_index++; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | return $va; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get anime id |
||
| 222 | * |
||
| 223 | * @param \simplehtmldom_1_5\simple_html_dom $anime_area |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | private function getAnimeId($anime_area) |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get anime title |
||
| 236 | * |
||
| 237 | * @param \simplehtmldom_1_5\simple_html_dom $anime_area |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | private function getAnimeTitle($anime_area) |
||
| 242 | { |
||
| 243 | return $anime_area->find('a', 0)->plaintext; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get anime image |
||
| 248 | * |
||
| 249 | * @param \simplehtmldom_1_5\simple_html_dom $anime_image_area |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | private function getAnimeImage($anime_image_area) |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get anime role |
||
| 261 | * |
||
| 262 | * @param \simplehtmldom_1_5\simple_html_dom $anime_area |
||
| 263 | * @param bool $staff (Optional) |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | private function getAnimeRole($anime_area, $staff = false) |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get people staff list. |
||
| 277 | * |
||
| 278 | * @param bool $staff (Optional) |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | private function getStaff($manga = false) |
||
| 283 | { |
||
| 284 | $staff = []; |
||
| 285 | $staff_index = 0; |
||
| 286 | $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling(); |
||
| 287 | if ($manga) { |
||
| 288 | $staff_area = $html->find('.normal_header', 2)->next_sibling(); |
||
| 289 | } else { |
||
| 290 | $staff_area = $html->find('.normal_header', 1)->next_sibling(); |
||
| 291 | } |
||
| 292 | if ($staff_area->tag == 'table') { |
||
| 293 | foreach ($staff_area->find('tr') as $each_staff) { |
||
| 294 | $anime_image_area = $each_staff->find('td', 0); |
||
| 295 | $staff_area = $each_staff->find('td', 1); |
||
| 296 | |||
| 297 | $staff[$staff_index]['image'] = $this->getAnimeImage($anime_image_area); |
||
| 298 | $staff[$staff_index]['id'] = $this->getAnimeId($staff_area); |
||
| 299 | $staff[$staff_index]['title'] = $this->getAnimeTitle($staff_area); |
||
| 300 | $staff[$staff_index]['role'] = $this->getAnimeRole($staff_area, true); |
||
| 301 | |||
| 302 | $staff_index++; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | return $staff; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get people all information. |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | private function getAllInfo() |
||
| 332 | } |
||
| 333 | } |