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 |
||
| 8 | View Code Duplication | class Query extends AbstractUriPart |
|
| 9 | { |
||
| 10 | protected static $unencoded_characters = array("/", "?"); |
||
| 11 | |||
| 12 | protected static $part_pattern; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Query constructor. Accepts a string representing a URI query. Construction will throw an exception if the |
||
| 16 | * query is not a string. |
||
| 17 | * |
||
| 18 | * Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded |
||
| 19 | * and will encode invalid characters. |
||
| 20 | * |
||
| 21 | * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded |
||
| 22 | * string, resulting in double-encoding. |
||
| 23 | * |
||
| 24 | * query = *( pchar / "/" / "?" ) |
||
| 25 | * pchar = unreserved / pct-encoded / sub-delims / ":" / "@" |
||
| 26 | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
||
| 27 | * pct-encoded = "%" HEXDIG HEXDIG |
||
| 28 | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" |
||
| 29 | * |
||
| 30 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
| 31 | * |
||
| 32 | * @throws \InvalidArgumentException |
||
| 33 | * |
||
| 34 | * @param string $query A string representing a URI query |
||
| 35 | */ |
||
| 36 | 83 | public function __construct($query) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Returns a string representation of the query formatted so that it can be compiled into a complete URI string |
||
| 43 | * per the Uri object's string specification. |
||
| 44 | * |
||
| 45 | * If the query is empty, toUriString returns an empty string; if the query is not empty, toUriString returns the |
||
| 46 | * query prefixed with a question mark. |
||
| 47 | * |
||
| 48 | * @see Uri::__toString |
||
| 49 | * |
||
| 50 | * @return string A string representation of the query formatted for a complete URI string |
||
| 51 | */ |
||
| 52 | 18 | public function toUriString() |
|
| 62 | } |
||
| 63 |