| Total Complexity | 70 | 
| Total Lines | 478 | 
| Duplicated Lines | 0 % | 
| Changes | 23 | ||
| Bugs | 1 | Features | 0 | 
Complex classes like HttpProcessUtility 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 HttpProcessUtility, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 14 | class HttpProcessUtility  | 
            ||
| 15 | { | 
            ||
| 16 | /**  | 
            ||
| 17 | * Gets the appropriate MIME type for the request, throwing if there is none.  | 
            ||
| 18 | *  | 
            ||
| 19 | * @param string|null $acceptTypesText Text as it appears in an HTTP Accepts header  | 
            ||
| 20 | * @param string[] $exactContentTypes Preferred content type to match if an exact media type is given - in  | 
            ||
| 21 | * descending order of preference  | 
            ||
| 22 | * @param string|null $inexactContentType Preferred fallback content type for inexact matches  | 
            ||
| 23 | *  | 
            ||
| 24 | * @throws HttpHeaderFailure  | 
            ||
| 25 | * @return string|null One of exactContentType or inexactContentType  | 
            ||
| 26 | */  | 
            ||
| 27 | public static function selectRequiredMimeType(  | 
            ||
| 28 | ?string $acceptTypesText,  | 
            ||
| 29 | array $exactContentTypes,  | 
            ||
| 30 | ?string $inexactContentType  | 
            ||
| 31 |     ): ?string { | 
            ||
| 32 |         if (null === $acceptTypesText) { | 
            ||
| 33 | throw new HttpHeaderFailure(Messages::unsupportedMediaType(), 415);  | 
            ||
| 34 | }  | 
            ||
| 35 | // fetch types client will accept  | 
            ||
| 36 | $acceptTypes = self::mimeTypesFromAcceptHeaders($acceptTypesText);  | 
            ||
| 37 |         if (empty($acceptTypes)) { | 
            ||
| 38 | return $inexactContentType;  | 
            ||
| 39 | }  | 
            ||
| 40 | // filter out transmitted types which have a zero quality  | 
            ||
| 41 |         $acceptTypes = array_filter($acceptTypes, function (MediaType $acceptType) { | 
            ||
| 42 | return 0 !== $acceptType->getQualityValue();  | 
            ||
| 43 | });  | 
            ||
| 44 | // reduce acceptable types down to any that matches are exact set.  | 
            ||
| 45 |         $exactMatch = array_uintersect($acceptTypes, $exactContentTypes, function (MediaType $acceptType, $exactType) { | 
            ||
| 46 | return strcasecmp($acceptType->getMimeType(), $exactType);  | 
            ||
| 47 | });  | 
            ||
| 48 | // check if an exact match was found  | 
            ||
| 49 |         if (0 !== count($exactMatch)) { | 
            ||
| 50 | return $exactMatch[0]->getMimeType();  | 
            ||
| 51 | }  | 
            ||
| 52 | // filter down all remaining accept types to any that are a partial match for inexact  | 
            ||
| 53 |         $acceptTypes = array_filter($acceptTypes, function (MediaType $acceptType) use ($inexactContentType) { | 
            ||
| 54 | return 0 < $acceptType->getMatchingRating($inexactContentType);  | 
            ||
| 55 | });  | 
            ||
| 56 | // if no inexact type go boom.  | 
            ||
| 57 |         if (count($acceptTypes) === 0) { | 
            ||
| 58 | throw new HttpHeaderFailure(Messages::unsupportedMediaType(), 415);  | 
            ||
| 59 | }  | 
            ||
| 60 | return $inexactContentType;  | 
            ||
| 61 | }  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Returns all MIME types from the $text.  | 
            ||
| 65 | *  | 
            ||
| 66 | * @param string $text Text as it appears on an HTTP Accepts header  | 
            ||
| 67 | *  | 
            ||
| 68 | * @throws HttpHeaderFailure If found any syntax error in the given text  | 
            ||
| 69 | * @return MediaType[] Array of media (MIME) type description  | 
            ||
| 70 | */  | 
            ||
| 71 | public static function mimeTypesFromAcceptHeaders(string $text): array  | 
            ||
| 72 |     { | 
            ||
| 73 | $mediaTypes = [];  | 
            ||
| 74 | $textIndex = 0;  | 
            ||
| 75 |         while (!self::skipWhiteSpace($text, $textIndex)) { | 
            ||
| 76 | $type = null;  | 
            ||
| 77 | $subType = null;  | 
            ||
| 78 | self::readMediaTypeAndSubtype($text, $textIndex, $type, $subType);  | 
            ||
| 79 | |||
| 80 | $parameters = [];  | 
            ||
| 81 |             while (!self::skipWhitespace($text, $textIndex)) { | 
            ||
| 82 |                 if (',' == $text[$textIndex]) { | 
            ||
| 83 | ++$textIndex;  | 
            ||
| 84 | break;  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 |                 if (';' != $text[$textIndex]) { | 
            ||
| 88 | throw new HttpHeaderFailure(  | 
            ||
| 89 | Messages::httpProcessUtilityMediaTypeRequiresSemicolonBeforeParameter(),  | 
            ||
| 90 | 400  | 
            ||
| 91 | );  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | ++$textIndex;  | 
            ||
| 95 |                 if (self::skipWhiteSpace($text, $textIndex)) { | 
            ||
| 96 | break;  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | self::readMediaTypeParameter($text, $textIndex, $parameters);  | 
            ||
| 100 | }  | 
            ||
| 101 | |||
| 102 | $mediaTypes[] = new MediaType($type, $subType, $parameters);  | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | return $mediaTypes;  | 
            ||
| 106 | }  | 
            ||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * Skips whitespace in the specified text by advancing an index to  | 
            ||
| 110 | * the next non-whitespace character.  | 
            ||
| 111 | *  | 
            ||
| 112 | * @param string $text Text to scan  | 
            ||
| 113 | * @param int &$textIndex Index to begin scanning from  | 
            ||
| 114 | *  | 
            ||
| 115 | * @return bool true if the end of the string was reached, false otherwise  | 
            ||
| 116 | */  | 
            ||
| 117 | public static function skipWhiteSpace(string $text, int &$textIndex): bool  | 
            ||
| 118 |     { | 
            ||
| 119 | $textLen = strlen(strval($text));  | 
            ||
| 120 |         while (($textIndex < $textLen) && Char::isWhiteSpace($text[$textIndex])) { | 
            ||
| 121 | ++$textIndex;  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | return $textLen == $textIndex;  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * Reads the type and subtype specifications for a MIME type.  | 
            ||
| 129 | *  | 
            ||
| 130 | * @param string $text Text in which specification exists  | 
            ||
| 131 | * @param int &$textIndex Pointer into text  | 
            ||
| 132 | * @param string &$type Type of media found  | 
            ||
| 133 | * @param string &$subType Subtype of media found  | 
            ||
| 134 | *  | 
            ||
| 135 | * @throws HttpHeaderFailure If failed to read type and sub-type  | 
            ||
| 136 | */  | 
            ||
| 137 | public static function readMediaTypeAndSubtype(  | 
            ||
| 138 | string $text,  | 
            ||
| 139 | int &$textIndex,  | 
            ||
| 140 | &$type,  | 
            ||
| 141 | &$subType  | 
            ||
| 142 |     ): void { | 
            ||
| 143 | $textStart = $textIndex;  | 
            ||
| 144 |         if (self::readToken($text, $textIndex)) { | 
            ||
| 145 | throw new HttpHeaderFailure(  | 
            ||
| 146 | Messages::httpProcessUtilityMediaTypeUnspecified(),  | 
            ||
| 147 | 400  | 
            ||
| 148 | );  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 |         if ('/' != $text[$textIndex]) { | 
            ||
| 152 | throw new HttpHeaderFailure(  | 
            ||
| 153 | Messages::httpProcessUtilityMediaTypeRequiresSlash(),  | 
            ||
| 154 | 400  | 
            ||
| 155 | );  | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | $type = substr($text, $textStart, $textIndex - $textStart);  | 
            ||
| 159 | ++$textIndex;  | 
            ||
| 160 | |||
| 161 | $subTypeStart = $textIndex;  | 
            ||
| 162 | self::readToken($text, $textIndex);  | 
            ||
| 163 |         if ($textIndex == $subTypeStart) { | 
            ||
| 164 | throw new HttpHeaderFailure(  | 
            ||
| 165 | Messages::httpProcessUtilityMediaTypeRequiresSubType(),  | 
            ||
| 166 | 400  | 
            ||
| 167 | );  | 
            ||
| 168 | }  | 
            ||
| 169 | |||
| 170 | $subType = substr($text, $subTypeStart, $textIndex - $subTypeStart);  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * Reads a token on the specified text by advancing an index on it.  | 
            ||
| 175 | *  | 
            ||
| 176 | * @param string $text Text to read token from  | 
            ||
| 177 | * @param int &$textIndex Index for the position being scanned on text  | 
            ||
| 178 | *  | 
            ||
| 179 | * @return bool true if the end of the text was reached; false otherwise  | 
            ||
| 180 | */  | 
            ||
| 181 | public static function readToken(string $text, int &$textIndex): bool  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | /**  | 
            ||
| 192 | * To check whether the given character is a HTTP token character  | 
            ||
| 193 | * or not.  | 
            ||
| 194 | *  | 
            ||
| 195 | * @param string $char The character to inspect  | 
            ||
| 196 | *  | 
            ||
| 197 | * @return bool True if the given character is a valid HTTP token  | 
            ||
| 198 | * character, False otherwise  | 
            ||
| 199 | */  | 
            ||
| 200 | public static function isHttpTokenChar(string $char): bool  | 
            ||
| 201 |     { | 
            ||
| 202 | return 126 > ord($char) && 31 < ord($char) && !self::isHttpSeparator($char);  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | /**  | 
            ||
| 206 | * To check whether the given character is a HTTP separator character.  | 
            ||
| 207 | *  | 
            ||
| 208 | * @param string $char The character to inspect  | 
            ||
| 209 | *  | 
            ||
| 210 | * @return bool True if the given character is a valid HTTP separator  | 
            ||
| 211 | * character, False otherwise  | 
            ||
| 212 | */  | 
            ||
| 213 | public static function isHttpSeparator(string $char): bool  | 
            ||
| 214 |     { | 
            ||
| 215 |         $httpSeperators = ['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ']; | 
            ||
| 216 | return in_array($char, $httpSeperators) || ord($char) == Char::TAB;  | 
            ||
| 217 | }  | 
            ||
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * Read a parameter for a media type/range.  | 
            ||
| 221 | *  | 
            ||
| 222 | * @param string $text Text to read from  | 
            ||
| 223 | * @param int &$textIndex Pointer in text  | 
            ||
| 224 | * @param array &$parameters Array with parameters  | 
            ||
| 225 | *  | 
            ||
| 226 | * @throws HttpHeaderFailure If found parameter value missing  | 
            ||
| 227 | */  | 
            ||
| 228 | public static function readMediaTypeParameter(string $text, int &$textIndex, array &$parameters)  | 
            ||
| 249 | }  | 
            ||
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * Reads Mime type parameter value for a particular parameter in the  | 
            ||
| 253 | * Content-Type/Accept headers.  | 
            ||
| 254 | *  | 
            ||
| 255 | * @param string $parameterName Name of parameter  | 
            ||
| 256 | * @param string $text Header text  | 
            ||
| 257 | * @param int &$textIndex Parsing index in $text  | 
            ||
| 258 | *  | 
            ||
| 259 | * @throws HttpHeaderFailure  | 
            ||
| 260 | * @return string String representing the value of the $parameterName parameter  | 
            ||
| 261 | */  | 
            ||
| 262 | public static function readQuotedParameterValue(  | 
            ||
| 263 | string $parameterName,  | 
            ||
| 264 | string $text,  | 
            ||
| 265 | int &$textIndex  | 
            ||
| 266 |     ): ?string { | 
            ||
| 267 | $parameterValue = [];  | 
            ||
| 268 | $textLen = strlen($text);  | 
            ||
| 269 | $valueIsQuoted = false;  | 
            ||
| 270 | if ($textIndex < $textLen &&  | 
            ||
| 271 |             '"' == $text[$textIndex]) { | 
            ||
| 272 | ++$textIndex;  | 
            ||
| 273 | $valueIsQuoted = true;  | 
            ||
| 274 | }  | 
            ||
| 275 | |||
| 276 |         while ($textIndex < $textLen) { | 
            ||
| 277 | $currentChar = $text[$textIndex];  | 
            ||
| 278 | |||
| 279 |             if ('\\' == $currentChar || '"' == $currentChar) { | 
            ||
| 280 |                 if (!$valueIsQuoted) { | 
            ||
| 281 | throw new HttpHeaderFailure(  | 
            ||
| 282 | Messages::httpProcessUtilityEscapeCharWithoutQuotes(  | 
            ||
| 283 | $parameterName  | 
            ||
| 284 | ),  | 
            ||
| 285 | 400  | 
            ||
| 286 | );  | 
            ||
| 287 | }  | 
            ||
| 288 | |||
| 289 | ++$textIndex;  | 
            ||
| 290 | |||
| 291 | // End of quoted parameter value.  | 
            ||
| 292 |                 if ('"' == $currentChar) { | 
            ||
| 293 | $valueIsQuoted = false;  | 
            ||
| 294 | break;  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 |                 if ($textIndex >= $textLen) { | 
            ||
| 298 | throw new HttpHeaderFailure(  | 
            ||
| 299 | Messages::httpProcessUtilityEscapeCharAtEnd($parameterName),  | 
            ||
| 300 | 400  | 
            ||
| 301 | );  | 
            ||
| 302 | }  | 
            ||
| 303 | |||
| 304 | $currentChar = $text[$textIndex];  | 
            ||
| 305 |             } elseif (!self::isHttpTokenChar($currentChar)) { | 
            ||
| 306 | // If the given character is special, we stop processing.  | 
            ||
| 307 | break;  | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 310 | $parameterValue[] = $currentChar;  | 
            ||
| 311 | ++$textIndex;  | 
            ||
| 312 | }  | 
            ||
| 313 | |||
| 314 |         if ($valueIsQuoted) { | 
            ||
| 315 | throw new HttpHeaderFailure(  | 
            ||
| 316 | Messages::httpProcessUtilityClosingQuoteNotFound($parameterName),  | 
            ||
| 317 | 400  | 
            ||
| 318 | );  | 
            ||
| 319 | }  | 
            ||
| 320 | |||
| 321 |         return empty($parameterValue) ? null : implode('', $parameterValue); | 
            ||
| 322 | }  | 
            ||
| 323 | |||
| 324 | /**  | 
            ||
| 325 | * Selects an acceptable MIME type that satisfies the Accepts header.  | 
            ||
| 326 | *  | 
            ||
| 327 | * @param string $acceptTypesText Text for Accepts header  | 
            ||
| 328 | * @param string[] $availableTypes Types that the server is willing to return, in descending order of preference  | 
            ||
| 329 | *  | 
            ||
| 330 | * @throws HttpHeaderFailure  | 
            ||
| 331 | * @return string|null The best MIME type for the client  | 
            ||
| 332 | */  | 
            ||
| 333 | public static function selectMimeType(string $acceptTypesText, array $availableTypes): ?string  | 
            ||
| 386 | }  | 
            ||
| 387 | |||
| 388 | /**  | 
            ||
| 389 | * Reads the numeric part of a quality value substring, normalizing it to 0-1000.  | 
            ||
| 390 | * rather than the standard 0.000-1.000 ranges.  | 
            ||
| 391 | *  | 
            ||
| 392 | * @param string $text Text to read qvalue from  | 
            ||
| 393 | * @param int &$textIndex Index into text where the qvalue starts  | 
            ||
| 394 | *  | 
            ||
| 395 | * @throws HttpHeaderFailure If any error occurred while reading and processing the quality factor  | 
            ||
| 396 | * @return int The normalised qvalue  | 
            ||
| 397 | */  | 
            ||
| 398 | public static function readQualityValue(string $text, int &$textIndex): int  | 
            ||
| 399 |     { | 
            ||
| 400 | $digit = $text[$textIndex++];  | 
            ||
| 401 |         if ('0' == $digit) { | 
            ||
| 402 | $qualityValue = 0;  | 
            ||
| 403 |         } elseif ('1' == $digit) { | 
            ||
| 404 | $qualityValue = 1;  | 
            ||
| 405 |         } else { | 
            ||
| 406 | throw new HttpHeaderFailure(  | 
            ||
| 407 | Messages::httpProcessUtilityMalformedHeaderValue(),  | 
            ||
| 408 | 400  | 
            ||
| 409 | );  | 
            ||
| 410 | }  | 
            ||
| 411 | |||
| 412 | $textLen = strlen($text);  | 
            ||
| 413 |         if ($textIndex >= $textLen || '.' != $text[$textIndex]) { | 
            ||
| 414 | return $qualityValue * 1000;  | 
            ||
| 415 | }  | 
            ||
| 416 | |||
| 417 | ++$textIndex;  | 
            ||
| 418 | |||
| 419 | $adjustFactor = 1000;  | 
            ||
| 420 |         while (1 < $adjustFactor && $textIndex < $textLen) { | 
            ||
| 421 | $c = $text[$textIndex];  | 
            ||
| 422 | $charValue = self::digitToInt32($c);  | 
            ||
| 423 |             if (0 <= $charValue) { | 
            ||
| 424 | ++$textIndex;  | 
            ||
| 425 | $adjustFactor /= 10;  | 
            ||
| 426 | $qualityValue *= 10;  | 
            ||
| 427 | $qualityValue += $charValue;  | 
            ||
| 428 |             } else { | 
            ||
| 429 | break;  | 
            ||
| 430 | }  | 
            ||
| 431 | }  | 
            ||
| 432 | |||
| 433 | $qualityValue *= $adjustFactor;  | 
            ||
| 434 |         if ($qualityValue > 1000) { | 
            ||
| 435 | // Too high of a value in qvalue.  | 
            ||
| 436 | throw new HttpHeaderFailure(  | 
            ||
| 437 | Messages::httpProcessUtilityMalformedHeaderValue(),  | 
            ||
| 438 | 400  | 
            ||
| 439 | );  | 
            ||
| 440 | }  | 
            ||
| 441 | |||
| 442 | return $qualityValue;  | 
            ||
| 443 | }  | 
            ||
| 444 | |||
| 445 | /**  | 
            ||
| 446 | * Converts the specified character from the ASCII range to a digit.  | 
            ||
| 447 | *  | 
            ||
| 448 | * @param string $c Character to convert  | 
            ||
| 449 | *  | 
            ||
| 450 | * @throws HttpHeaderFailure If $c is not ASCII value for digit or element separator  | 
            ||
| 451 | * @return int The Int32 value for $c, or -1 if it is an element separator  | 
            ||
| 452 | */  | 
            ||
| 453 | public static function digitToInt32(string $c): int  | 
            ||
| 454 |     { | 
            ||
| 455 |         if ('0' <= $c && '9' >= $c) { | 
            ||
| 456 | return intval($c);  | 
            ||
| 457 | }  | 
            ||
| 458 |         if (self::isHttpElementSeparator($c)) { | 
            ||
| 459 | return -1;  | 
            ||
| 460 | }  | 
            ||
| 461 | throw new HttpHeaderFailure(  | 
            ||
| 462 | Messages::httpProcessUtilityMalformedHeaderValue(),  | 
            ||
| 463 | 400  | 
            ||
| 464 | );  | 
            ||
| 465 | }  | 
            ||
| 466 | |||
| 467 | /**  | 
            ||
| 468 | * Verifies whether the specified character is a valid separator in.  | 
            ||
| 469 | * an HTTP header list of element.  | 
            ||
| 470 | *  | 
            ||
| 471 | * @param string $c Character to verify  | 
            ||
| 472 | *  | 
            ||
| 473 | * @return bool true if c is a valid character for separating elements; false otherwise  | 
            ||
| 474 | */  | 
            ||
| 475 | public static function isHttpElementSeparator(string $c): bool  | 
            ||
| 476 |     { | 
            ||
| 477 | return ',' == $c || ' ' == $c || '\t' == $c;  | 
            ||
| 478 | }  | 
            ||
| 479 | |||
| 480 | /**  | 
            ||
| 481 | * Get server key by header.  | 
            ||
| 482 | *  | 
            ||
| 483 | * @param string $headerName Name of header  | 
            ||
| 484 | * @return string  | 
            ||
| 485 | */  | 
            ||
| 486 | public static function headerToServerKey(string $headerName): string  | 
            ||
| 492 | }  | 
            ||
| 493 | }  | 
            ||
| 494 | 
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