| Total Complexity | 50 |
| Total Lines | 343 |
| 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) |
||
| 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) |
||
| 143 | { |
||
| 144 | // initialize string to return |
||
| 145 | $decoded = ''; |
||
| 146 | // all white-space characters shall be ignored |
||
| 147 | $data = preg_replace('/[\s]/', '', $data); |
||
| 148 | // remove start sequence 2-character sequence <~ (3Ch)(7Eh) |
||
| 149 | if (false !== strpos($data, '<~')) { |
||
| 150 | // remove EOD and extra data (if any) |
||
| 151 | $data = substr($data, 2); |
||
| 152 | } |
||
| 153 | // check for EOD: 2-character sequence ~> (7Eh)(3Eh) |
||
| 154 | $eod = strpos($data, '~>'); |
||
| 155 | if (false !== $eod) { |
||
| 156 | // remove EOD and extra data (if any) |
||
| 157 | $data = substr($data, 0, $eod); |
||
| 158 | } |
||
| 159 | // data length |
||
| 160 | $data_length = \strlen($data); |
||
| 161 | // check for invalid characters |
||
| 162 | if (preg_match('/[^\x21-\x75,\x74]/', $data) > 0) { |
||
| 163 | throw new Exception('decodeFilterASCII85Decode: invalid code'); |
||
| 164 | } |
||
| 165 | // z sequence |
||
| 166 | $zseq = \chr(0).\chr(0).\chr(0).\chr(0); |
||
| 167 | // position inside a group of 4 bytes (0-3) |
||
| 168 | $group_pos = 0; |
||
| 169 | $tuple = 0; |
||
| 170 | $pow85 = [(85 * 85 * 85 * 85), (85 * 85 * 85), (85 * 85), 85, 1]; |
||
| 171 | |||
| 172 | // for each byte |
||
| 173 | for ($i = 0; $i < $data_length; ++$i) { |
||
| 174 | // get char value |
||
| 175 | $char = \ord($data[$i]); |
||
| 176 | if (122 == $char) { // 'z' |
||
| 177 | if (0 == $group_pos) { |
||
| 178 | $decoded .= $zseq; |
||
| 179 | } else { |
||
| 180 | throw new Exception('decodeFilterASCII85Decode: invalid code'); |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | // the value represented by a group of 5 characters should never be greater than 2^32 - 1 |
||
| 184 | $tuple += (($char - 33) * $pow85[$group_pos]); |
||
| 185 | if (4 == $group_pos) { |
||
| 186 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8).\chr($tuple); |
||
| 187 | $tuple = 0; |
||
| 188 | $group_pos = 0; |
||
| 189 | } else { |
||
| 190 | ++$group_pos; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | if ($group_pos > 1) { |
||
| 195 | $tuple += $pow85[($group_pos - 1)]; |
||
| 196 | } |
||
| 197 | // last tuple (if any) |
||
| 198 | switch ($group_pos) { |
||
| 199 | case 4: |
||
| 200 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8); |
||
| 201 | break; |
||
| 202 | |||
| 203 | case 3: |
||
| 204 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16); |
||
| 205 | break; |
||
| 206 | |||
| 207 | case 2: |
||
| 208 | $decoded .= \chr($tuple >> 24); |
||
| 209 | break; |
||
| 210 | |||
| 211 | case 1: |
||
| 212 | throw new Exception('decodeFilterASCII85Decode: invalid code'); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $decoded; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * FlateDecode |
||
| 220 | * |
||
| 221 | * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data. |
||
| 222 | * |
||
| 223 | * @param string $data Data to decode |
||
| 224 | * |
||
| 225 | * @return string data string |
||
| 226 | */ |
||
| 227 | protected function decodeFilterFlateDecode($data) |
||
| 228 | { |
||
| 229 | /* |
||
| 230 | * gzuncompress may throw a not catchable E_WARNING in case of an error (like $data is empty) |
||
| 231 | * the following set_error_handler changes an E_WARNING to an E_ERROR, which is catchable. |
||
| 232 | */ |
||
| 233 | set_error_handler(function ($errNo, $errStr) { |
||
| 234 | if (E_WARNING === $errNo) { |
||
| 235 | throw new Exception($errStr); |
||
| 236 | } else { |
||
| 237 | // fallback to default php error handler |
||
| 238 | return false; |
||
| 239 | } |
||
| 240 | }); |
||
| 241 | |||
| 242 | // initialize string to return |
||
| 243 | try { |
||
| 244 | $decoded = gzuncompress($data); |
||
| 245 | if (false === $decoded) { |
||
| 246 | throw new Exception('decodeFilterFlateDecode: invalid code'); |
||
| 247 | } |
||
| 248 | } catch (Exception $e) { |
||
| 249 | throw $e; |
||
| 250 | } finally { |
||
| 251 | // Restore old handler just in case it was customized outside of PDFParser. |
||
| 252 | restore_error_handler(); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $decoded; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * LZWDecode |
||
| 260 | * |
||
| 261 | * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
||
| 262 | * |
||
| 263 | * @param string $data Data to decode |
||
| 264 | * |
||
| 265 | * @return string Data string |
||
| 266 | */ |
||
| 267 | protected function decodeFilterLZWDecode($data) |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * RunLengthDecode |
||
| 344 | * |
||
| 345 | * Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
||
| 346 | * |
||
| 347 | * @param string $data Data to decode |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | protected function decodeFilterRunLengthDecode($data) |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return array list of available filters |
||
| 384 | */ |
||
| 385 | public function getAvailableFilters() |
||
| 390 |