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 | * Parses HTTP headers into an associative array. |
||
| 15 | * |
||
| 16 | * Example: |
||
| 17 | * <code> |
||
| 18 | * $headers = "HTTP/1.1 200 OK\r\n". |
||
| 19 | * "content-type: text/html; charset=UTF-8\r\n". |
||
| 20 | * "Server: Funky/1.0\r\n". |
||
| 21 | * "Set-Cookie: foo=bar\r\n". |
||
| 22 | * "Set-Cookie: baz=quux\r\n". |
||
| 23 | * "Folds: are\r\n\treformatted\r\n"; |
||
| 24 | * print_r(\TraderInteractive\HttpUtil::parseHeaders($headers)); |
||
| 25 | * </code> |
||
| 26 | * The above example will output: |
||
| 27 | * <pre> |
||
| 28 | * Array |
||
| 29 | * ( |
||
| 30 | * [Response Code] => 200 |
||
| 31 | * [Response Status] => OK |
||
| 32 | * [Content-Type] => text/html; charset=UTF-8 |
||
| 33 | * [Server] => Funky/1.0 |
||
| 34 | * [Set-Cookie] => Array |
||
| 35 | * ( |
||
| 36 | * [0] => foo=bar |
||
| 37 | * [1] => baz=quux |
||
| 38 | * ) |
||
| 39 | * [Folds] => are reformatted |
||
| 40 | * ) |
||
| 41 | * </pre> |
||
| 42 | * |
||
| 43 | * @param string $rawHeaders string containing HTTP headers |
||
| 44 | * |
||
| 45 | * @return array the parsed headers |
||
| 46 | * |
||
| 47 | * @throws Exception Thrown if unable to parse the headers |
||
| 48 | */ |
||
| 49 | public static function parseHeaders(string $rawHeaders) : array |
||
| 97 | |||
| 98 | private static function addRequestDataToHeaders(array $match, array $headers) : array |
||
| 104 | |||
| 105 | private static function addResponseDataToHeaders(array $match, array $headers) : array |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Generate URL-encoded query string |
||
| 114 | * |
||
| 115 | * Example: |
||
| 116 | * <code> |
||
| 117 | * $parameters = [ |
||
| 118 | * 'param1' => ['value', 'another value'], |
||
| 119 | * 'param2' => 'a value', |
||
| 120 | * 'param3' => false, |
||
| 121 | * ]; |
||
| 122 | * |
||
| 123 | * $queryString = \TraderInteractive\HttpUtil::buildQueryString($parameters); |
||
| 124 | * |
||
| 125 | * echo $queryString |
||
| 126 | * </code> |
||
| 127 | * |
||
| 128 | * Output: |
||
| 129 | * <pre> |
||
| 130 | * param1=value¶m1=another+value¶m2=a+value¶m3=false |
||
| 131 | * </pre> |
||
| 132 | * |
||
| 133 | * @param array $parameters An associative array containing parameter key/value(s) |
||
| 134 | * |
||
| 135 | * @return string the built query string |
||
| 136 | */ |
||
| 137 | public static function buildQueryString(array $parameters) : string |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get an array of all url parameters. |
||
| 163 | * |
||
| 164 | * @param string $url The url to parse such as http://foo.com/bar/?id=boo&another=wee&another=boo |
||
| 165 | * @param array $collapsedParams Parameters to collapse. ex. 'id' => ['boo'] to just 'id' => 'boo'. Exception thrown |
||
| 166 | * if more than 1 value |
||
| 167 | * |
||
| 168 | * @return array such as ['id' => ['boo'], 'another' => ['wee', 'boo']] |
||
| 169 | * |
||
| 170 | * @throws Exception if more than one value in a $collapsedParams param |
||
| 171 | */ |
||
| 172 | public static function getQueryParams(string $url, array $collapsedParams = []) : array |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get an array of all url parameters. |
||
| 215 | * |
||
| 216 | * @param string $url The url to parse such as http://foo.com/bar/?single=boo&multi=wee&multi=boo |
||
| 217 | * @param array $expectedArrayParams List of parameter names which are not collapsed. |
||
| 218 | * |
||
| 219 | * @return array such as ['single' => 'boo', 'multi' => ['wee', 'boo']] if 'multi' is given in $expectedArrayParams |
||
| 220 | * |
||
| 221 | * @throws Exception if a parameter is given as array but not included in the expected array argument |
||
| 222 | */ |
||
| 223 | public static function getQueryParamsCollapsed(string $url, array $expectedArrayParams = []) : array |
||
| 260 | } |
||
| 261 |