Total Complexity | 56 |
Total Lines | 379 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | Features | 2 |
Complex classes like FilterHelper 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 FilterHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class FilterHelper |
||
48 | { |
||
49 | protected $availableFilters = ['ASCIIHexDecode', 'ASCII85Decode', 'LZWDecode', 'FlateDecode', 'RunLengthDecode']; |
||
50 | |||
51 | /** |
||
52 | * Decode data using the specified filter type. |
||
53 | * |
||
54 | * @param string $filter Filter name |
||
55 | * @param string $data Data to decode |
||
56 | * |
||
57 | * @return string Decoded data string |
||
58 | * |
||
59 | * @throws \Exception |
||
60 | * @throws \Smalot\PdfParser\Exception\NotImplementedException if a certain decode function is not implemented yet |
||
61 | */ |
||
62 | public function decodeFilter(string $filter, string $data, int $decodeMemoryLimit = 0): string |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * ASCIIHexDecode |
||
97 | * |
||
98 | * Decodes data encoded in an ASCII hexadecimal representation, reproducing the original binary data. |
||
99 | * |
||
100 | * @param string $data Data to decode |
||
101 | * |
||
102 | * @return string data string |
||
103 | * |
||
104 | * @throws \Exception |
||
105 | */ |
||
106 | protected function decodeFilterASCIIHexDecode(string $data): string |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * ASCII85Decode |
||
140 | * |
||
141 | * Decodes data encoded in an ASCII base-85 representation, reproducing the original binary data. |
||
142 | * |
||
143 | * @param string $data Data to decode |
||
144 | * |
||
145 | * @return string data string |
||
146 | * |
||
147 | * @throws \Exception |
||
148 | */ |
||
149 | protected function decodeFilterASCII85Decode(string $data): string |
||
150 | { |
||
151 | // initialize string to return |
||
152 | $decoded = ''; |
||
153 | // all white-space characters shall be ignored |
||
154 | $data = preg_replace('/[\s]/', '', $data); |
||
155 | // remove start sequence 2-character sequence <~ (3Ch)(7Eh) |
||
156 | if (0 === strpos($data, '<~')) { |
||
157 | // remove EOD and extra data (if any) |
||
158 | $data = substr($data, 2); |
||
159 | } |
||
160 | // check for EOD: 2-character sequence ~> (7Eh)(3Eh) |
||
161 | $eod = strpos($data, '~>'); |
||
162 | if (\strlen($data) - 2 === $eod) { |
||
163 | // remove EOD and extra data (if any) |
||
164 | $data = substr($data, 0, $eod); |
||
165 | } |
||
166 | // data length |
||
167 | $data_length = \strlen($data); |
||
168 | // check for invalid characters |
||
169 | if (preg_match('/[^\x21-\x75,\x74]/', $data) > 0) { |
||
170 | throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
||
171 | } |
||
172 | // z sequence |
||
173 | $zseq = \chr(0).\chr(0).\chr(0).\chr(0); |
||
174 | // position inside a group of 4 bytes (0-3) |
||
175 | $group_pos = 0; |
||
176 | $tuple = 0; |
||
177 | $pow85 = [85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1]; |
||
178 | |||
179 | // for each byte |
||
180 | for ($i = 0; $i < $data_length; ++$i) { |
||
181 | // get char value |
||
182 | $char = \ord($data[$i]); |
||
183 | if (122 == $char) { // 'z' |
||
184 | if (0 == $group_pos) { |
||
185 | $decoded .= $zseq; |
||
186 | } else { |
||
187 | throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
||
188 | } |
||
189 | } else { |
||
190 | // the value represented by a group of 5 characters should never be greater than 2^32 - 1 |
||
191 | $tuple += (($char - 33) * $pow85[$group_pos]); |
||
192 | if (4 == $group_pos) { |
||
193 | // The following if-clauses are an attempt to fix/suppress the following deprecation warning: |
||
194 | // chr(): Providing a value not in-between 0 and 255 is deprecated, this is because a byte value |
||
195 | // must be in the [0, 255] interval. The value used will be constrained using % 256 |
||
196 | // I know this is ugly and there might be more fancier ways. If you know one, feel free to provide a pull request. |
||
197 | if (255 < $tuple >> 8) { |
||
198 | $chr8Part = \chr(($tuple >> 8) % 256); |
||
199 | } else { |
||
200 | $chr8Part = \chr($tuple >> 8); |
||
201 | } |
||
202 | |||
203 | if (255 < $tuple >> 16) { |
||
204 | $chr16Part = \chr(($tuple >> 16) % 256); |
||
205 | } else { |
||
206 | $chr16Part = \chr($tuple >> 16); |
||
207 | } |
||
208 | |||
209 | if (255 < $tuple >> 24) { |
||
210 | $chr24Part = \chr(($tuple >> 24) % 256); |
||
211 | } else { |
||
212 | $chr24Part = \chr($tuple >> 24); |
||
213 | } |
||
214 | |||
215 | if (255 < $tuple) { |
||
216 | $chrTuple = \chr($tuple % 256); |
||
217 | } else { |
||
218 | $chrTuple = \chr($tuple); |
||
219 | } |
||
220 | |||
221 | $decoded .= $chr24Part . $chr16Part . $chr8Part . $chrTuple; |
||
222 | $tuple = 0; |
||
223 | $group_pos = 0; |
||
224 | } else { |
||
225 | ++$group_pos; |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | if ($group_pos > 1) { |
||
230 | $tuple += $pow85[$group_pos - 1]; |
||
231 | } |
||
232 | // last tuple (if any) |
||
233 | switch ($group_pos) { |
||
234 | case 4: |
||
235 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8); |
||
236 | break; |
||
237 | |||
238 | case 3: |
||
239 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16); |
||
240 | break; |
||
241 | |||
242 | case 2: |
||
243 | $decoded .= \chr($tuple >> 24); |
||
244 | break; |
||
245 | |||
246 | case 1: |
||
247 | throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
||
248 | } |
||
249 | |||
250 | return $decoded; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * FlateDecode |
||
255 | * |
||
256 | * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data. |
||
257 | * |
||
258 | * @param string $data Data to decode |
||
259 | * @param int $decodeMemoryLimit Memory limit on deflation |
||
260 | * |
||
261 | * @return string data string |
||
262 | * |
||
263 | * @throws \Exception |
||
264 | */ |
||
265 | protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit): ?string |
||
266 | { |
||
267 | // Uncatchable E_WARNING for "data error" is @ suppressed |
||
268 | // so execution may proceed with an alternate decompression |
||
269 | // method. |
||
270 | $decoded = @gzuncompress($data, $decodeMemoryLimit); |
||
271 | |||
272 | if (false === $decoded) { |
||
273 | // If gzuncompress() failed, try again using the compress.zlib:// |
||
274 | // wrapper to decode it in a file-based context. |
||
275 | // See: https://www.php.net/manual/en/function.gzuncompress.php#79042 |
||
276 | // Issue: https://github.com/smalot/pdfparser/issues/592 |
||
277 | $ztmp = tmpfile(); |
||
278 | if (false != $ztmp) { |
||
279 | fwrite($ztmp, "\x1f\x8b\x08\x00\x00\x00\x00\x00".$data); |
||
280 | $file = stream_get_meta_data($ztmp)['uri']; |
||
281 | if (0 === $decodeMemoryLimit) { |
||
282 | $decoded = file_get_contents('compress.zlib://'.$file); |
||
283 | } else { |
||
284 | $decoded = file_get_contents('compress.zlib://'.$file, false, null, 0, $decodeMemoryLimit); |
||
285 | } |
||
286 | fclose($ztmp); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | if (false === \is_string($decoded) || '' === $decoded) { |
||
291 | // If the decoded string is empty, that means decoding failed. |
||
292 | throw new \Exception('decodeFilterFlateDecode: invalid data'); |
||
293 | } |
||
294 | |||
295 | return $decoded; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * LZWDecode |
||
300 | * |
||
301 | * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
||
302 | * |
||
303 | * @param string $data Data to decode |
||
304 | * |
||
305 | * @return string Data string |
||
306 | */ |
||
307 | protected function decodeFilterLZWDecode(string $data): string |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * RunLengthDecode |
||
384 | * |
||
385 | * Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
||
386 | * |
||
387 | * @param string $data Data to decode |
||
388 | */ |
||
389 | protected function decodeFilterRunLengthDecode(string $data): string |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @return array list of available filters |
||
422 | */ |
||
423 | public function getAvailableFilters(): array |
||
426 | } |
||
427 | } |
||
428 |