Conditions | 18 |
Paths | 38 |
Total Lines | 58 |
Lines | 10 |
Ratio | 17.24 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
43 | public function Analyze() { |
||
44 | $info = &$this->getid3->info; |
||
45 | $filesize = $info['avdataend'] - $info['avdataoffset']; |
||
46 | if ($filesize > $this->max_torrent_filesize) { // |
||
47 | $this->error('File larger ('.number_format($filesize).' bytes) than $max_torrent_filesize ('.number_format($this->max_torrent_filesize).' bytes), increase getid3_torrent->max_torrent_filesize if needed'); |
||
48 | return false; |
||
49 | } |
||
50 | $this->fseek($info['avdataoffset']); |
||
51 | $TORRENT = $this->fread($filesize); |
||
52 | $offset = 0; |
||
53 | View Code Duplication | if (!preg_match('#^(d8\\:announce|d7\\:comment)#', $TORRENT)) { |
|
|
|||
54 | $this->error('Expecting "d8:announce" or "d7:comment" at '.$info['avdataoffset'].', found "'.substr($TORRENT, $offset, 12).'" instead.'); |
||
55 | return false; |
||
56 | } |
||
57 | $info['fileformat'] = 'torrent'; |
||
58 | |||
59 | $info['torrent'] = $this->NextEntity($TORRENT, $offset); |
||
60 | if ($this->infohash) { |
||
61 | $info['torrent']['infohash'] = $this->infohash; |
||
62 | } |
||
63 | |||
64 | if (empty($info['torrent']['info']['length']) && !empty($info['torrent']['info']['files'][0]['length'])) { |
||
65 | $info['torrent']['info']['length'] = 0; |
||
66 | View Code Duplication | foreach ($info['torrent']['info']['files'] as $key => $filedetails) { |
|
67 | $info['torrent']['info']['length'] += $filedetails['length']; |
||
68 | } |
||
69 | } |
||
70 | if (!empty($info['torrent']['info']['length']) && !empty($info['torrent']['info']['piece length']) && !empty($info['torrent']['info']['pieces'])) { |
||
71 | $num_pieces_size = ceil($info['torrent']['info']['length'] / $info['torrent']['info']['piece length']); |
||
72 | $num_pieces_hash = strlen($info['torrent']['info']['pieces']) / getid3_torrent::PIECE_HASHLENGTH; // should be concatenated 20-byte SHA1 hashes |
||
73 | if ($num_pieces_hash == $num_pieces_size) { |
||
74 | $info['torrent']['info']['piece_hash'] = array(); |
||
75 | for ($i = 0; $i < $num_pieces_size; $i++) { |
||
76 | $info['torrent']['info']['piece_hash'][$i] = ''; |
||
77 | for ($j = 0; $j < getid3_torrent::PIECE_HASHLENGTH; $j++) { |
||
78 | $info['torrent']['info']['piece_hash'][$i] .= sprintf('%02x', ord($info['torrent']['info']['pieces'][(($i * getid3_torrent::PIECE_HASHLENGTH) + $j)])); |
||
79 | } |
||
80 | } |
||
81 | unset($info['torrent']['info']['pieces']); |
||
82 | } else { |
||
83 | $this->warning('found '.$num_pieces_size.' pieces based on file/chunk size; found '.$num_pieces_hash.' pieces in hash table'); |
||
84 | } |
||
85 | } |
||
86 | if (!empty($info['torrent']['info']['name']) && !empty($info['torrent']['info']['length']) && !isset($info['torrent']['info']['files'])) { |
||
87 | // single-file torrent |
||
88 | $info['torrent']['files'] = array($info['torrent']['info']['name'] => $info['torrent']['info']['length']); |
||
89 | } elseif (!empty($info['torrent']['info']['files'])) { |
||
90 | // multi-file torrent |
||
91 | $info['torrent']['files'] = array(); |
||
92 | View Code Duplication | foreach ($info['torrent']['info']['files'] as $key => $filedetails) { |
|
93 | $info['torrent']['files'][implode('/', $filedetails['path'])] = $filedetails['length']; |
||
94 | } |
||
95 | } else { |
||
96 | $this->warning('no files found'); |
||
97 | } |
||
98 | |||
99 | return true; |
||
100 | } |
||
101 | |||
248 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.