Complex classes like Torrent 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 Torrent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Torrent extends AbstractModel |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var integer |
||
| 13 | */ |
||
| 14 | protected $id; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var integer |
||
| 18 | */ |
||
| 19 | protected $eta; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var integer |
||
| 23 | */ |
||
| 24 | protected $size; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $name; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $hash; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Status |
||
| 38 | */ |
||
| 39 | protected $status; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected $finished; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var integer |
||
| 48 | */ |
||
| 49 | protected $startDate; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var integer |
||
| 53 | */ |
||
| 54 | protected $uploadRate; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var integer |
||
| 58 | */ |
||
| 59 | protected $downloadRate; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var integer |
||
| 63 | */ |
||
| 64 | protected $peersConnected; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var double |
||
| 68 | */ |
||
| 69 | protected $percentDone; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $files = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $peers = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $trackers = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $trackerStats = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var double |
||
| 93 | */ |
||
| 94 | protected $uploadRatio; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $downloadDir; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var integer |
||
| 103 | */ |
||
| 104 | protected $downloadedEver; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var integer |
||
| 108 | */ |
||
| 109 | protected $uploadedEver; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param integer $id |
||
| 113 | */ |
||
| 114 | public function setId($id) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return integer |
||
| 121 | */ |
||
| 122 | public function getId() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param integer $eta |
||
| 129 | */ |
||
| 130 | public function setEta($eta) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return integer |
||
| 137 | */ |
||
| 138 | public function getEta() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param integer $size |
||
| 145 | */ |
||
| 146 | public function setSize($size) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return integer |
||
| 153 | */ |
||
| 154 | public function getSize() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $name |
||
| 161 | */ |
||
| 162 | public function setName($name) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getName() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $hash |
||
| 177 | */ |
||
| 178 | public function setHash($hash) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getHash() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param integer|Status $status |
||
| 193 | */ |
||
| 194 | public function setStatus($status) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return integer |
||
| 201 | */ |
||
| 202 | public function getStatus() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param boolean $finished |
||
| 209 | */ |
||
| 210 | public function setFinished($finished) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | public function isFinished() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @var integer $startDate |
||
| 225 | */ |
||
| 226 | public function setStartDate($startDate) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return integer |
||
| 233 | */ |
||
| 234 | public function getStartDate() |
||
| 238 | /** |
||
| 239 | * @var integer $rate |
||
| 240 | */ |
||
| 241 | public function setUploadRate($rate) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return integer |
||
| 248 | */ |
||
| 249 | public function getUploadRate() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param integer $rate |
||
| 256 | */ |
||
| 257 | public function setDownloadRate($rate) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param integer $peersConnected |
||
| 264 | */ |
||
| 265 | public function setPeersConnected($peersConnected) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return integer |
||
| 272 | */ |
||
| 273 | public function getPeersConnected() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return integer |
||
| 280 | */ |
||
| 281 | public function getDownloadRate() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param double $done |
||
| 288 | */ |
||
| 289 | public function setPercentDone($done) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return double |
||
| 296 | */ |
||
| 297 | public function getPercentDone() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param array $files |
||
| 304 | */ |
||
| 305 | public function setFiles(array $files) |
||
| 306 | { |
||
| 307 | $this->files = array_map(function ($file) { |
||
| 308 | return PropertyMapper::map(new File(), $file); |
||
| 309 | }, $files); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function getFiles() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param array $peers |
||
| 322 | */ |
||
| 323 | public function setPeers(array $peers) |
||
| 324 | { |
||
| 325 | $this->peers = array_map(function ($peer) { |
||
| 326 | return PropertyMapper::map(new Peer(), $peer); |
||
| 327 | }, $peers); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | public function getPeers() |
||
| 337 | /** |
||
| 338 | * @param array $trackerStats |
||
| 339 | */ |
||
| 340 | public function setTrackerStats(array $trackerStats) |
||
| 341 | { |
||
| 342 | $this->trackerStats = array_map(function ($trackerStats) { |
||
| 343 | return PropertyMapper::map(new TrackerStats(), $trackerStats); |
||
| 344 | }, $trackerStats); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getTrackerStats() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param array $trackers |
||
| 357 | */ |
||
| 358 | public function setTrackers(array $trackers) |
||
| 359 | { |
||
| 360 | $this->trackers = array_map(function ($tracker) { |
||
| 361 | return PropertyMapper::map(new Tracker(), $tracker); |
||
| 362 | }, $trackers); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public function getTrackers() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param double $ratio |
||
| 375 | */ |
||
| 376 | public function setUploadRatio($ratio) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return double |
||
| 383 | */ |
||
| 384 | public function getUploadRatio() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return boolean |
||
| 391 | */ |
||
| 392 | public function isStopped() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @return boolean |
||
| 399 | */ |
||
| 400 | public function isChecking() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return boolean |
||
| 407 | */ |
||
| 408 | public function isDownloading() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return boolean |
||
| 415 | */ |
||
| 416 | public function isSeeding() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getDownloadDir() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $downloadDir |
||
| 431 | */ |
||
| 432 | public function setDownloadDir($downloadDir) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return int |
||
| 439 | */ |
||
| 440 | public function getDownloadedEver() { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param int $downloadedEver |
||
| 446 | */ |
||
| 447 | public function setDownloadedEver($downloadedEver) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return int |
||
| 453 | */ |
||
| 454 | public function getUploadedEver() { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param int $uploadedEver |
||
| 460 | */ |
||
| 461 | public function setUploadedEver($uploadedEver) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * {@inheritDoc} |
||
| 467 | */ |
||
| 468 | public static function getMapping() |
||
| 493 | } |
||
| 494 |