| Total Complexity | 50 |
| Total Lines | 344 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 45 | class FilterHelper |
||
| 46 | { |
||
| 47 | protected $availableFilters = ['ASCIIHexDecode', 'ASCII85Decode', 'LZWDecode', 'FlateDecode', 'RunLengthDecode']; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Decode data using the specified filter type. |
||
| 51 | * |
||
| 52 | * @param string $filter Filter name |
||
| 53 | * @param string $data Data to decode |
||
| 54 | * |
||
| 55 | * @return string Decoded data string |
||
| 56 | * |
||
| 57 | * @throws Exception if a certain decode function is not implemented yet |
||
| 58 | */ |
||
| 59 | public function decodeFilter($filter, $data) |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * ASCIIHexDecode |
||
| 94 | * |
||
| 95 | * Decodes data encoded in an ASCII hexadecimal representation, reproducing the original binary data. |
||
| 96 | * |
||
| 97 | * @param string $data Data to decode |
||
| 98 | * |
||
| 99 | * @return string data string |
||
| 100 | */ |
||
| 101 | protected function decodeFilterASCIIHexDecode($data) |
||
| 102 | { |
||
| 103 | // all white-space characters shall be ignored |
||
| 104 | $data = preg_replace('/[\s]/', '', $data); |
||
| 105 | // check for EOD character: GREATER-THAN SIGN (3Eh) |
||
| 106 | $eod = strpos($data, '>'); |
||
| 107 | if (false !== $eod) { |
||
| 108 | // remove EOD and extra data (if any) |
||
| 109 | $data = substr($data, 0, $eod); |
||
| 110 | $eod = true; |
||
| 111 | } |
||
| 112 | // get data length |
||
| 113 | $data_length = \strlen($data); |
||
| 114 | if (0 != ($data_length % 2)) { |
||
| 115 | // odd number of hexadecimal digits |
||
| 116 | if ($eod) { |
||
| 117 | // EOD shall behave as if a 0 (zero) followed the last digit |
||
| 118 | $data = substr($data, 0, -1).'0'.substr($data, -1); |
||
| 119 | } else { |
||
| 120 | throw new Exception('decodeFilterASCIIHexDecode: invalid code'); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | // check for invalid characters |
||
| 124 | if (preg_match('/[^a-fA-F\d]/', $data) > 0) { |
||
| 125 | throw new Exception('decodeFilterASCIIHexDecode: invalid code'); |
||
| 126 | } |
||
| 127 | // get one byte of binary data for each pair of ASCII hexadecimal digits |
||
| 128 | $decoded = pack('H*', $data); |
||
| 129 | |||
| 130 | return $decoded; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * ASCII85Decode |
||
| 135 | * |
||
| 136 | * Decodes data encoded in an ASCII base-85 representation, reproducing the original binary data. |
||
| 137 | * |
||
| 138 | * @param string $data Data to decode |
||
| 139 | * |
||
| 140 | * @return string data string |
||
| 141 | */ |
||
| 142 | protected function decodeFilterASCII85Decode($data) |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * FlateDecode |
||
| 221 | * |
||
| 222 | * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data. |
||
| 223 | * |
||
| 224 | * @param string $data Data to decode |
||
| 225 | * |
||
| 226 | * @return string data string |
||
| 227 | */ |
||
| 228 | protected function decodeFilterFlateDecode($data) |
||
| 229 | { |
||
| 230 | /* |
||
| 231 | * gzuncompress may throw a not catchable E_WARNING in case of an error (like $data is empty) |
||
| 232 | * the following set_error_handler changes an E_WARNING to an E_ERROR, which is catchable. |
||
| 233 | */ |
||
| 234 | set_error_handler(function ($errNo, $errStr) { |
||
| 235 | if (E_WARNING === $errNo) { |
||
| 236 | throw new Exception($errStr); |
||
| 237 | } else { |
||
| 238 | // fallback to default php error handler |
||
| 239 | return false; |
||
| 240 | } |
||
| 241 | }); |
||
| 242 | |||
| 243 | // initialize string to return |
||
| 244 | try { |
||
| 245 | $decoded = gzuncompress($data); |
||
| 246 | if (false === $decoded) { |
||
| 247 | throw new Exception('decodeFilterFlateDecode: invalid code'); |
||
| 248 | } |
||
| 249 | } catch (Exception $e) { |
||
| 250 | throw $e; |
||
| 251 | } finally { |
||
| 252 | // Restore old handler just in case it was customized outside of PDFParser. |
||
| 253 | restore_error_handler(); |
||
| 254 | } |
||
| 255 | |||
| 256 | return $decoded; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * LZWDecode |
||
| 261 | * |
||
| 262 | * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
||
| 263 | * |
||
| 264 | * @param string $data Data to decode |
||
| 265 | * |
||
| 266 | * @return string Data string |
||
| 267 | */ |
||
| 268 | protected function decodeFilterLZWDecode($data) |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * RunLengthDecode |
||
| 345 | * |
||
| 346 | * Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
||
| 347 | * |
||
| 348 | * @param string $data Data to decode |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function decodeFilterRunLengthDecode($data) |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return array list of available filters |
||
| 385 | */ |
||
| 386 | public function getAvailableFilters() |
||
| 391 |