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 | class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary { |
||
| 14 | /** |
||
| 15 | * Get the given header |
||
| 16 | * |
||
| 17 | * Unlike {@see self::getValues()}, this returns a string. If there are |
||
| 18 | * multiple values, it concatenates them with a comma as per RFC2616. |
||
| 19 | * |
||
| 20 | * Avoid using this where commas may be used unquoted in values, such as |
||
| 21 | * Set-Cookie headers. |
||
| 22 | * |
||
| 23 | * @param string $key |
||
| 24 | * @return string|null Header value |
||
| 25 | */ |
||
| 26 | View Code Duplication | public function offsetGet($key) { |
|
| 34 | |||
| 35 | /** |
||
| 36 | * Set the given item |
||
| 37 | * |
||
| 38 | * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) |
||
| 39 | * |
||
| 40 | * @param string $key Item name |
||
| 41 | * @param string $value Item value |
||
| 42 | */ |
||
| 43 | public function offsetSet($key, $value) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get all values for a given header |
||
| 59 | * |
||
| 60 | * @param string $key |
||
| 61 | * @return array|null Header values |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function getValues($key) { |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Flattens a value into a string |
||
| 74 | * |
||
| 75 | * Converts an array into a string by imploding values with a comma, as per |
||
| 76 | * RFC2616's rules for folding headers. |
||
| 77 | * |
||
| 78 | * @param string|array $value Value to flatten |
||
| 79 | * @return string Flattened value |
||
| 80 | */ |
||
| 81 | public function flatten($value) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get an iterator for the data |
||
| 91 | * |
||
| 92 | * Converts the internal |
||
| 93 | * @return ArrayIterator |
||
| 94 | */ |
||
| 95 | public function getIterator() { |
||
| 98 | } |
||
| 99 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.