Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RequestSpec 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 RequestSpec, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class RequestSpec extends ObjectBehavior |
||
| 16 | { |
||
| 17 | function it_is_initializable() |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Retrieves the HTTP protocol version as a string. |
||
| 24 | */ |
||
| 25 | function it_returns_its_protocol_version_as_a_string() |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
||
| 33 | */ |
||
| 34 | function it_returns_a_string_containing_only_the_http_version() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Return an instance with the specified HTTP protocol version. |
||
| 41 | * |
||
| 42 | * The version string MUST contain only the HTTP version number (e.g., |
||
| 43 | * "1.1", "1.0"). |
||
| 44 | * |
||
| 45 | * This method MUST be implemented in such a way as to retain the |
||
| 46 | * immutability of the message, and MUST return an instance that has the |
||
| 47 | * new protocol version. |
||
| 48 | */ |
||
| 49 | function it_returns_a_new_request_with_a_protocol_update() |
||
| 56 | |||
| 57 | function it_retains_the_original_protocol_version_when_a_new_protocol_version_is_set() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns an associative array of the message's headers. Each key MUST be a |
||
| 69 | * header name, and each value MUST be an array of strings for that header. |
||
| 70 | * |
||
| 71 | * The keys represent the header name as it will be sent over the wire, and |
||
| 72 | * each value is an array of strings associated with the header. |
||
| 73 | */ |
||
| 74 | function it_returns_its_headers_as_an_associative_array_of_arrays() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * While header names are not case-sensitive, getHeaders() will preserve the |
||
| 96 | * exact case in which headers were originally specified. |
||
| 97 | */ |
||
| 98 | function it_preserves_the_case_of_returned_headers() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns true if any header names match the given header |
||
| 113 | */ |
||
| 114 | function it_reports_that_it_has_a_header() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Checks if a header exists by the given case-insensitive name. |
||
| 121 | */ |
||
| 122 | function it_determines_the_existence_of_a_header_in_a_case_insensitive_way() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns false if no matching header name is found in the message. |
||
| 129 | */ |
||
| 130 | function it_reports_that_it_does_not_have_a_header() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Returns an array of string values as provided for the given header. |
||
| 137 | */ |
||
| 138 | function it_returns_an_existing_header_as_an_array_of_strings() |
||
| 156 | |||
| 157 | function it_returns_an_existing_header_in_a_case_insensitive_way() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * If the header does not appear in the message, this method MUST return an |
||
| 170 | * empty array. |
||
| 171 | */ |
||
| 172 | function it_returns_an_empty_array_when_a_header_does_not_exist() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Retrieves a comma-separated string of the values for a single header. |
||
| 182 | */ |
||
| 183 | function it_returns_a_string_of_values_for_an_existing_header() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * This method returns all of the header values of the given |
||
| 190 | * case-insensitive header name as a string concatenated together using |
||
| 191 | * a comma. |
||
| 192 | */ |
||
| 193 | function it_returns_a_string_of_values_for_an_existing_header_in_a_case_insensitive_way() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * If the header does not appear in the message, this method MUST return |
||
| 200 | * an empty string. |
||
| 201 | */ |
||
| 202 | function it_returns_an_empty_string_for_a_header_that_does_not_exist() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return an instance with the provided value replacing the specified header. |
||
| 209 | */ |
||
| 210 | function it_returns_a_new_request_with_a_header_update_with_a_single_value() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * While header names are case-insensitive, the casing of the header will |
||
| 222 | * be preserved by this function, and returned from getHeaders(). |
||
| 223 | */ |
||
| 224 | View Code Duplication | function it_returns_a_new_request_with_a_header_update_in_a_case_insensitive_way() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Return an instance with the provided value replacing the specified header. |
||
| 236 | */ |
||
| 237 | View Code Duplication | function it_returns_a_new_request_with_a_header_update_with_an_array_of_values() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * This method MUST be implemented in such a way as to retain the |
||
| 249 | * immutability of the message, and MUST return an instance that has the |
||
| 250 | * new and/or updated header and value. |
||
| 251 | */ |
||
| 252 | function it_retains_its_original_header_when_it_replaces_a_header() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 272 | */ |
||
| 273 | View Code Duplication | function it_throws_an_exception_with_a_header_update_with_invalid_header_name() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 285 | */ |
||
| 286 | View Code Duplication | function it_throws_an_exception_with_a_header_update_with_invalid_value() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Existing values for the specified header will be maintained. The new |
||
| 298 | * value(s) will be appended to the existing list. |
||
| 299 | */ |
||
| 300 | View Code Duplication | function it_returns_a_new_request_with_a_header_added_with_a_single_value() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * While header names are case-insensitive, the casing of the header will |
||
| 312 | * be preserved by this function, and returned from getHeaders(). |
||
| 313 | */ |
||
| 314 | View Code Duplication | function it_returns_a_new_request_with_a_header_added_in_a_case_insensitive_way() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Existing values for the specified header will be maintained. The new |
||
| 326 | * value(s) will be appended to the existing list. |
||
| 327 | */ |
||
| 328 | View Code Duplication | function it_returns_a_new_request_with_a_header_added_with_an_array_of_values() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * If the header did not exist previously, it will be added. |
||
| 340 | */ |
||
| 341 | View Code Duplication | function it_returns_a_new_request_with_a_header_created_given_a_nonexistent_header() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * This method MUST be implemented in such a way as to retain the |
||
| 353 | * immutability of the message, and MUST return an instance that has the |
||
| 354 | * new and/or updated header and value. |
||
| 355 | */ |
||
| 356 | View Code Duplication | function it_retains_its_original_header_when_adding_a_header() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 369 | */ |
||
| 370 | View Code Duplication | function it_throws_an_exception_with_a_header_added_with_invalid_header_name() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Throws \InvalidArgumentException for invalid header names or values. |
||
| 382 | */ |
||
| 383 | View Code Duplication | function it_throws_an_exception_with_a_header_added_with_invalid_value() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Return an instance without the specified header. |
||
| 395 | */ |
||
| 396 | function it_returns_a_new_request_without_a_header() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Header resolution MUST be done without case-sensitivity. |
||
| 405 | */ |
||
| 406 | function it_returns_a_new_request_without_a_header_in_a_case_insensitive_way() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * This method MUST be implemented in such a way as to retain the |
||
| 415 | * immutability of the message, and MUST return an instance that removes |
||
| 416 | * the named header. |
||
| 417 | */ |
||
| 418 | function it_retains_its_header_when_returning_a_new_request_without_a_header() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Gets the body of the message. |
||
| 429 | */ |
||
| 430 | function it_returns_the_body_of_its_message_as_a_stream() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Return an instance with the specified message body. |
||
| 437 | * |
||
| 438 | * @param \PhpSpec\Wrapper\Collaborator|StreamInterface $body |
||
| 439 | */ |
||
| 440 | function it_returns_a_new_request_with_a_body(StreamInterface $body) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * This method MUST be implemented in such a way as to retain the |
||
| 451 | * immutability of the message, and MUST return a new instance that has the |
||
| 452 | * new body stream. |
||
| 453 | * |
||
| 454 | * @param \PhpSpec\Wrapper\Collaborator|StreamInterface $old_body |
||
| 455 | * @param \PhpSpec\Wrapper\Collaborator|StreamInterface $new_body |
||
| 456 | */ |
||
| 457 | function it_retains_its_body_when_returning_a_new_request_with_a_body(StreamInterface $old_body, |
||
| 469 | |||
| 470 | /** |
||
| 471 | * The body MUST be a StreamInterface object. |
||
| 472 | * |
||
| 473 | * Throws \InvalidArgumentException When the body is not valid. |
||
| 474 | */ |
||
| 475 | function it_throws_an_exception_with_an_invalid_body() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Retrieves the message's request target. |
||
| 486 | * |
||
| 487 | * Retrieves the message's request-target either as it will appear (for |
||
| 488 | * clients), as it appeared at request (for servers), or as it was |
||
| 489 | * specified for the instance (see withRequestTarget()). |
||
| 490 | * |
||
| 491 | * In most cases, this will be the origin-form of the composed URI, |
||
| 492 | * unless a value was provided to the concrete implementation (see |
||
| 493 | * withRequestTarget() below). |
||
| 494 | */ |
||
| 495 | function it_returns_the_request_target_from_a_uri_path() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Retrieves the message's request target. |
||
| 502 | * |
||
| 503 | * Retrieves the message's request-target either as it will appear (for |
||
| 504 | * clients), as it appeared at request (for servers), or as it was |
||
| 505 | * specified for the instance (see withRequestTarget()). |
||
| 506 | * |
||
| 507 | * In most cases, this will be the origin-form of the composed URI, |
||
| 508 | * unless a value was provided to the concrete implementation (see |
||
| 509 | * withRequestTarget() below). |
||
| 510 | */ |
||
| 511 | function it_returns_the_request_target_explicitly_set() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * If no URI is available, and no request-target has been specifically |
||
| 518 | * provided, this method MUST return the string "/". |
||
| 519 | */ |
||
| 520 | function it_returns_an_empty_path_when_no_request_target_and_no_uri_exists() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Return an instance with the specific request-target. |
||
| 527 | */ |
||
| 528 | function it_returns_a_new_request_with_a_request_target() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * This method MUST be implemented in such a way as to retain the |
||
| 539 | * immutability of the message, and MUST return an instance that has the |
||
| 540 | * changed request target. |
||
| 541 | */ |
||
| 542 | function it_retains_its_request_target_when_creating_a_request_with_a_new_request_target() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * $requestTarget parameter type may be mixed. |
||
| 554 | */ |
||
| 555 | function it_returns_a_new_request_with_an_empty_request_target_with_a_null_request_target() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Retrieves the HTTP method of the request. |
||
| 566 | */ |
||
| 567 | function it_returns_its_method() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Return an instance with the provided HTTP method. |
||
| 574 | * |
||
| 575 | * While HTTP method names are typically all uppercase characters, HTTP |
||
| 576 | * method names are case-sensitive and thus implementations SHOULD NOT |
||
| 577 | * modify the given string. |
||
| 578 | */ |
||
| 579 | function it_returns_a_new_request_with_a_method() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * This method MUST be implemented in such a way as to retain the |
||
| 590 | * immutability of the message, and MUST return an instance that has the |
||
| 591 | * changed request method. |
||
| 592 | */ |
||
| 593 | function it_retains_its_method_when_creating_a_request_with_a_new_method() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Throws \InvalidArgumentException for invalid HTTP methods. |
||
| 605 | */ |
||
| 606 | function it_throws_an_exception_with_an_invalid_method() |
||
| 614 | |||
| 615 | /** |
||
| 616 | * This method MUST return a UriInterface instance. |
||
| 617 | */ |
||
| 618 | function it_returns_its_uri() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * This method MUST update the Host header of the returned request by |
||
| 625 | * default if the URI contains a host component. |
||
| 626 | * |
||
| 627 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 628 | */ |
||
| 629 | function it_returns_a_new_request_with_a_uri_with_host_replacement(UriInterface $uri) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * You can opt-in to preserving the original state of the Host header by |
||
| 643 | * setting `$preserveHost` to `true`. |
||
| 644 | * |
||
| 645 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 646 | * Host header in the following ways: |
||
| 647 | * |
||
| 648 | * - If a Host header is present and non-empty, this method MUST NOT update |
||
| 649 | * the Host header in the returned request. |
||
| 650 | * |
||
| 651 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 652 | */ |
||
| 653 | function it_returns_a_new_request_with_a_uri_without_host_replacement(UriInterface $uri) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 668 | * Host header in the following ways: |
||
| 669 | * |
||
| 670 | * - If the Host header is missing or empty, and the new URI contains |
||
| 671 | * a host component, this method MUST update the Host header in the returned |
||
| 672 | * request. |
||
| 673 | * |
||
| 674 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 675 | */ |
||
| 676 | View Code Duplication | function it_returns_a_new_request_with_a_host_when_no_host_set(UriInterface $uri) |
|
| 688 | |||
| 689 | /** |
||
| 690 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 691 | * Host header in the following ways: |
||
| 692 | * |
||
| 693 | * - If the Host header is missing or empty, and the new URI contains |
||
| 694 | * a host component, this method MUST update the Host header in the returned |
||
| 695 | * request. |
||
| 696 | * |
||
| 697 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 698 | */ |
||
| 699 | View Code Duplication | function it_returns_a_new_request_with_a_host_when_empty_host_set(UriInterface $uri) |
|
| 711 | |||
| 712 | /** |
||
| 713 | * When `$preserveHost` is set to `true`, this method interacts with the |
||
| 714 | * Host header in the following ways: |
||
| 715 | * |
||
| 716 | * - If the Host header is missing or empty, and the new URI does not contain a |
||
| 717 | * host component, this method MUST NOT update the Host header in the returned |
||
| 718 | * request. |
||
| 719 | * |
||
| 720 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $uri |
||
| 721 | */ |
||
| 722 | function it_preserves_its_original_empty_host_with_a_hostless_uri(UriInterface $uri) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * This method MUST be implemented in such a way as to retain the |
||
| 735 | * immutability of the message, and MUST return an instance that has the |
||
| 736 | * new UriInterface instance. |
||
| 737 | * |
||
| 738 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $old_uri |
||
| 739 | * @param \PhpSpec\Wrapper\Collaborator|UriInterface $new_uri |
||
| 740 | */ |
||
| 741 | function it_retains_its_uri_when_returning_a_new_request_with_a_uri(UriInterface $old_uri, |
||
| 752 | } |
||
| 753 |