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 | class UserInfo extends AbstractUriPart |
||
| 9 | { |
||
| 10 | protected $unencoded_characters = array(":"); |
||
| 11 | |||
| 12 | /** |
||
| 13 | * UserInfo constructor. Accepts a string representing a URI user info component. Construction will throw an |
||
| 14 | * exception if the user info 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 | * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) |
||
| 23 | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
||
| 24 | * pct-encoded = "%" HEXDIG HEXDIG |
||
| 25 | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" |
||
| 26 | * |
||
| 27 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
| 28 | * |
||
| 29 | * @throws \InvalidArgumentException |
||
| 30 | * |
||
| 31 | * @param string $user_info A string representing a URI fragment |
||
| 32 | */ |
||
| 33 | 12 | public function __construct($user_info) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Compiles a regexp pattern based on predefined patterns that define allowed characters for user info |
||
| 40 | */ |
||
| 41 | 12 | View Code Duplication | protected function compileValidPattern() |
| 50 | |||
| 51 | /** |
||
| 52 | * Compiles an array of legal characters for user info based upon the RFC3986 specification: |
||
| 53 | * |
||
| 54 | * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) |
||
| 55 | */ |
||
| 56 | 12 | protected function compileUnencodedCharacters() |
|
| 64 | } |
||
| 65 |
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.