Total Complexity | 55 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | 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-clause is 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 | if ( |
||
197 | 255 < $tuple >> 24 |
||
198 | || 255 < $tuple >> 16 |
||
199 | || 255 < $tuple >> 8 |
||
200 | ) { |
||
201 | $tuple = $tuple % 256; |
||
202 | } |
||
203 | |||
204 | $decoded .= \chr($tuple >> 24) |
||
205 | .\chr($tuple >> 16) |
||
206 | .\chr($tuple >> 8) |
||
207 | .\chr($tuple); |
||
208 | $tuple = 0; |
||
209 | $group_pos = 0; |
||
210 | } else { |
||
211 | ++$group_pos; |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | if ($group_pos > 1) { |
||
216 | $tuple += $pow85[$group_pos - 1]; |
||
217 | } |
||
218 | // last tuple (if any) |
||
219 | switch ($group_pos) { |
||
220 | case 4: |
||
221 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8); |
||
222 | break; |
||
223 | |||
224 | case 3: |
||
225 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16); |
||
226 | break; |
||
227 | |||
228 | case 2: |
||
229 | $decoded .= \chr($tuple >> 24); |
||
230 | break; |
||
231 | |||
232 | case 1: |
||
233 | throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
||
234 | } |
||
235 | |||
236 | return $decoded; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * FlateDecode |
||
241 | * |
||
242 | * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data. |
||
243 | * |
||
244 | * @param string $data Data to decode |
||
245 | * @param int $decodeMemoryLimit Memory limit on deflation |
||
246 | * |
||
247 | * @return string data string |
||
248 | * |
||
249 | * @throws \Exception |
||
250 | */ |
||
251 | protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit): ?string |
||
252 | { |
||
253 | // Uncatchable E_WARNING for "data error" is @ suppressed |
||
254 | // so execution may proceed with an alternate decompression |
||
255 | // method. |
||
256 | $decoded = @gzuncompress($data, $decodeMemoryLimit); |
||
257 | |||
258 | if (false === $decoded) { |
||
259 | // If gzuncompress() failed, try again using the compress.zlib:// |
||
260 | // wrapper to decode it in a file-based context. |
||
261 | // See: https://www.php.net/manual/en/function.gzuncompress.php#79042 |
||
262 | // Issue: https://github.com/smalot/pdfparser/issues/592 |
||
263 | $ztmp = tmpfile(); |
||
264 | if (false != $ztmp) { |
||
265 | fwrite($ztmp, "\x1f\x8b\x08\x00\x00\x00\x00\x00".$data); |
||
266 | $file = stream_get_meta_data($ztmp)['uri']; |
||
267 | if (0 === $decodeMemoryLimit) { |
||
268 | $decoded = file_get_contents('compress.zlib://'.$file); |
||
269 | } else { |
||
270 | $decoded = file_get_contents('compress.zlib://'.$file, false, null, 0, $decodeMemoryLimit); |
||
271 | } |
||
272 | fclose($ztmp); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | if (false === \is_string($decoded) || '' === $decoded) { |
||
277 | // If the decoded string is empty, that means decoding failed. |
||
278 | throw new \Exception('decodeFilterFlateDecode: invalid data'); |
||
279 | } |
||
280 | |||
281 | return $decoded; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * LZWDecode |
||
286 | * |
||
287 | * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
||
288 | * |
||
289 | * @param string $data Data to decode |
||
290 | * |
||
291 | * @return string Data string |
||
292 | */ |
||
293 | protected function decodeFilterLZWDecode(string $data): string |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * RunLengthDecode |
||
370 | * |
||
371 | * Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
||
372 | * |
||
373 | * @param string $data Data to decode |
||
374 | */ |
||
375 | protected function decodeFilterRunLengthDecode(string $data): string |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @return array list of available filters |
||
408 | */ |
||
409 | public function getAvailableFilters(): array |
||
414 |