| Total Complexity | 106 |
| Total Lines | 534 |
| Duplicated Lines | 0 % |
| Coverage | 58.75% |
| Changes | 2 | ||
| 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 |
||
| 188 | class HttpProcessUtility |
||
| 189 | { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the appropriate MIME type for the request, throwing if there is none. |
||
| 193 | * |
||
| 194 | * @param string $acceptTypesText Text as it appears in an HTTP |
||
| 195 | * Accepts header. |
||
| 196 | * @param string[] $exactContentTypes Preferred content type to match if an exact media type is given - this is in descending order of preference. |
||
| 197 | * |
||
| 198 | * @param string $inexactContentType Preferred fallback content type for inexact matches. |
||
| 199 | * |
||
| 200 | * @return string One of exactContentType or inexactContentType. |
||
| 201 | */ |
||
| 202 | public static function selectRequiredMimeType($acceptTypesText, |
||
| 203 | $exactContentTypes, |
||
| 204 | $inexactContentType |
||
| 205 | ) { |
||
| 206 | $selectedContentType = null; |
||
| 207 | $selectedMatchingParts = -1; |
||
| 208 | $selectedQualityValue = 0; |
||
| 209 | $acceptable = false; |
||
| 210 | $acceptTypesEmpty = true; |
||
| 211 | $foundExactMatch = false; |
||
| 212 | |||
| 213 | if (!is_null($acceptTypesText)) { |
||
| 214 | $acceptTypes = self::mimeTypesFromAcceptHeaders($acceptTypesText); |
||
| 215 | foreach ($acceptTypes as $acceptType) { |
||
| 216 | $acceptTypesEmpty = false; |
||
| 217 | foreach ($exactContentTypes as $exactContentType) { |
||
| 218 | if (strcasecmp($acceptType->getMimeType(), $exactContentType) == 0) { |
||
| 219 | $selectedContentType = $exactContentType; |
||
| 220 | $selectedQualityValue = $acceptType->getQualityValue(); |
||
| 221 | $acceptable = $selectedQualityValue != 0; |
||
| 222 | $foundExactMatch = true; |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | if ($foundExactMatch) { |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | |||
| 231 | $matchingParts |
||
| 232 | = $acceptType->getMatchingRating($inexactContentType); |
||
| 233 | if ($matchingParts < 0) { |
||
| 234 | continue; |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($matchingParts > $selectedMatchingParts) { |
||
| 238 | // A more specific type wins. |
||
| 239 | $selectedContentType = $inexactContentType; |
||
| 240 | $selectedMatchingParts = $matchingParts; |
||
| 241 | $selectedQualityValue = $acceptType->getQualityValue(); |
||
| 242 | $acceptable = $selectedQualityValue != 0; |
||
| 243 | } else if ($matchingParts == $selectedMatchingParts) { |
||
| 244 | // A type with a higher q-value wins. |
||
| 245 | $candidateQualityValue = $acceptType->getQualityValue(); |
||
| 246 | if ($candidateQualityValue > $selectedQualityValue) { |
||
| 247 | $selectedContentType = $inexactContentType; |
||
| 248 | $selectedQualityValue = $candidateQualityValue; |
||
| 249 | $acceptable = $selectedQualityValue != 0; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | if (!$acceptable && !$acceptTypesEmpty) { |
||
| 256 | throw new HttpHeaderFailure( |
||
| 257 | Messages::unsupportedMediaType(), |
||
| 258 | 415 |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($acceptTypesEmpty) { |
||
| 263 | $selectedContentType = $inexactContentType; |
||
| 264 | } |
||
| 265 | |||
| 266 | return $selectedContentType; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Selects an acceptable MIME type that satisfies the Accepts header. |
||
| 271 | * |
||
| 272 | * @param string $acceptTypesText Text for Accepts header. |
||
| 273 | * @param string[] $availableTypes Types that the server is willing to return, in descending order of preference. |
||
| 274 | * |
||
| 275 | * @return string The best MIME type for the client. |
||
| 276 | * |
||
| 277 | * @throws HttpHeaderFailure |
||
| 278 | */ |
||
| 279 | 160 | public static function selectMimeType($acceptTypesText, array $availableTypes) |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns all MIME types from the $text. |
||
| 336 | * |
||
| 337 | * @param string $text Text as it appears on an HTTP Accepts header. |
||
| 338 | * |
||
| 339 | * @return MediaType[] Array of media (MIME) type description. |
||
| 340 | * |
||
| 341 | * @throws HttpHeaderFailure If found any syntax error in the given text. |
||
| 342 | */ |
||
| 343 | 158 | public static function mimeTypesFromAcceptHeaders($text) |
|
| 344 | { |
||
| 345 | 158 | $mediaTypes = array(); |
|
| 346 | 158 | $textIndex = 0; |
|
| 347 | 158 | while (!self::skipWhitespace($text, $textIndex)) { |
|
| 348 | 158 | $type = null; |
|
| 349 | 158 | $subType = null; |
|
| 350 | 158 | self::readMediaTypeAndSubtype($text, $textIndex, $type, $subType); |
|
| 351 | |||
| 352 | 158 | $parameters = array(); |
|
| 353 | 158 | while (!self::skipWhitespace($text, $textIndex)) { |
|
| 354 | 131 | if ($text[$textIndex] == ',') { |
|
| 355 | 8 | $textIndex++; |
|
| 356 | 8 | break; |
|
| 357 | } |
||
| 358 | |||
| 359 | 131 | if ($text[$textIndex] != ';') { |
|
| 360 | throw new HttpHeaderFailure( |
||
| 361 | Messages::httpProcessUtilityMediaTypeRequiresSemicolonBeforeParameter(), |
||
| 362 | 400 |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | |||
| 366 | 131 | $textIndex++; |
|
| 367 | 131 | if (self::skipWhitespace($text, $textIndex)) { |
|
| 368 | break; |
||
| 369 | } |
||
| 370 | |||
| 371 | 131 | self::readMediaTypeParameter($text, $textIndex, $parameters); |
|
| 372 | } |
||
| 373 | |||
| 374 | 158 | $mediaTypes[] = new MediaType($type, $subType, $parameters); |
|
| 375 | } |
||
| 376 | |||
| 377 | 158 | return $mediaTypes; |
|
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Skips whitespace in the specified text by advancing an index to |
||
| 382 | * the next non-whitespace character. |
||
| 383 | * |
||
| 384 | * @param string $text Text to scan. |
||
| 385 | * @param int &$textIndex Index to begin scanning from. |
||
| 386 | * |
||
| 387 | * @return boolean true if the end of the string was reached, false otherwise. |
||
| 388 | */ |
||
| 389 | 158 | public static function skipWhiteSpace($text, &$textIndex) |
|
| 390 | { |
||
| 391 | 158 | $textLen = strlen($text); |
|
| 392 | 158 | while (($textIndex < $textLen) && Char::isWhiteSpace($text[$textIndex])) { |
|
| 393 | 8 | $textIndex++; |
|
| 394 | } |
||
| 395 | |||
| 396 | 158 | return $textLen == $textIndex; |
|
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Reads the type and subtype specifications for a MIME type. |
||
| 401 | * |
||
| 402 | * @param string $text Text in which specification exists. |
||
| 403 | * @param int &$textIndex Pointer into text. |
||
| 404 | * @param string &$type Type of media found. |
||
| 405 | * @param string &$subType Subtype of media found. |
||
| 406 | * |
||
| 407 | * @throws HttpHeaderFailure If failed to read type and sub-type. |
||
| 408 | * |
||
| 409 | * @return void |
||
| 410 | */ |
||
| 411 | 158 | public static function readMediaTypeAndSubtype($text, &$textIndex, |
|
| 412 | &$type, &$subType |
||
| 413 | ) { |
||
| 414 | 158 | $textStart = $textIndex; |
|
| 415 | 158 | if (self::readToken($text, $textIndex)) { |
|
| 416 | throw new HttpHeaderFailure( |
||
| 417 | Messages::httpProcessUtilityMediaTypeUnspecified(), |
||
| 418 | 400 |
||
| 419 | ); |
||
| 420 | } |
||
| 421 | |||
| 422 | 158 | if ($text[$textIndex] != '/') { |
|
| 423 | throw new HttpHeaderFailure( |
||
| 424 | Messages::httpProcessUtilityMediaTypeRequiresSlash(), |
||
| 425 | 400 |
||
| 426 | ); |
||
| 427 | } |
||
| 428 | |||
| 429 | 158 | $type = substr($text, $textStart, $textIndex - $textStart); |
|
| 430 | 158 | $textIndex++; |
|
| 431 | |||
| 432 | 158 | $subTypeStart = $textIndex; |
|
| 433 | 158 | self::readToken($text, $textIndex); |
|
| 434 | 158 | if ($textIndex == $subTypeStart) { |
|
| 435 | throw new HttpHeaderFailure( |
||
| 436 | Messages::httpProcessUtilityMediaTypeRequiresSubType(), |
||
| 437 | 400 |
||
| 438 | ); |
||
| 439 | } |
||
| 440 | |||
| 441 | 158 | $subType = substr($text, $subTypeStart, $textIndex - $subTypeStart); |
|
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Reads a token on the specified text by advancing an index on it. |
||
| 446 | * |
||
| 447 | * @param string $text Text to read token from. |
||
| 448 | * @param int &$textIndex Index for the position being scanned on text. |
||
| 449 | * |
||
| 450 | * @return boolean true if the end of the text was reached; false otherwise. |
||
| 451 | */ |
||
| 452 | 158 | public static function readToken($text, &$textIndex) |
|
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * To check whether the given character is a HTTP token character |
||
| 464 | * or not. |
||
| 465 | * |
||
| 466 | * @param string $char The character to inspect. |
||
| 467 | * |
||
| 468 | * @return boolean True if the given character is a valid HTTP token |
||
| 469 | * character, False otherwise. |
||
| 470 | */ |
||
| 471 | 158 | public static function isHttpTokenChar($char) |
|
| 472 | { |
||
| 473 | 158 | return ord($char) < 126 && ord($char) > 31 |
|
| 474 | 158 | && !self::isHttpSeparator($char); |
|
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * To check whether the given character is a HTTP seperator character. |
||
| 479 | * |
||
| 480 | * @param string $char The character to inspect. |
||
| 481 | * |
||
| 482 | * @return boolean True if the given character is a valid HTTP seperator |
||
| 483 | * character, False otherwise. |
||
| 484 | */ |
||
| 485 | 158 | public static function isHttpSeparator($char) |
|
| 486 | { |
||
| 487 | 158 | return |
|
| 488 | 158 | $char == '(' || $char == ')' || $char == '<' || $char == '>' || |
|
| 489 | 158 | $char == '@' || $char == ',' || $char == ';' || $char == ':' || |
|
| 490 | 158 | $char == '\\' || $char == '"' || $char == '/' || $char == '[' || |
|
| 491 | 158 | $char == ']' || $char == '?' || $char == '=' || $char == '{' || |
|
| 492 | 158 | $char == '}' || $char == ' ' || ord($char) == Char::TAB; |
|
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Read a parameter for a media type/range. |
||
| 497 | * |
||
| 498 | * @param string $text Text to read from. |
||
| 499 | * @param int &$textIndex Pointer in text. |
||
| 500 | * @param array &$parameters Array with parameters. |
||
| 501 | * |
||
| 502 | * @throws HttpHeaderFailure If found parameter value missing. |
||
| 503 | * @return void |
||
| 504 | */ |
||
| 505 | 131 | public static function readMediaTypeParameter($text, &$textIndex, &$parameters) |
|
| 506 | { |
||
| 507 | 131 | $textStart = $textIndex; |
|
| 508 | 131 | if (self::readToken($text, $textIndex)) { |
|
| 509 | throw new HttpHeaderFailure( |
||
| 510 | Messages::httpProcessUtilityMediaTypeMissingValue(), |
||
| 511 | 400 |
||
| 512 | ); |
||
| 513 | } |
||
| 514 | |||
| 515 | 131 | $parameterName = substr($text, $textStart, $textIndex - $textStart); |
|
| 516 | 131 | if ($text[$textIndex] != '=') { |
|
| 517 | throw new HttpHeaderFailure( |
||
| 518 | Messages::httpProcessUtilityMediaTypeMissingValue(), |
||
| 519 | 400 |
||
| 520 | ); |
||
| 521 | } |
||
| 522 | |||
| 523 | 131 | $textIndex++; |
|
| 524 | 131 | $parameterValue |
|
| 525 | 131 | = self::readQuotedParameterValue($parameterName, $text, $textIndex); |
|
| 526 | 131 | $parameters[] = array($parameterName => $parameterValue); |
|
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Reads Mime type parameter value for a particular parameter in the |
||
| 531 | * Content-Type/Accept headers. |
||
| 532 | * |
||
| 533 | * @param string $parameterName Name of parameter. |
||
| 534 | * @param string $text Header text. |
||
| 535 | * @param int &$textIndex Parsing index in $text. |
||
| 536 | * |
||
| 537 | * @return string String representing the value of the $parameterName parameter. |
||
| 538 | * |
||
| 539 | * @throws HttpHeaderFailure |
||
| 540 | */ |
||
| 541 | 131 | public static function readQuotedParameterValue($parameterName, $text, |
|
| 542 | &$textIndex |
||
| 543 | ) { |
||
| 544 | 131 | $parameterValue = array(); |
|
| 545 | 131 | $textLen = strlen($text); |
|
| 546 | 131 | $valueIsQuoted = false; |
|
| 547 | 131 | if ($textIndex < $textLen) { |
|
| 548 | 131 | if ($text[$textIndex] == '"') { |
|
| 549 | $textIndex++; |
||
| 550 | $valueIsQuoted = true; |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | 131 | while ($textIndex < $textLen) { |
|
| 555 | 131 | $currentChar = $text[$textIndex]; |
|
| 556 | |||
| 557 | 131 | if ($currentChar == '\\' || $currentChar == '"') { |
|
| 558 | if (!$valueIsQuoted) { |
||
| 559 | throw new HttpHeaderFailure( |
||
| 560 | Messages::httpProcessUtilityEscapeCharWithoutQuotes( |
||
| 561 | $parameterName |
||
| 562 | ), |
||
| 563 | 400 |
||
| 564 | ); |
||
| 565 | } |
||
| 566 | |||
| 567 | $textIndex++; |
||
| 568 | |||
| 569 | // End of quoted parameter value. |
||
| 570 | if ($currentChar == '"') { |
||
| 571 | $valueIsQuoted = false; |
||
| 572 | break; |
||
| 573 | } |
||
| 574 | |||
| 575 | if ($textIndex >= $textLen) { |
||
| 576 | throw new HttpHeaderFailure( |
||
| 577 | Messages::httpProcessUtilityEscapeCharAtEnd($parameterName), |
||
| 578 | 400 |
||
| 579 | ); |
||
| 580 | } |
||
| 581 | |||
| 582 | $currentChar = $text[$textIndex]; |
||
| 583 | 131 | } else if (!self::isHttpTokenChar($currentChar)) { |
|
| 584 | // If the given character is special, we stop processing. |
||
| 585 | 14 | break; |
|
| 586 | } |
||
| 587 | |||
| 588 | 131 | $parameterValue[] = $currentChar; |
|
| 589 | 131 | $textIndex++; |
|
| 590 | } |
||
| 591 | |||
| 592 | 131 | if ($valueIsQuoted) { |
|
| 593 | throw new HttpHeaderFailure( |
||
| 594 | Messages::httpProcessUtilityClosingQuoteNotFound($parameterName), |
||
| 595 | 400 |
||
| 596 | ); |
||
| 597 | } |
||
| 598 | |||
| 599 | 131 | return empty($parameterValue) ? null : implode('', $parameterValue); |
|
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Reads the numeric part of a quality value substring, normalizing it to 0-1000 |
||
| 604 | rather than the standard 0.000-1.000 ranges. |
||
| 605 | * |
||
| 606 | * @param string $text Text to read qvalue from. |
||
| 607 | * @param int &$textIndex Index into text where the qvalue starts. |
||
| 608 | * @param int &$qualityValue After the method executes, the normalized qvalue. |
||
| 609 | * @param integer $textIndex |
||
| 610 | * |
||
| 611 | * @throws HttpHeaderFailure If any error occured while reading and processing |
||
| 612 | * the quality factor. |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | 45 | public static function readQualityValue($text, &$textIndex, &$qualityValue) |
|
| 616 | { |
||
| 617 | 45 | $digit = $text[$textIndex++]; |
|
| 618 | 45 | if ($digit == '0') { |
|
| 619 | 8 | $qualityValue = 0; |
|
| 620 | 40 | } else if ($digit == '1') { |
|
| 621 | 40 | $qualityValue = 1; |
|
| 622 | } else { |
||
| 623 | throw new HttpHeaderFailure( |
||
| 624 | Messages::httpProcessUtilityMalformedHeaderValue(), |
||
| 625 | 400 |
||
| 626 | ); |
||
| 627 | } |
||
| 628 | |||
| 629 | 45 | $textLen = strlen($text); |
|
| 630 | 45 | if ($textIndex < $textLen && $text[$textIndex] == '.') { |
|
| 631 | 45 | $textIndex++; |
|
| 632 | |||
| 633 | 45 | $adjustFactor = 1000; |
|
| 634 | 45 | while ($adjustFactor > 1 && $textIndex < $textLen) { |
|
| 635 | 45 | $c = $text[$textIndex]; |
|
| 636 | 45 | $charValue = self::digitToInt32($c); |
|
| 637 | 45 | if ($charValue >= 0) { |
|
| 638 | 45 | $textIndex++; |
|
| 639 | 45 | $adjustFactor /= 10; |
|
| 640 | 45 | $qualityValue *= 10; |
|
| 641 | 45 | $qualityValue += $charValue; |
|
| 642 | } else { |
||
| 643 | break; |
||
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | 45 | $qualityValue = $qualityValue *= $adjustFactor; |
|
| 648 | 45 | if ($qualityValue > 1000) { |
|
| 649 | // Too high of a value in qvalue. |
||
| 650 | 45 | throw new HttpHeaderFailure( |
|
| 651 | 45 | Messages::httpProcessUtilityMalformedHeaderValue(), |
|
| 652 | 45 | 400 |
|
| 653 | 45 | ); |
|
| 654 | } |
||
| 655 | } else { |
||
| 656 | $qualityValue *= 1000; |
||
| 657 | } |
||
| 658 | } |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Converts the specified character from the ASCII range to a digit. |
||
| 662 | * |
||
| 663 | * @param string $c Character to convert |
||
| 664 | * |
||
| 665 | * @return int The Int32 value for $c, or -1 if it is an element separator. |
||
| 666 | * |
||
| 667 | * @throws HttpHeaderFailure If $c is not ASCII value for digit or element |
||
| 668 | * seperator. |
||
| 669 | */ |
||
| 670 | 45 | public static function digitToInt32($c) |
|
| 671 | { |
||
| 672 | 45 | if ($c >= '0' && $c <= '9') { |
|
| 673 | 45 | return intval($c); |
|
| 674 | } else { |
||
| 675 | if (self::isHttpElementSeparator($c)) { |
||
| 676 | return -1; |
||
| 677 | } else { |
||
| 678 | throw new HttpHeaderFailure( |
||
| 679 | Messages::httpProcessUtilityMalformedHeaderValue(), |
||
| 680 | 400 |
||
| 681 | ); |
||
| 682 | } |
||
| 683 | } |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Verifies whether the specified character is a valid separator in |
||
| 688 | an HTTP header list of element. |
||
| 689 | * |
||
| 690 | * @param string $c Character to verify |
||
| 691 | * |
||
| 692 | * @return boolean true if c is a valid character for separating elements; |
||
| 693 | * false otherwise. |
||
| 694 | */ |
||
| 695 | public static function isHttpElementSeparator($c) |
||
| 696 | { |
||
| 697 | return $c == ',' || $c == ' ' || $c == '\t'; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Get server key by header |
||
| 702 | * @param string $headerName Name of header |
||
| 703 | */ |
||
| 704 | 84 | public static function headerToServerKey($headerName) |
|
| 722 | } |
||
| 723 | } |
||
| 724 |