Complex classes like Uri 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 Uri, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Uri |
||
| 17 | { |
||
| 18 | private static $partNames = [ |
||
| 19 | 'scheme', |
||
| 20 | 'user', |
||
| 21 | 'pass', |
||
| 22 | 'host', |
||
| 23 | 'port', |
||
| 24 | 'path', |
||
| 25 | 'query', |
||
| 26 | 'fragment', |
||
| 27 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $raw; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $parts; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $authority; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | private $segments; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $primaryIdentifier; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor. |
||
| 56 | * |
||
| 57 | * @param string $rawUri |
||
| 58 | */ |
||
| 59 | 388 | public function __construct($rawUri) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 19 | public function getRawUri() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 58 | public function getRawPointer() |
|
| 76 | { |
||
| 77 | 58 | return $this->parts['fragment']; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | 376 | public function isAbsolute() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 59 | public function getScheme() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 59 | public function getAuthority() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 59 | public function getPath() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 59 | public function getQuery() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return string[] |
||
| 122 | */ |
||
| 123 | 70 | public function getPointerSegments() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 337 | public function hasPointer() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the primary resource identifier part of the URI, i.e. everything |
||
| 138 | * excluding its fragment part. |
||
| 139 | * |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | 354 | public function getPrimaryResourceIdentifier() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Resolves the current (relative) URI against another (absolute) URI. |
||
| 149 | * Example:. |
||
| 150 | * |
||
| 151 | * Current = foo.json |
||
| 152 | * Other = http://localhost/bar/baz |
||
| 153 | * Resolved = http://localhost/bar/foo.json |
||
| 154 | * |
||
| 155 | * @param Uri $uri |
||
| 156 | * |
||
| 157 | * @return Uri |
||
| 158 | */ |
||
| 159 | 75 | public function resolveAgainst(Uri $uri) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Returns whether two URIs share the same primary resource identifier, |
||
| 175 | * i.e. whether they point to the same document. |
||
| 176 | * |
||
| 177 | * @param Uri $uri |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 5 | public function isSamePrimaryResource(Uri $uri) |
|
| 189 | |||
| 190 | 388 | private function buildFromRawUri($rawUri) |
|
| 213 | |||
| 214 | 387 | private function buildAuthority() |
|
| 233 | |||
| 234 | 387 | private function buildSegments() |
|
| 250 | |||
| 251 | 387 | private function buildPrimaryIdentifier() |
|
| 271 | |||
| 272 | 59 | private function buildResolvedUriAgainst(Uri $uri) |
|
| 296 | |||
| 297 | 14 | private function buildResolvedPathAgainst($againstPath) |
|
| 309 | |||
| 310 | 59 | private function appendRelativeParts($absolutePart, $query, $fragment) |
|
| 322 | } |
||
| 323 |