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 Authority extends AbstractUriPart |
||
| 9 | { |
||
| 10 | /** @var UserInfo */ |
||
| 11 | protected $user_info; |
||
| 12 | /** @var Host */ |
||
| 13 | protected $host; |
||
| 14 | /** @var Port */ |
||
| 15 | protected $port; |
||
| 16 | |||
| 17 | protected $authority_parts = array( |
||
| 18 | "user_info" => "", |
||
| 19 | "host" => "", |
||
| 20 | "port" => null, |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** @noinspection PhpMissingParentConstructorInspection */ |
||
| 24 | /** |
||
| 25 | * Authority constructor. Accepts a string representing a URI authority component. Construction will throw an |
||
| 26 | * exception if the authority is not a string. |
||
| 27 | * |
||
| 28 | * Construction splits a given authority string into its component objects, UserInfo, Host, and Port, whose |
||
| 29 | * individual constructors may throw exceptions provided invalid component strings. |
||
| 30 | * |
||
| 31 | * Construction accepts strings for UserInfo and Host that have been percent-encoded as well as strings that have |
||
| 32 | * not been percent-encoded and will encode invalid characters. |
||
| 33 | * |
||
| 34 | * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded |
||
| 35 | * string, resulting in double-encoding. |
||
| 36 | * |
||
| 37 | * authority = [ userinfo "@" ] host [ ":" port ] |
||
| 38 | * |
||
| 39 | * @see UserInfo |
||
| 40 | * @see Host |
||
| 41 | * @see Port |
||
| 42 | * @see https://tools.ietf.org/html/rfc3986#appendix-A |
||
| 43 | * |
||
| 44 | * @throws \InvalidArgumentException given a non-string |
||
| 45 | * |
||
| 46 | * @param string $authority A string representing a URI authority |
||
| 47 | */ |
||
| 48 | 10 | public function __construct($authority) |
|
| 49 | { |
||
| 50 | 10 | if (!is_string($authority)) { |
|
| 51 | 6 | throw new \InvalidArgumentException("Authority must be a string"); |
|
| 52 | } else { |
||
| 53 | 4 | $exploded_authority = $this->explodeAuthority($authority); |
|
| 54 | |||
| 55 | 4 | $this->authority_parts = array_merge($this->authority_parts, $exploded_authority); |
|
| 56 | |||
| 57 | 4 | $this->user_info = new UserInfo($this->authority_parts["user_info"]); |
|
| 58 | 4 | $this->host = new Host($this->authority_parts["host"]); |
|
| 59 | 4 | $this->port = new Port($this->authority_parts["port"]); |
|
| 60 | } |
||
| 61 | 3 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the authority's UserInfo object |
||
| 65 | * |
||
| 66 | * @return UserInfo |
||
| 67 | */ |
||
| 68 | 4 | public function getUserInfo() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the authority's Host object |
||
| 75 | * |
||
| 76 | * @return Host |
||
| 77 | */ |
||
| 78 | 3 | public function getHost() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the authority's Port object |
||
| 85 | * |
||
| 86 | * @return Port |
||
| 87 | */ |
||
| 88 | 4 | public function getPort() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Returns a string representation of the authority |
||
| 95 | * |
||
| 96 | * @return string A string representation of the authority |
||
| 97 | */ |
||
| 98 | 4 | public function __toString() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Splits URI authority data into user info, host, and port data, returning an array with named keys. |
||
| 105 | * |
||
| 106 | * For the host component, it will capture everything within brackets to support ipv6 or match all characters until |
||
| 107 | * it finds a colon indicating the start of the port component. |
||
| 108 | * |
||
| 109 | * @param string $authority The authority part of a URI to be decomposed |
||
| 110 | * @return mixed[] An array with named keys containing the component parts of the supplied |
||
|
|
|||
| 111 | * authority |
||
| 112 | */ |
||
| 113 | 4 | View Code Duplication | private function explodeAuthority($authority) |
| 131 | } |
||
| 132 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.