Total Complexity | 53 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like ID3 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.
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 ID3, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | final class ID3{ |
||
31 | |||
32 | private const BITRATES = [ |
||
33 | 0b0000 => [ 0, 0, 0, 0, 0], |
||
34 | 0b0001 => [ 32, 32, 32, 32, 8], |
||
35 | 0b0010 => [ 64, 48, 40, 48, 16], |
||
36 | 0b0011 => [ 96, 56, 48, 56, 24], |
||
37 | 0b0100 => [128, 64, 56, 64, 32], |
||
38 | 0b0101 => [160, 80, 64, 80, 40], |
||
39 | 0b0110 => [192, 96, 80, 96, 48], |
||
40 | 0b0111 => [224, 112, 96, 112, 56], |
||
41 | 0b1000 => [256, 128, 112, 128, 64], |
||
42 | 0b1001 => [288, 160, 128, 144, 80], |
||
43 | 0b1010 => [320, 192, 160, 160, 96], |
||
44 | 0b1011 => [352, 224, 192, 176, 112], |
||
45 | 0b1100 => [384, 256, 224, 192, 128], |
||
46 | 0b1101 => [416, 320, 256, 224, 144], |
||
47 | 0b1110 => [448, 384, 320, 256, 160], |
||
48 | 0b1111 => [ -1, -1, -1, -1, -1], |
||
49 | ]; |
||
50 | |||
51 | private const SAMPLE_RATES = [ |
||
52 | 0b00 => [ |
||
53 | 0b00 => 11025, |
||
54 | 0b01 => 12000, |
||
55 | 0b10 => 8000, |
||
56 | 0b11 => 0, |
||
57 | ], |
||
58 | 0b10 => [ |
||
59 | 0b00 => 22050, |
||
60 | 0b01 => 24000, |
||
61 | 0b10 => 16000, |
||
62 | 0b11 => 0, |
||
63 | ], |
||
64 | 0b11 => [ |
||
65 | 0b00 => 44100, |
||
66 | 0b01 => 48000, |
||
67 | 0b10 => 32000, |
||
68 | 0b11 => 0, |
||
69 | ], |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * "Xing"/"Info" identification string at 0x0D (13), 0x15 (21) or 0x24 (36) |
||
74 | */ |
||
75 | private const VBR_ID_STRING_POS = [ |
||
76 | // v2.5 |
||
77 | 0b00 => [ |
||
78 | 0b00 => 21, // stereo |
||
79 | 0b01 => 21, // jntstereo |
||
80 | 0b10 => 21, // dual channel |
||
81 | 0b11 => 21, // mono |
||
82 | ], |
||
83 | // v2 |
||
84 | 0b10 => [ |
||
85 | 0b00 => 21, |
||
86 | 0b01 => 21, |
||
87 | 0b10 => 21, |
||
88 | 0b11 => 13, |
||
89 | ], |
||
90 | // v1 |
||
91 | 0b11 => [ |
||
92 | 0b00 => 36, |
||
93 | 0b01 => 36, |
||
94 | 0b10 => 36, |
||
95 | 0b11 => 21, |
||
96 | ], |
||
97 | ]; |
||
98 | |||
99 | /** |
||
100 | * @var resource |
||
101 | */ |
||
102 | private $fh; |
||
103 | |||
104 | /** |
||
105 | * @return void |
||
106 | */ |
||
107 | public function __destruct(){ |
||
108 | |||
109 | if(is_resource($this->fh)){ |
||
110 | fclose($this->fh); // @codeCoverageIgnore |
||
111 | } |
||
112 | |||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @throws \chillerlan\ID3Tag\ID3Exception |
||
117 | */ |
||
118 | public function read(string $filename):ID3Data{ |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * |
||
172 | * @noinspection PhpUnusedLocalVariableInspection |
||
173 | */ |
||
174 | private function readID3v2Tag(string $rawdata):?array{ |
||
175 | |||
176 | /** |
||
177 | * 3.1. ID3v2 header |
||
178 | * |
||
179 | * The ID3v2 tag header, which should be the first information in the |
||
180 | * file, is 10 bytes as follows: |
||
181 | * |
||
182 | * ID3v2/file identifier "ID3" |
||
183 | * ID3v2 version $03 00 |
||
184 | * ID3v2 flags %abc00000 |
||
185 | * ID3v2 size 4 * %0xxxxxxx |
||
186 | * |
||
187 | * @link http://id3.org/id3v2.3.0#ID3v2_header |
||
188 | */ |
||
189 | $id3version = ord(substr($rawdata, 3, 1)); |
||
190 | |||
191 | if(!in_array($id3version, [2, 3, 4], true)){ |
||
192 | # throw new ID3Exception('invalid id3 version'); |
||
193 | return null; |
||
194 | } |
||
195 | |||
196 | $flags = ord(substr($rawdata, 5, 1)); |
||
197 | |||
198 | $compression = false; |
||
|
|||
199 | $exthead = false; |
||
200 | $experimental = false; |
||
201 | $hasfooter = false; |
||
202 | |||
203 | /** |
||
204 | * a - Unsynchronisation |
||
205 | * |
||
206 | * Bit 7 in the 'ID3v2 flags' indicates whether or not |
||
207 | * unsynchronisation is used (see section 5 for details); a set bit |
||
208 | * indicates usage. |
||
209 | */ |
||
210 | $unsync = (bool)($flags & 0b10000000); |
||
211 | |||
212 | if($id3version === 2){ |
||
213 | |||
214 | /** |
||
215 | * b - Compression |
||
216 | * |
||
217 | * Bit 6 is indicating whether or not compression is used; |
||
218 | * a set bit indicates usage. Since no compression scheme has been |
||
219 | * decided yet, the ID3 decoder (for now) should just ignore the entire |
||
220 | * tag if the compression bit is set. |
||
221 | */ |
||
222 | $compression = (bool)($flags & 0b01000000); |
||
223 | } |
||
224 | |||
225 | if($id3version === 3 || $id3version === 4){ |
||
226 | |||
227 | /** |
||
228 | * b - Extended header |
||
229 | * |
||
230 | * Bit 6 indicates whether or not the header is followed by an |
||
231 | * extended header. The extended header is described in section 3.2. |
||
232 | */ |
||
233 | $exthead = (bool)($flags & 0b01000000); |
||
234 | |||
235 | /** |
||
236 | * c - Experimental indicator |
||
237 | * |
||
238 | * Bit 5 should be used as an 'experimental indicator'. |
||
239 | * This flag should always be set when the tag is in an experimental stage. |
||
240 | */ |
||
241 | $experimental = (bool)($flags & 0b00100000); |
||
242 | } |
||
243 | |||
244 | if($id3version === 4){ |
||
245 | |||
246 | /** |
||
247 | * d - Footer present |
||
248 | * |
||
249 | * Bit 4 indicates that a footer (section 3.4) is present at the very |
||
250 | * end of the tag. A set bit indicates the presence of a footer. |
||
251 | */ |
||
252 | $hasfooter = (bool)($flags & 0b00010000); |
||
253 | } |
||
254 | |||
255 | $start = 10; |
||
256 | |||
257 | /** |
||
258 | * 3.2. ID3v2.3 extended header |
||
259 | * |
||
260 | * @link http://id3.org/id3v2.3.0#ID3v2_extended_header |
||
261 | * |
||
262 | * Extended header size $xx xx xx xx |
||
263 | * Extended Flags $xx xx |
||
264 | * Size of padding $xx xx xx xx |
||
265 | */ |
||
266 | if($exthead){ |
||
267 | $extHeaderSize = ID3Helpers::syncSafeInteger(unpack('N', substr($rawdata, $start, 4))[1]); |
||
268 | $start += 4; |
||
269 | // @todo (skipping the extended header for now...) |
||
270 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
271 | $extHeader = substr($rawdata, $start, $extHeaderSize); |
||
272 | $start += $extHeaderSize; |
||
273 | } |
||
274 | |||
275 | $tagdata = substr($rawdata, $start); |
||
276 | |||
277 | if($unsync && $id3version <= 3){ |
||
278 | $tagdata = ID3Helpers::unsyncString($tagdata); |
||
279 | } |
||
280 | |||
281 | $parser = __NAMESPACE__.'\\ID3v2'.$id3version; |
||
282 | |||
283 | return (new $parser)->parse($tagdata); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * |
||
288 | */ |
||
289 | private function getMP3Stats(int $filesize, int $v1Tagsize, int $v2Tagsize):array{ |
||
290 | $offset = $this->getMP3FrameStart($filesize, $v1Tagsize, $v2Tagsize); |
||
291 | $framecount = 0; |
||
292 | $duration = 0.0; |
||
293 | $bitrate = 0; |
||
294 | |||
295 | if($offset === null){ |
||
296 | return []; |
||
297 | } |
||
298 | |||
299 | while($offset < $filesize){ |
||
300 | $frame = $this->parseMP3FrameHeader($offset); |
||
301 | |||
302 | if($frame === null){ |
||
303 | $offset = $this->getMP3FrameStart($filesize, $v1Tagsize, $offset); |
||
304 | |||
305 | if($offset !== null){ |
||
306 | continue; |
||
307 | } |
||
308 | |||
309 | break; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * In the Info Tag, the "Xing" identification string at 0x0D (13), |
||
314 | * 0x15 (21) or 0x24 (36) (depending on MPEG layer and number of channels) |
||
315 | * of the header is replaced by "Info" in case of a CBR file. |
||
316 | * |
||
317 | * This was done to avoid CBR files to be recognized as traditional |
||
318 | * Xing VBR files by some decoders. Although the two identification |
||
319 | * strings "Xing" and "Info" are both valid, it is suggested that you |
||
320 | * keep the identification string "Xing" in case of VBR bistream |
||
321 | * in order to keep compatibility. |
||
322 | * |
||
323 | * @link http://gabriel.mp3-tech.org/mp3infotag.html |
||
324 | */ |
||
325 | if($framecount === 0){ |
||
326 | $pos = $this::VBR_ID_STRING_POS[$frame->version][$frame->channelmode] ?? 36; |
||
327 | fseek($this->fh, $offset + $pos, SEEK_SET); |
||
328 | |||
329 | if(strtolower(fread($this->fh, 4)) !== 'xing'){ // lower just in case someone thinks they're special |
||
330 | |||
331 | return [ |
||
332 | 'duration' => (int)round(($filesize - $v1Tagsize - $v2Tagsize) / (($frame->bitrate * 1000) / 8)), |
||
333 | 'bitrate' => $frame->bitrate, |
||
334 | ]; |
||
335 | } |
||
336 | |||
337 | } |
||
338 | |||
339 | $duration += $frame->duration; |
||
340 | $offset += $frame->length; |
||
341 | $bitrate += $frame->bitrate; |
||
342 | |||
343 | $framecount++; |
||
344 | } |
||
345 | |||
346 | return [ |
||
347 | 'duration' => (int)round($duration), |
||
348 | 'bitrate' => (int)round($bitrate / $framecount), |
||
349 | 'framecount' => $framecount, |
||
350 | ]; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * |
||
355 | */ |
||
356 | private function getMP3FrameStart(int $filesize, int $v1Tagsize, int $offset):?int{ |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * |
||
388 | */ |
||
389 | private function parseMP3FrameHeader(int $offset):?stdClass{ |
||
506 |