| Total Complexity | 48 |
| Total Lines | 331 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 Decoded data string |
||
|
|
|||
| 56 | * |
||
| 57 | * @throws Exception if a certain decode function is not implemented yet. |
||
| 58 | */ |
||
| 59 | public function decodeFilter($filter, $data) |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * ASCIIHexDecode |
||
| 99 | * |
||
| 100 | * Decodes data encoded in an ASCII hexadecimal representation, reproducing the original binary data. |
||
| 101 | * |
||
| 102 | * @param string $data Data to decode |
||
| 103 | * |
||
| 104 | * @return string data string |
||
| 105 | */ |
||
| 106 | public function decodeFilterASCIIHexDecode($data) |
||
| 107 | { |
||
| 108 | // initialize string to return |
||
| 109 | $decoded = ''; |
||
| 110 | // all white-space characters shall be ignored |
||
| 111 | $data = preg_replace('/[\s]/', '', $data); |
||
| 112 | // check for EOD character: GREATER-THAN SIGN (3Eh) |
||
| 113 | $eod = strpos($data, '>'); |
||
| 114 | if (false !== $eod) { |
||
| 115 | // remove EOD and extra data (if any) |
||
| 116 | $data = substr($data, 0, $eod); |
||
| 117 | $eod = true; |
||
| 118 | } |
||
| 119 | // get data length |
||
| 120 | $data_length = \strlen($data); |
||
| 121 | if (0 != ($data_length % 2)) { |
||
| 122 | // odd number of hexadecimal digits |
||
| 123 | if ($eod) { |
||
| 124 | // EOD shall behave as if a 0 (zero) followed the last digit |
||
| 125 | $data = substr($data, 0, -1).'0'.substr($data, -1); |
||
| 126 | } else { |
||
| 127 | throw new Exception('decodeFilterASCIIHexDecode: invalid code'); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | // check for invalid characters |
||
| 131 | if (preg_match('/[^a-fA-F\d]/', $data) > 0) { |
||
| 132 | throw new Exception('decodeFilterASCIIHexDecode: invalid code'); |
||
| 133 | } |
||
| 134 | // get one byte of binary data for each pair of ASCII hexadecimal digits |
||
| 135 | $decoded = pack('H*', $data); |
||
| 136 | |||
| 137 | return $decoded; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * ASCII85Decode |
||
| 142 | * |
||
| 143 | * Decodes data encoded in an ASCII base-85 representation, reproducing the original binary data. |
||
| 144 | * |
||
| 145 | * @param string $data Data to decode |
||
| 146 | * |
||
| 147 | * @return string data string |
||
| 148 | */ |
||
| 149 | public function decodeFilterASCII85Decode($data) |
||
| 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 (false !== 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 (false !== $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 | $last_pos = ($data_length - 1); |
||
| 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 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8).\chr($tuple); |
||
| 194 | $tuple = 0; |
||
| 195 | $group_pos = 0; |
||
| 196 | } else { |
||
| 197 | ++$group_pos; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | if ($group_pos > 1) { |
||
| 202 | $tuple += $pow85[($group_pos - 1)]; |
||
| 203 | } |
||
| 204 | // last tuple (if any) |
||
| 205 | switch ($group_pos) { |
||
| 206 | case 4: |
||
| 207 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8); |
||
| 208 | break; |
||
| 209 | |||
| 210 | case 3: |
||
| 211 | $decoded .= \chr($tuple >> 24).\chr($tuple >> 16); |
||
| 212 | break; |
||
| 213 | |||
| 214 | case 2: |
||
| 215 | $decoded .= \chr($tuple >> 24); |
||
| 216 | break; |
||
| 217 | |||
| 218 | case 1: |
||
| 219 | throw new Exception('decodeFilterASCII85Decode: invalid code'); |
||
| 220 | break; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $decoded; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * FlateDecode |
||
| 228 | * |
||
| 229 | * Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data. |
||
| 230 | * |
||
| 231 | * @param string $data Data to decode |
||
| 232 | * |
||
| 233 | * @return string data string |
||
| 234 | */ |
||
| 235 | public function decodeFilterFlateDecode($data) |
||
| 236 | { |
||
| 237 | // initialize string to return |
||
| 238 | $decoded = @gzuncompress($data); |
||
| 239 | if (false === $decoded) { |
||
| 240 | throw new Exception('decodeFilterFlateDecode: invalid code'); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $decoded; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * LZWDecode |
||
| 248 | * |
||
| 249 | * Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
||
| 250 | * |
||
| 251 | * @param string $data Data to decode |
||
| 252 | * |
||
| 253 | * @return string Data string |
||
| 254 | */ |
||
| 255 | public function decodeFilterLZWDecode($data) |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * RunLengthDecode |
||
| 332 | * |
||
| 333 | * Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
||
| 334 | * |
||
| 335 | * @param string $data Data to decode |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function decodeFilterRunLengthDecode($data) |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return array list of available filters |
||
| 372 | */ |
||
| 373 | public function getAvailableFilters() |
||
| 376 | } |
||
| 377 | } |
||
| 378 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths