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:
Complex classes like IRI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IRI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class IRI |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The scheme |
||
| 23 | * |
||
| 24 | * @var string|null |
||
| 25 | */ |
||
| 26 | private $scheme = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The user information |
||
| 30 | * |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | private $userinfo = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The host |
||
| 37 | * |
||
| 38 | * @var string|null |
||
| 39 | */ |
||
| 40 | private $host = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The port |
||
| 44 | * |
||
| 45 | * @var string|null |
||
| 46 | */ |
||
| 47 | private $port = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The path |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $path = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The query component |
||
| 58 | * |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | private $query = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The fragment identifier |
||
| 65 | * |
||
| 66 | * @var string|null |
||
| 67 | */ |
||
| 68 | private $fragment = null; |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructor |
||
| 73 | * |
||
| 74 | * @param null|string|IRI $iri The IRI. |
||
| 75 | * |
||
| 76 | * @throws \InvalidArgumentException If an invalid IRI is passed. |
||
| 77 | * |
||
| 78 | * @api |
||
| 79 | */ |
||
| 80 | public function __construct($iri = null) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the scheme |
||
| 104 | * |
||
| 105 | * @return string|null Returns the scheme or null if not set. |
||
| 106 | */ |
||
| 107 | public function getScheme() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the authority |
||
| 114 | * |
||
| 115 | * @return string|null Returns the authority or null if not set. |
||
| 116 | */ |
||
| 117 | public function getAuthority() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the user information |
||
| 137 | * |
||
| 138 | * @return string|null Returns the user information or null if not set. |
||
| 139 | */ |
||
| 140 | public function getUserInfo() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get the host |
||
| 147 | * |
||
| 148 | * @return string|null Returns the host or null if not set. |
||
| 149 | */ |
||
| 150 | public function getHost() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the port |
||
| 157 | * |
||
| 158 | * @return string|null Returns the port or null if not set. |
||
| 159 | */ |
||
| 160 | public function getPort() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the path |
||
| 167 | * |
||
| 168 | * @return string Returns the path which might be empty. |
||
| 169 | */ |
||
| 170 | public function getPath() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the query component |
||
| 177 | * |
||
| 178 | * @return string|null Returns the query component or null if not set. |
||
| 179 | */ |
||
| 180 | public function getQuery() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the fragment identifier |
||
| 187 | * |
||
| 188 | * @return string|null Returns the fragment identifier or null if not set. |
||
| 189 | */ |
||
| 190 | public function getFragment() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Find out whether the IRI is absolute |
||
| 197 | * |
||
| 198 | * @return bool Returns true if the IRI is absolute, false otherwise. |
||
| 199 | * |
||
| 200 | * @api |
||
| 201 | */ |
||
| 202 | public function isAbsolute() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get as absolute IRI, i.e., without fragment identifier |
||
| 209 | * |
||
| 210 | * @return IRI The absolute IRI, i.e., without fragment identifier |
||
| 211 | * |
||
| 212 | * @throws \UnexpectedValueException If the IRI is a relative IRI. |
||
| 213 | * |
||
| 214 | * @link http://tools.ietf.org/html/rfc3987#section-2.2 RFC3987 absolute-IRI |
||
| 215 | * |
||
| 216 | * @api |
||
| 217 | */ |
||
| 218 | public function getAbsoluteIri() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Check whether the passed IRI is equal |
||
| 232 | * |
||
| 233 | * @param IRI|string $iri IRI to compare to this instance. |
||
| 234 | * |
||
| 235 | * @return bool Returns true if the two IRIs are equal, false otherwise. |
||
| 236 | * |
||
| 237 | * @api |
||
| 238 | */ |
||
| 239 | public function equals($iri) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Resolve a (relative) IRI reference against this IRI |
||
| 247 | * |
||
| 248 | * @param IRI|string $reference The (relative) IRI reference that should |
||
| 249 | * be resolved against this IRI. |
||
| 250 | * |
||
| 251 | * @return IRI The resolved IRI. |
||
| 252 | * |
||
| 253 | * @throws \InvalidArgumentException If an invalid IRI is passed. |
||
| 254 | * |
||
| 255 | * @link http://tools.ietf.org/html/rfc3986#section-5.2 |
||
| 256 | * |
||
| 257 | * @api |
||
| 258 | */ |
||
| 259 | public function resolve($reference) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Transform this IRI to a IRI reference relative to the passed base IRI |
||
| 342 | * |
||
| 343 | * @param IRI|string $base The (relative) IRI reference that should be |
||
| 344 | * be used as base IRI. |
||
| 345 | * @param bool Defines whether schema-relative IRIs such |
||
| 346 | * as `//example.com` should be created (`true`) |
||
| 347 | * or not (`false`). |
||
| 348 | * |
||
| 349 | * @return IRI The IRI reference relative to the passed base IRI. |
||
| 350 | * |
||
| 351 | * @throws \InvalidArgumentException If an invalid IRI is passed. |
||
| 352 | * |
||
| 353 | * @api |
||
| 354 | */ |
||
| 355 | public function relativeTo($base, $schemaRelative = false) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Convert an IRI to a relative IRI reference using this IRI as base |
||
| 430 | * |
||
| 431 | * This method provides a more convenient interface than the |
||
| 432 | * {@link IRI::relativeTo()} method if the base IRI stays the same while |
||
| 433 | * the IRIs to convert to relative IRI references change. |
||
| 434 | * |
||
| 435 | * @param string|IRI $iri The IRI to convert to a relative reference |
||
| 436 | * @param bool Defines whether schema-relative IRIs such |
||
| 437 | * as `//example.com` should be created (`true`) |
||
| 438 | * or not (`false`). |
||
| 439 | * |
||
| 440 | * @throws \InvalidArgumentException If an invalid IRI is passed. |
||
| 441 | * |
||
| 442 | * @see \ML\IRI\IRI::relativeTo() |
||
| 443 | * |
||
| 444 | * @return IRI The relative IRI reference |
||
| 445 | */ |
||
| 446 | public function baseFor($iri, $schemaRelative = false) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get a string representation of this IRI object |
||
| 457 | * |
||
| 458 | * @return string A string representation of this IRI instance. |
||
| 459 | * |
||
| 460 | * @api |
||
| 461 | */ |
||
| 462 | public function __toString() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Parse an IRI into it's components |
||
| 489 | * |
||
| 490 | * This is done according to |
||
| 491 | * {@link http://tools.ietf.org/html/rfc3986#section-3.1 RFC3986}. |
||
| 492 | * |
||
| 493 | * @param string $iri The IRI to parse. |
||
| 494 | */ |
||
| 495 | protected function parse($iri) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Remove dot-segments |
||
| 555 | * |
||
| 556 | * This method removes the special "." and ".." complete path segments |
||
| 557 | * from an IRI. |
||
| 558 | * |
||
| 559 | * @param string $input The IRI from which dot segments should be removed. |
||
| 560 | * |
||
| 561 | * @return string The IRI with all dot-segments removed. |
||
| 562 | * |
||
| 563 | * @link http://tools.ietf.org/html/rfc3986#section-5.2.4 |
||
| 564 | */ |
||
| 565 | private static function removeDotSegments($input) |
||
| 602 | } |
||
| 603 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.