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 |
||
| 14 | class Torrent |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Optional comment |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $comment; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $announce; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $createdBy; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $creationDate; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Info about the file(s) in the torrent |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $info; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param null $data |
||
| 50 | * @param array $meta |
||
| 51 | * @param int $piece_length |
||
| 52 | * |
||
| 53 | * @throws Exception |
||
| 54 | */ |
||
| 55 | public function __construct($data = null, $meta = array(), $piece_length = 256) |
||
| 72 | |||
| 73 | public static function createFromTorrentFile($filename, $meta = array()) |
||
| 77 | |||
| 78 | public static function createFromUrl($url, $meta = array()) |
||
| 86 | |||
| 87 | public static function createFromFilesList(array $list, $meta = array()) |
||
| 96 | |||
| 97 | public static function setMeta($instance, $data = '', $meta = array()) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function __toString() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param null $announce |
||
| 120 | * |
||
| 121 | * @return array|mixed|null|string |
||
| 122 | */ |
||
| 123 | public function announce($announce = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * |
||
| 151 | * @return null|string |
||
| 152 | */ |
||
| 153 | public function getComment() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param null $comment |
||
| 160 | * |
||
| 161 | */ |
||
| 162 | public function setComment($comment) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * |
||
| 169 | * @return string|null |
||
| 170 | */ |
||
| 171 | public function getName() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $name |
||
| 178 | * |
||
| 179 | * @return null|string |
||
| 180 | */ |
||
| 181 | public function setName($name) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param null $private |
||
| 188 | * |
||
| 189 | * @return bool|null |
||
| 190 | */ |
||
| 191 | public function isPrivate($private = null) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param null $urls |
||
| 200 | * |
||
| 201 | * @return null |
||
| 202 | */ |
||
| 203 | public function urlList($urls = null) |
||
| 204 | { |
||
| 205 | return is_null($urls) ? |
||
| 206 | isset($this->{'url-list'}) ? $this->{'url-list'} : null : |
||
| 207 | $this->touch($this->{'url-list'} = is_string($urls) ? $urls : (array) $urls); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param null $urls |
||
| 212 | * |
||
| 213 | * @return null |
||
| 214 | */ |
||
| 215 | public function httpSeeds($urls = null) |
||
| 216 | { |
||
| 217 | return is_null($urls) ? |
||
| 218 | isset($this->httpseeds) ? $this->httpseeds : null : |
||
| 219 | $this->touch($this->httpseeds = (array) $urls); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return null |
||
| 224 | */ |
||
| 225 | public function piece_length() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return null|string |
||
| 234 | */ |
||
| 235 | public function hash_info() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function content() |
||
| 258 | |||
| 259 | |||
| 260 | private function isOneFile() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | public function offset() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * |
||
| 295 | * @return int |
||
| 296 | */ |
||
| 297 | public function getSize() |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * @param null $announce |
||
| 315 | * @param null $hash_info |
||
| 316 | * @param int $timeout |
||
| 317 | * |
||
| 318 | * @return array|bool |
||
| 319 | * |
||
| 320 | * @throws Exception |
||
| 321 | */ |
||
| 322 | public function scrape($announce = null, $hash_info = null, $timeout = FileSystem::timeout) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $filename |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function saveToFile($filename = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function save() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param bool $html |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function magnet($html = true) |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * @param $data |
||
| 416 | * @param integer $piece_length |
||
| 417 | * |
||
| 418 | * @return array|bool |
||
| 419 | */ |
||
| 420 | protected function build($data, $piece_length) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param null $void |
||
| 440 | * |
||
| 441 | * @return null |
||
| 442 | */ |
||
| 443 | protected function touch($void = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param $announce |
||
| 452 | * @param array $merge |
||
| 453 | * |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | protected static function announce_list($announce, $merge = array()) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param $announce |
||
| 463 | * |
||
| 464 | * @return array|mixed |
||
| 465 | */ |
||
| 466 | protected static function first_announce($announce) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param $handle |
||
| 476 | * @param $piece_length |
||
| 477 | * @param bool $last |
||
| 478 | * |
||
| 479 | * @return bool|string |
||
| 480 | * |
||
| 481 | * @throws Exception |
||
| 482 | */ |
||
| 483 | private function pieces($handle, $piece_length, $last = true) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param $file |
||
| 503 | * @param $piece_length |
||
| 504 | * |
||
| 505 | * @return array|bool |
||
| 506 | */ |
||
| 507 | protected function file($file, $piece_length) |
||
| 508 | { |
||
| 509 | |||
| 510 | if (FileSystem::is_url($file)) { |
||
| 511 | $this->urlList($file); |
||
| 512 | } |
||
| 513 | $path = explode(DIRECTORY_SEPARATOR, $file); |
||
| 514 | return array( |
||
| 515 | 'length' => FileSystem::filesize($file), |
||
| 516 | 'name' => end($path), |
||
| 517 | 'piece length' => $piece_length, |
||
| 518 | 'pieces' => $this->pieces(self::fopen($file), $piece_length) |
||
| 519 | ); |
||
| 520 | } |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param $files |
||
| 524 | * @param $piece_length |
||
| 525 | * |
||
| 526 | * @return array |
||
| 527 | * |
||
| 528 | * @throws Exception |
||
| 529 | */ |
||
| 530 | protected function files($files, $piece_length) |
||
| 531 | { |
||
| 532 | if (!FileSystem::is_url(current($files))) { |
||
| 533 | $files = array_map('realpath', $files); |
||
| 534 | } |
||
| 535 | sort($files); |
||
| 536 | usort($files, |
||
| 537 | create_function('$a,$b', 'return strrpos($a,DIRECTORY_SEPARATOR)-strrpos($b,DIRECTORY_SEPARATOR);')); |
||
| 538 | $first = current($files); |
||
| 539 | $root = dirname($first); |
||
| 540 | if ($url = FileSystem::is_url($first)) { |
||
| 541 | $this->urlList(dirname($root) . DIRECTORY_SEPARATOR); |
||
| 542 | } |
||
| 543 | $path = explode(DIRECTORY_SEPARATOR, dirname($url ? $first : realpath($first))); |
||
| 544 | $pieces = null; |
||
| 545 | $info_files = array(); |
||
| 546 | $count = count($files) - 1; |
||
| 547 | foreach ($files as $i => $file) { |
||
| 548 | if ($path != array_intersect_assoc($file_path = explode(DIRECTORY_SEPARATOR, $file), $path)) { |
||
| 549 | throw new Exception('Files must be in the same folder: "' . $file . '" discarded'); |
||
| 550 | } |
||
| 551 | $pieces .= $this->pieces(self::fopen($file), $piece_length, $count == $i); |
||
| 552 | $info_files[] = array( |
||
| 553 | 'length' => FileSystem::filesize($file), |
||
| 554 | 'path' => array_values(array_diff($file_path, $path)) |
||
| 555 | ); |
||
| 556 | } |
||
| 557 | return array( |
||
| 558 | 'files' => $info_files, |
||
| 559 | 'name' => end($path), |
||
| 560 | 'piece length' => $piece_length, |
||
| 561 | 'pieces' => $pieces |
||
| 562 | ); |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param $dir |
||
| 567 | * @param $piece_length |
||
| 568 | * |
||
| 569 | * @return array |
||
| 570 | */ |
||
| 571 | protected function folder($dir, $piece_length) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param $filename |
||
| 578 | * |
||
| 579 | * @return bool|resource |
||
| 580 | * |
||
| 581 | * @throws Exception |
||
| 582 | */ |
||
| 583 | public static function fopen($filename) |
||
| 591 | |||
| 592 | |||
| 593 | } |
||
| 594 |