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 | 371 | public function __construct($rawUri) | |
| 63 | |||
| 64 | /** | ||
| 65 | * @return string | ||
| 66 | */ | ||
| 67 | 3 | public function getRawUri() | |
| 71 | |||
| 72 | /** | ||
| 73 | * @return string | ||
| 74 | */ | ||
| 75 | 52 | public function getRawPointer() | |
| 79 | |||
| 80 | /** | ||
| 81 | * @return bool | ||
| 82 | */ | ||
| 83 | 85 | public function isAbsolute() | |
| 87 | |||
| 88 | /** | ||
| 89 | * @return string | ||
| 90 | */ | ||
| 91 | 61 | public function getScheme() | |
| 95 | |||
| 96 | /** | ||
| 97 | * @return string | ||
| 98 | */ | ||
| 99 | 61 | public function getAuthority() | |
| 103 | |||
| 104 | /** | ||
| 105 | * @return string | ||
| 106 | */ | ||
| 107 | 61 | public function getPath() | |
| 111 | |||
| 112 | /** | ||
| 113 | * @return string | ||
| 114 | */ | ||
| 115 | 61 | public function getQuery() | |
| 119 | |||
| 120 | /** | ||
| 121 | * @return string[] | ||
| 122 | */ | ||
| 123 | 62 | public function getPointerSegments() | |
| 127 | |||
| 128 | /** | ||
| 129 | * Returns the primary resource identifier part of the URI, i.e. everything | ||
| 130 | * excluding its fragment part. | ||
| 131 | * | ||
| 132 | * @return string | ||
| 133 | */ | ||
| 134 | 341 | public function getPrimaryResourceIdentifier() | |
| 138 | |||
| 139 | /** | ||
| 140 | * Resolves the current (relative) URI against another (absolute) URI. | ||
| 141 | * Example:. | ||
| 142 | * | ||
| 143 | * Current = foo.json | ||
| 144 | * Other = http://localhost/bar/baz | ||
| 145 | * Resolved = http://localhost/bar/foo.json | ||
| 146 | * | ||
| 147 | * @param Uri $uri | ||
| 148 | * | ||
| 149 | * @return string | ||
| 150 | */ | ||
| 151 | 63 | public function resolveAgainst(Uri $uri) | |
| 170 | |||
| 171 | /** | ||
| 172 | * Returns whether two URIs share the same primary resource identifier, | ||
| 173 | * i.e. whether they point to the same document. | ||
| 174 | * | ||
| 175 | * @param Uri $uri | ||
| 176 | * | ||
| 177 | * @return bool | ||
| 178 | */ | ||
| 179 | 5 | public function isSamePrimaryResource(Uri $uri) | |
| 187 | |||
| 188 | 371 | private function buildFromRawUri($rawUri) | |
| 189 |     { | ||
| 190 | 371 | $this->raw = rawurldecode($rawUri); | |
| 191 | 371 | $this->parts = @parse_url($this->raw); | |
| 192 | |||
| 193 | 371 |         if (false === $this->parts) { | |
| 194 | 1 |             throw new \InvalidArgumentException("Cannot parse URI '{$rawUri}'"); | |
| 195 | } | ||
| 196 | |||
| 197 | 370 |         foreach (self::$partNames as $part) { | |
| 198 | 370 |             if (!isset($this->parts[$part])) { | |
| 199 | 370 | $this->parts[$part] = ''; | |
| 200 | 370 | } | |
| 201 | 370 | } | |
| 202 | |||
| 203 | 370 |         if ($this->parts['scheme'] === 'file' && preg_match('/^[A-Z]:/', $this->parts['path'])) { | |
| 204 | $this->parts['path'] = '/' . $this->parts['path']; | ||
| 205 | } | ||
| 206 | |||
| 207 | 370 | $this->authority = $this->buildAuthority(); | |
| 208 | 370 | $this->segments = $this->buildSegments(); | |
| 209 | 370 | $this->primaryIdentifier = $this->buildPrimaryIdentifier(); | |
| 210 | 370 | } | |
| 211 | |||
| 212 | 370 | private function buildAuthority() | |
| 231 | |||
| 232 | 370 | private function buildSegments() | |
| 252 | |||
| 253 | 370 | private function buildPrimaryIdentifier() | |
| 269 | |||
| 270 | 61 | private function buildResolvedUriAgainst(Uri $uri) | |
| 294 | |||
| 295 | 12 | private function buildResolvedPathAgainst($againstPath) | |
| 307 | |||
| 308 | 61 | private function appendRelativeParts($absolutePart, $query, $fragment) | |
| 320 | } | ||
| 321 |