Total Complexity | 46 |
Total Lines | 184 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like getid3_tiff 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 getid3_tiff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class getid3_tiff extends getid3_handler |
||
27 | { |
||
28 | |||
29 | public function Analyze() { |
||
30 | |||
31 | $getid3 = $this->getid3; |
||
32 | |||
33 | fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET); |
||
34 | $tiff_header = fread($getid3->fp, 4); |
||
35 | |||
36 | $getid3->info['tiff']['byte_order'] = substr($tiff_header, 0, 2) == 'II' ? 'Intel' : 'Motorola'; |
||
37 | $endian2int = substr($tiff_header, 0, 2) == 'II' ? 'LittleEndian2Int' : 'BigEndian2Int'; |
||
38 | |||
39 | $getid3->info['fileformat'] = 'tiff'; |
||
40 | $getid3->info['video']['dataformat'] = 'tiff'; |
||
41 | $getid3->info['video']['lossless'] = true; |
||
42 | $getid3->info['tiff']['ifd'] = array (); |
||
43 | $current_ifd = array (); |
||
44 | |||
45 | $field_type_byte_length = array (1=>1, 2=>1, 3=>2, 4=>4, 5=>8); |
||
46 | |||
47 | $next_ifd_offset = getid3_lib::$endian2int(fread($getid3->fp, 4)); |
||
48 | |||
49 | while ($next_ifd_offset > 0) { |
||
50 | |||
51 | $current_ifd['offset'] = $next_ifd_offset; |
||
52 | |||
53 | fseek($getid3->fp, $getid3->info['avdataoffset'] + $next_ifd_offset, SEEK_SET); |
||
54 | $current_ifd['fieldcount'] = getid3_lib::$endian2int(fread($getid3->fp, 2)); |
||
55 | |||
56 | for ($i = 0; $i < $current_ifd['fieldcount']; $i++) { |
||
57 | |||
58 | // shortcut |
||
59 | $current_ifd['fields'][$i] = array (); |
||
60 | $current_ifd_fields_i = &$current_ifd['fields'][$i]; |
||
61 | |||
62 | $current_ifd_fields_i['raw']['tag'] = getid3_lib::$endian2int(fread($getid3->fp, 2)); |
||
63 | $current_ifd_fields_i['raw']['type'] = getid3_lib::$endian2int(fread($getid3->fp, 2)); |
||
64 | $current_ifd_fields_i['raw']['length'] = getid3_lib::$endian2int(fread($getid3->fp, 4)); |
||
65 | $current_ifd_fields_i['raw']['offset'] = fread($getid3->fp, 4); |
||
66 | |||
67 | switch ($current_ifd_fields_i['raw']['type']) { |
||
68 | case 1: // BYTE An 8-bit unsigned integer. |
||
69 | if ($current_ifd_fields_i['raw']['length'] <= 4) { |
||
70 | $current_ifd_fields_i['value'] = getid3_lib::$endian2int(substr($current_ifd_fields_i['raw']['offset'], 0, 1)); |
||
71 | } else { |
||
72 | $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']); |
||
73 | } |
||
74 | break; |
||
75 | |||
76 | case 2: // ASCII 8-bit bytes that store ASCII codes; the last byte must be null. |
||
77 | if ($current_ifd_fields_i['raw']['length'] <= 4) { |
||
78 | $current_ifd_fields_i['value'] = substr($current_ifd_fields_i['raw']['offset'], 3); |
||
79 | } else { |
||
80 | $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']); |
||
81 | } |
||
82 | break; |
||
83 | |||
84 | case 3: // SHORT A 16-bit (2-byte) unsigned integer. |
||
85 | if ($current_ifd_fields_i['raw']['length'] <= 2) { |
||
86 | $current_ifd_fields_i['value'] = getid3_lib::$endian2int(substr($current_ifd_fields_i['raw']['offset'], 0, 2)); |
||
87 | } else { |
||
88 | $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']); |
||
89 | } |
||
90 | break; |
||
91 | |||
92 | case 4: // LONG A 32-bit (4-byte) unsigned integer. |
||
93 | if ($current_ifd_fields_i['raw']['length'] <= 1) { |
||
94 | $current_ifd_fields_i['value'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']); |
||
95 | } else { |
||
96 | $current_ifd_fields_i['offset'] = getid3_lib::$endian2int($current_ifd_fields_i['raw']['offset']); |
||
97 | } |
||
98 | break; |
||
99 | |||
100 | case 5: // RATIONAL Two LONG_s: the first represents the numerator of a fraction, the second the denominator. |
||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $getid3->info['tiff']['ifd'][] = $current_ifd; |
||
106 | $current_ifd = array (); |
||
107 | $next_ifd_offset = getid3_lib::$endian2int(fread($getid3->fp, 4)); |
||
108 | |||
109 | } |
||
110 | |||
111 | foreach ($getid3->info['tiff']['ifd'] as $ifd_id => $ifd_array) { |
||
112 | foreach ($ifd_array['fields'] as $key => $field_array) { |
||
113 | switch ($field_array['raw']['tag']) { |
||
114 | case 256: // ImageWidth |
||
115 | case 257: // ImageLength |
||
116 | case 258: // BitsPerSample |
||
117 | case 259: // Compression |
||
118 | if (!isset($field_array['value'])) { |
||
119 | fseek($getid3->fp, $field_array['offset'], SEEK_SET); |
||
120 | $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'] = fread($getid3->fp, $field_array['raw']['length'] * $field_type_byte_length[$field_array['raw']['type']]); |
||
121 | } |
||
122 | break; |
||
123 | |||
124 | case 270: // ImageDescription |
||
125 | case 271: // Make |
||
126 | case 272: // Model |
||
127 | case 305: // Software |
||
128 | case 306: // DateTime |
||
129 | case 315: // Artist |
||
130 | case 316: // HostComputer |
||
131 | if (isset($field_array['value'])) { |
||
132 | $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'] = $field_array['value']; |
||
133 | } else { |
||
134 | fseek($getid3->fp, $field_array['offset'], SEEK_SET); |
||
135 | $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'] = fread($getid3->fp, $field_array['raw']['length'] * $field_type_byte_length[$field_array['raw']['type']]); |
||
136 | } |
||
137 | break; |
||
138 | } |
||
139 | switch ($field_array['raw']['tag']) { |
||
140 | case 256: // ImageWidth |
||
141 | $getid3->info['video']['resolution_x'] = $field_array['value']; |
||
142 | break; |
||
143 | |||
144 | case 257: // ImageLength |
||
145 | $getid3->info['video']['resolution_y'] = $field_array['value']; |
||
146 | break; |
||
147 | |||
148 | case 258: // BitsPerSample |
||
149 | if (isset($field_array['value'])) { |
||
150 | $getid3->info['video']['bits_per_sample'] = $field_array['value']; |
||
151 | } else { |
||
152 | $getid3->info['video']['bits_per_sample'] = 0; |
||
153 | for ($i = 0; $i < $field_array['raw']['length']; $i++) { |
||
154 | $getid3->info['video']['bits_per_sample'] += getid3_lib::$endian2int(substr($getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data'], $i * $field_type_byte_length[$field_array['raw']['type']], $field_type_byte_length[$field_array['raw']['type']])); |
||
155 | } |
||
156 | } |
||
157 | break; |
||
158 | |||
159 | case 259: // Compression |
||
160 | $getid3->info['video']['codec'] = getid3_tiff::TIFFcompressionMethod($field_array['value']); |
||
161 | break; |
||
162 | |||
163 | case 270: // ImageDescription |
||
164 | case 271: // Make |
||
165 | case 272: // Model |
||
166 | case 305: // Software |
||
167 | case 306: // DateTime |
||
168 | case 315: // Artist |
||
169 | case 316: // HostComputer |
||
170 | @$getid3->info['tiff']['comments'][getid3_tiff::TIFFcommentName($field_array['raw']['tag'])][] = $getid3->info['tiff']['ifd'][$ifd_id]['fields'][$key]['raw']['data']; |
||
171 | break; |
||
172 | |||
173 | default: |
||
174 | break; |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | return true; |
||
180 | } |
||
181 | |||
182 | |||
183 | |||
184 | public static function TIFFcompressionMethod($id) { |
||
194 | } |
||
195 | |||
196 | |||
197 | |||
198 | public static function TIFFcommentName($id) { |
||
215 | ?> |
||
|
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.