Conditions | 26 |
Paths | 4563 |
Total Lines | 215 |
Code Lines | 115 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
33 | public function Analyze() |
||
34 | { |
||
35 | $info = &$this->getid3->info; |
||
36 | |||
37 | $info['fileformat'] = 'gzip'; |
||
38 | |||
39 | $start_length = 10; |
||
40 | $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os'; |
||
41 | |||
42 | //+---+---+---+---+---+---+---+---+---+---+ |
||
43 | //|ID1|ID2|CM |FLG| MTIME |XFL|OS | |
||
44 | //+---+---+---+---+---+---+---+---+---+---+ |
||
45 | |||
46 | @fseek($this->getid3->fp, 0); |
||
|
|||
47 | $buffer = @fread($this->getid3->fp, $info['filesize']); |
||
48 | |||
49 | $arr_members = explode("\x1F\x8B\x08", $buffer); |
||
50 | |||
51 | while (true) { |
||
52 | $is_wrong_members = false; |
||
53 | $num_members = intval(count($arr_members)); |
||
54 | for ($i = 0; $i < $num_members; $i++) { |
||
55 | if (0 == strlen($arr_members[$i])) { |
||
56 | continue; |
||
57 | } |
||
58 | $buf = "\x1F\x8B\x08" . $arr_members[$i]; |
||
59 | |||
60 | $attr = unpack($unpack_header, substr($buf, 0, $start_length)); |
||
61 | if (!$this->get_os_type(ord($attr['os']))) { |
||
62 | // Merge member with previous if wrong OS type |
||
63 | $arr_members[$i - 1] .= $buf; |
||
64 | $arr_members[$i] = ''; |
||
65 | $is_wrong_members = true; |
||
66 | continue; |
||
67 | } |
||
68 | } |
||
69 | if (!$is_wrong_members) { |
||
70 | break; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | $fpointer = 0; |
||
75 | $idx = 0; |
||
76 | for ($i = 0; $i < $num_members; $i++) { |
||
77 | if (0 == strlen($arr_members[$i])) { |
||
78 | continue; |
||
79 | } |
||
80 | $info_gzip_member_header_idx = &$info['gzip']['member_header'][++$idx]; |
||
81 | |||
82 | $buff = "\x1F\x8B\x08" . $arr_members[$i]; |
||
83 | |||
84 | $attr = unpack($unpack_header, substr($buff, 0, $start_length)); |
||
85 | $info_gzip_member_header_idx['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']); |
||
86 | $info_gzip_member_header_idx['raw']['id1'] = ord($attr['cmethod']); |
||
87 | $info_gzip_member_header_idx['raw']['id2'] = ord($attr['cmethod']); |
||
88 | $info_gzip_member_header_idx['raw']['cmethod'] = ord($attr['cmethod']); |
||
89 | $info_gzip_member_header_idx['raw']['os'] = ord($attr['os']); |
||
90 | $info_gzip_member_header_idx['raw']['xflags'] = ord($attr['xflags']); |
||
91 | $info_gzip_member_header_idx['raw']['flags'] = ord($attr['flags']); |
||
92 | |||
93 | $info_gzip_member_header_idx['flags']['crc16'] = (bool)($info_gzip_member_header_idx['raw']['flags'] & 0x02); |
||
94 | $info_gzip_member_header_idx['flags']['extra'] = (bool)($info_gzip_member_header_idx['raw']['flags'] & 0x04); |
||
95 | $info_gzip_member_header_idx['flags']['filename'] = (bool)($info_gzip_member_header_idx['raw']['flags'] & 0x08); |
||
96 | $info_gzip_member_header_idx['flags']['comment'] = (bool)($info_gzip_member_header_idx['raw']['flags'] & 0x10); |
||
97 | |||
98 | $info_gzip_member_header_idx['compression'] = $this->get_xflag_type($info_gzip_member_header_idx['raw']['xflags']); |
||
99 | |||
100 | $info_gzip_member_header_idx['os'] = $this->get_os_type($info_gzip_member_header_idx['raw']['os']); |
||
101 | if (!$info_gzip_member_header_idx['os']) { |
||
102 | $info['error'][] = 'Read error on gzip file'; |
||
103 | return false; |
||
104 | } |
||
105 | |||
106 | $fpointer = 10; |
||
107 | $arr_xsubfield = []; |
||
108 | |||
109 | // bit 2 - FLG.FEXTRA |
||
110 | //+---+---+=================================+ |
||
111 | //| XLEN |...XLEN bytes of "extra field"...| |
||
112 | //+---+---+=================================+ |
||
113 | |||
114 | if ($info_gzip_member_header_idx['flags']['extra']) { |
||
115 | $w_xlen = substr($buff, $fpointer, 2); |
||
116 | $xlen = getid3_lib::LittleEndian2Int($w_xlen); |
||
117 | $fpointer += 2; |
||
118 | |||
119 | $info_gzip_member_header_idx['raw']['xfield'] = substr($buff, $fpointer, $xlen); |
||
120 | |||
121 | // Extra SubFields |
||
122 | //+---+---+---+---+==================================+ |
||
123 | //|SI1|SI2| LEN |... LEN bytes of subfield data ...| |
||
124 | //+---+---+---+---+==================================+ |
||
125 | |||
126 | $idx = 0; |
||
127 | while (true) { |
||
128 | if ($idx >= $xlen) { |
||
129 | break; |
||
130 | } |
||
131 | $si1 = ord(substr($buff, $fpointer + $idx++, 1)); |
||
132 | $si2 = ord(substr($buff, $fpointer + $idx++, 1)); |
||
133 | if ((0x41 == $si1) && (0x70 == $si2)) { |
||
134 | $w_xsublen = substr($buff, $fpointer + $idx, 2); |
||
135 | $xsublen = getid3_lib::LittleEndian2Int($w_xsublen); |
||
136 | $idx += 2; |
||
137 | $arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen); |
||
138 | $idx += $xsublen; |
||
139 | } else { |
||
140 | break; |
||
141 | } |
||
142 | } |
||
143 | $fpointer += $xlen; |
||
144 | } |
||
145 | |||
146 | // bit 3 - FLG.FNAME |
||
147 | //+=========================================+ |
||
148 | //|...original file name, zero-terminated...| |
||
149 | //+=========================================+ |
||
150 | // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz |
||
151 | |||
152 | $info_gzip_member_header_idx['filename'] = eregi_replace('.gz$', '', @$info['filename']); |
||
153 | if ($info_gzip_member_header_idx['flags']['filename']) { |
||
154 | while (true) { |
||
155 | if (0 == ord($buff[$fpointer])) { |
||
156 | $fpointer++; |
||
157 | break; |
||
158 | } |
||
159 | $info_gzip_member_header_idx['filename'] .= $buff[$fpointer]; |
||
160 | $fpointer++; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // bit 4 - FLG.FCOMMENT |
||
165 | //+===================================+ |
||
166 | //|...file comment, zero-terminated...| |
||
167 | //+===================================+ |
||
168 | |||
169 | if ($info_gzip_member_header_idx['flags']['comment']) { |
||
170 | while (true) { |
||
171 | if (0 == ord($buff[$fpointer])) { |
||
172 | $fpointer++; |
||
173 | break; |
||
174 | } |
||
175 | $info_gzip_member_header_idx['comment'] .= $buff[$fpointer]; |
||
176 | $fpointer++; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | // bit 1 - FLG.FHCRC |
||
181 | //+---+---+ |
||
182 | //| CRC16 | |
||
183 | //+---+---+ |
||
184 | |||
185 | if ($info_gzip_member_header_idx['flags']['crc16']) { |
||
186 | $w_crc = substr($buff, $fpointer, 2); |
||
187 | $info_gzip_member_header_idx['crc16'] = getid3_lib::LittleEndian2Int($w_crc); |
||
188 | $fpointer += 2; |
||
189 | } |
||
190 | |||
191 | // bit 0 - FLG.FTEXT |
||
192 | //if ($info_gzip_member_header_idx['raw']['flags'] & 0x01) { |
||
193 | // Ignored... |
||
194 | //} |
||
195 | // bits 5, 6, 7 - reserved |
||
196 | |||
197 | $info_gzip_member_header_idx['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4)); |
||
198 | $info_gzip_member_header_idx['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4)); |
||
199 | |||
200 | if ($this->option_gzip_parse_contents) { |
||
201 | // Try to inflate GZip |
||
202 | |||
203 | if (!function_exists('gzinflate')) { |
||
204 | $this->getid3->warning('PHP does not have zlib support - contents not parsed.'); |
||
205 | return true; |
||
206 | } |
||
207 | |||
208 | $csize = 0; |
||
209 | $inflated = ''; |
||
210 | $chkcrc32 = ''; |
||
211 | |||
212 | $cdata = substr($buff, $fpointer); |
||
213 | $cdata = substr($cdata, 0, strlen($cdata) - 8); |
||
214 | $csize = strlen($cdata); |
||
215 | $inflated = gzinflate($cdata); |
||
216 | |||
217 | // Calculate CRC32 for inflated content |
||
218 | $info_gzip_member_header_idx['crc32_valid'] = (bool)(sprintf('%u', crc32($inflated)) == $info_gzip_member_header_idx['crc32']); |
||
219 | |||
220 | //// Analyse contents |
||
221 | |||
222 | // write content to temp file |
||
223 | if (false === ($temp_file_name = tempnam('*', 'getID3'))) { |
||
224 | throw new getid3_exception('Unable to create temporary file.'); |
||
225 | } |
||
226 | |||
227 | if ($tmp = fopen($temp_file_name, 'wb')) { |
||
228 | fwrite($tmp, $inflated); |
||
229 | fclose($tmp); |
||
230 | |||
231 | // clone getid3 - we want same settings |
||
232 | $clone = clone $this->getid3; |
||
233 | unset($clone->info); |
||
234 | try { |
||
235 | $clone->Analyze($temp_file_name); |
||
236 | $info_gzip_member_header_idx['parsed_content'] = $clone->info; |
||
237 | } catch (getid3_exception $e) { |
||
238 | // unable to parse contents |
||
239 | } |
||
240 | |||
241 | unlink($temp_file_name); |
||
242 | } // Unknown/unhandled format |
||
243 | else { |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | return true; |
||
248 | } |
||
287 |
If you suppress an error, we recommend checking for the error condition explicitly: