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 | * @var double |
||
| 113 | */ |
||
| 114 | protected $metadataPercentComplete; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param integer $id |
||
| 118 | */ |
||
| 119 | public function setId($id) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return integer |
||
| 126 | */ |
||
| 127 | public function getId() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param integer $eta |
||
| 134 | */ |
||
| 135 | public function setEta($eta) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return integer |
||
| 142 | */ |
||
| 143 | public function getEta() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param integer $size |
||
| 150 | */ |
||
| 151 | public function setSize($size) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return integer |
||
| 158 | */ |
||
| 159 | public function getSize() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $name |
||
| 166 | */ |
||
| 167 | public function setName($name) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getName() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $hash |
||
| 182 | */ |
||
| 183 | public function setHash($hash) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getHash() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param integer|Status $status |
||
| 198 | */ |
||
| 199 | public function setStatus($status) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return integer |
||
| 206 | */ |
||
| 207 | public function getStatus() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param boolean $finished |
||
| 214 | */ |
||
| 215 | public function setFinished($finished) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return boolean |
||
| 222 | */ |
||
| 223 | public function isFinished() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @var integer $startDate |
||
| 230 | */ |
||
| 231 | public function setStartDate($startDate) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return integer |
||
| 238 | */ |
||
| 239 | public function getStartDate() |
||
| 243 | /** |
||
| 244 | * @var integer $rate |
||
| 245 | */ |
||
| 246 | public function setUploadRate($rate) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return integer |
||
| 253 | */ |
||
| 254 | public function getUploadRate() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param integer $rate |
||
| 261 | */ |
||
| 262 | public function setDownloadRate($rate) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param integer $peersConnected |
||
| 269 | */ |
||
| 270 | public function setPeersConnected($peersConnected) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @return integer |
||
| 277 | */ |
||
| 278 | public function getPeersConnected() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return integer |
||
| 285 | */ |
||
| 286 | public function getDownloadRate() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param double $done |
||
| 293 | */ |
||
| 294 | public function setPercentDone($done) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return double |
||
| 301 | */ |
||
| 302 | public function getPercentDone() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $files |
||
| 309 | */ |
||
| 310 | public function setFiles(array $files) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getFiles() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param array $peers |
||
| 327 | */ |
||
| 328 | public function setPeers(array $peers) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public function getPeers() |
||
| 342 | /** |
||
| 343 | * @param array $trackerStats |
||
| 344 | */ |
||
| 345 | public function setTrackerStats(array $trackerStats) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public function getTrackerStats() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param array $trackers |
||
| 362 | */ |
||
| 363 | public function setTrackers(array $trackers) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public function getTrackers() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param double $ratio |
||
| 380 | */ |
||
| 381 | public function setUploadRatio($ratio) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return double |
||
| 388 | */ |
||
| 389 | public function getUploadRatio() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return boolean |
||
| 396 | */ |
||
| 397 | public function isStopped() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return boolean |
||
| 404 | */ |
||
| 405 | public function isChecking() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return boolean |
||
| 412 | */ |
||
| 413 | public function isDownloading() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return boolean |
||
| 420 | */ |
||
| 421 | public function isSeeding() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getDownloadDir() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $downloadDir |
||
| 436 | */ |
||
| 437 | public function setDownloadDir($downloadDir) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return int |
||
| 444 | */ |
||
| 445 | public function getDownloadedEver() { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param int $downloadedEver |
||
| 451 | */ |
||
| 452 | public function setDownloadedEver($downloadedEver) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @return int |
||
| 458 | */ |
||
| 459 | public function getUploadedEver() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param int $uploadedEver |
||
| 465 | */ |
||
| 466 | public function setUploadedEver($uploadedEver) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @return double |
||
| 472 | */ |
||
| 473 | public function getMetadataPercentComplete() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param double $metadataPercentComplete |
||
| 479 | */ |
||
| 480 | public function setMetadataPercentComplete($metadataPercentComplete) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritDoc} |
||
| 486 | */ |
||
| 487 | public static function getMapping() |
||
| 513 | } |
||
| 514 |