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 |
|
|
1 ignored issue
–
show
|
|||
| 9 | { |
||
| 10 | protected $unencoded_characters = array("/", "?"); |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Query constructor. Accepts a string representing a URI query. Construction will throw an exception if the |
||
| 14 | * query is not a string. |
||
| 15 | * |
||
| 16 | * Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded |
||
| 17 | * and will encode invalid characters. |
||
| 18 | * |
||
| 19 | * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded |
||
| 20 | * string, resulting in double-encoding. |
||
| 21 | * |
||
| 22 | * query = *( pchar / "/" / "?" ) |
||
| 23 | * pchar = unreserved / pct-encoded / sub-delims / ":" / "@" |
||
| 24 | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
||
| 25 | * pct-encoded = "%" HEXDIG HEXDIG |
||
| 26 | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" |
||
| 27 | * |
||
| 28 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
| 29 | * |
||
| 30 | * @throws \InvalidArgumentException |
||
| 31 | * |
||
| 32 | * @param string $query A string representing a URI query |
||
| 33 | */ |
||
| 34 | 89 | public function __construct($query) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Returns a string representation of the query formatted so that it can be compiled into a complete URI string |
||
| 50 | * per the Uri object's string specification. |
||
| 51 | * |
||
| 52 | * If the query is empty, toUriString returns an empty string; if the query is not empty, toUriString returns the |
||
| 53 | * query prefixed with a question mark. |
||
| 54 | * |
||
| 55 | * @see Uri::__toString |
||
| 56 | * |
||
| 57 | * @return string A string representation of the query formatted for a complete URI string |
||
| 58 | */ |
||
| 59 | 33 | public function toUriString() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Compiles a regexp pattern based on predefined patterns that define allowed characters for a query. |
||
| 72 | */ |
||
| 73 | 89 | protected function compileValidPattern() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Compiles an array of legal characters for a query based upon the RFC3986 specification: |
||
| 86 | * |
||
| 87 | * query = *( pchar / "/" / "?" ) |
||
| 88 | */ |
||
| 89 | 89 | protected function compileUnencodedCharacters() |
|
| 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.