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:
| 1 | <?php |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Case-insensitive dictionary, suitable for HTTP headers |
||
| 16 | * |
||
| 17 | * @package Rmccue\Requests |
||
| 18 | */ |
||
| 19 | class Headers extends CaseInsensitiveDictionary { |
||
| 20 | /** |
||
| 21 | * Get the given header |
||
| 22 | * |
||
| 23 | * Unlike {@see self::getValues()}, this returns a string. If there are |
||
| 24 | * multiple values, it concatenates them with a comma as per RFC2616. |
||
| 25 | * |
||
| 26 | * Avoid using this where commas may be used unquoted in values, such as |
||
| 27 | * Set-Cookie headers. |
||
| 28 | * |
||
| 29 | * @param string $key |
||
| 30 | * @return string Header value |
||
| 31 | */ |
||
| 32 | View Code Duplication | public function offsetGet($key) { |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Set the given item |
||
| 43 | * |
||
| 44 | * @throws Rmccue\Requests\Exception On attempting to use dictionary as list (`invalidset`) |
||
| 45 | * |
||
| 46 | * @param string $key Item name |
||
| 47 | * @param string $value Item value |
||
| 48 | */ |
||
| 49 | public function offsetSet($key, $value) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get all values for a given header |
||
| 65 | * |
||
| 66 | * @param string $key |
||
| 67 | * @return array Header values |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function getValues($key) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Flattens a value into a string |
||
| 80 | * |
||
| 81 | * Converts an array into a string by imploding values with a comma, as per |
||
| 82 | * RFC2616's rules for folding headers. |
||
| 83 | * |
||
| 84 | * @param string|array $value Value to flatten |
||
| 85 | * @return string Flattened value |
||
| 86 | */ |
||
| 87 | public function flatten($value) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get an iterator for the data |
||
| 97 | * |
||
| 98 | * Converts the internal |
||
| 99 | * @return ArrayIterator |
||
| 105 |