| Total Complexity | 48 |
| Total Lines | 324 |
| 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) |
||
| 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 | break; |
||
| 214 | } |
||
| 215 | |||
| 216 | return $decoded; |
||
| 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 | // initialize string to return |
||
| 231 | $decoded = @gzuncompress($data); |
||
| 232 | if (false === $decoded) { |
||
| 233 | throw new Exception('decodeFilterFlateDecode: invalid code'); |
||
| 234 | } |
||
| 235 | |||
| 236 | return $decoded; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * LZWDecode |
||
| 241 | * |
||
| 242 | * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
||
| 243 | * |
||
| 244 | * @param string $data Data to decode |
||
| 245 | * |
||
| 246 | * @return string Data string |
||
| 247 | */ |
||
| 248 | protected function decodeFilterLZWDecode($data) |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * RunLengthDecode |
||
| 325 | * |
||
| 326 | * Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
||
| 327 | * |
||
| 328 | * @param string $data Data to decode |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | protected function decodeFilterRunLengthDecode($data) |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return array list of available filters |
||
| 365 | */ |
||
| 366 | public function getAvailableFilters() |
||
| 369 | } |
||
| 370 | } |
||
| 371 |