Complex classes like Url 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 Url, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Url |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * All parts of the URL format, as returned by parse_url. |
||
| 30 | * scheme://user:pass@host:port/path?query#fragment |
||
| 31 | */ |
||
| 32 | public $scheme, $user, $pass, $host, $port, $path, $fragment; |
||
| 33 | private $query; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param string $url The URL to parse, the query part will remain a string. |
||
| 37 | * @param QueryInterface queryObject Optional. An object that parses the query string. |
||
| 38 | */ |
||
| 39 | 17 | public function __construct($url, $queryObject = null) |
|
| 40 | { |
||
| 41 | $componentList = [ |
||
| 42 | 17 | 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment' |
|
| 43 | ]; |
||
| 44 | 17 | $this->importUrlComponents( parse_url( $url ), $componentList ); |
|
| 45 | 17 | if ( isset( $queryObject ) ) { |
|
| 46 | 17 | $this->query = $queryObject->import( $this->query ); |
|
| 47 | } |
||
| 48 | 17 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | 15 | public function __toString() |
|
| 54 | { |
||
| 55 | 15 | switch ($this->scheme) { |
|
| 56 | case 'file': |
||
| 57 | 1 | return $this->getScheme() . '//' . $this->host . $this->getFilePath(); |
|
| 58 | break; |
||
| 59 | case 'mailto': |
||
| 60 | case 'news': |
||
| 61 | 1 | return ( $this->path ? $this->getScheme() . $this->getPath() : '' ); |
|
| 62 | break; |
||
| 63 | 15 | case 'ldap': |
|
| 64 | 1 | return $this->getSchemeAndAuthority() . $this->getPath() . $this->getLdapQuery(); |
|
| 65 | break; |
||
| 66 | default: |
||
| 67 | 15 | return $this->getSchemeAndAuthority() . $this->getPath() . $this->getQuery() . $this->getFragment(); |
|
| 68 | break; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param $name |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | 8 | public function __get($name) |
|
| 77 | { |
||
| 78 | 8 | switch ( (string) $name ) { |
|
| 79 | case 'password': |
||
| 80 | return $this->pass; |
||
| 81 | break; |
||
| 82 | case 'query': |
||
| 83 | 8 | return $this->query; |
|
| 84 | break; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $name |
||
| 90 | * @param $value |
||
| 91 | */ |
||
| 92 | public function __set($name, $value) |
||
| 93 | { |
||
| 94 | switch ( (string) $name ) { |
||
| 95 | case 'password': |
||
| 96 | $this->pass = $value; |
||
| 97 | break; |
||
| 98 | case 'query': |
||
| 99 | if ( is_object( $this->query ) ) { |
||
| 100 | $this->query->reset()->import( $value ); |
||
| 101 | } else { |
||
| 102 | $this->query = $value; |
||
| 103 | } |
||
| 104 | break; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * |
||
| 110 | */ |
||
| 111 | public function __clone() |
||
| 112 | { |
||
| 113 | if ( is_object( $this->query ) ) { |
||
| 114 | $this->query = clone $this->query; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $components |
||
| 120 | * @param $validComponents |
||
| 121 | */ |
||
| 122 | private function importUrlComponents($components, $validComponents) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | 15 | private function getSchemeAndAuthority() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 13 | private function getScheme() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 13 | private function getAuthority() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 13 | private function getUser() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | 1 | private function getPassword() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 13 | private function getPort() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @return mixed|string |
||
| 180 | */ |
||
| 181 | 15 | private function getPath() |
|
| 182 | { |
||
| 183 | 15 | if (!$this->path) { |
|
| 184 | 1 | return ''; |
|
| 185 | } |
||
| 186 | 15 | $path = $this->path; |
|
| 187 | 15 | if ( $this->host && ( !$path || $path[0] !== '/' ) ) { |
|
| 188 | // note: if a host is set, the path _must_ be made absolute or the URL will be invalid |
||
| 189 | $path = '/' . $path; |
||
| 190 | } |
||
| 191 | // urlencode encodes too many characters for the path part, so we decode them back to get readable urls. |
||
| 192 | 15 | return str_replace( [ '%3D', '%2B', '%3A', '%40', '%7E'], [ '=', '+', ':', '@', '~'], $path = join( '/', array_map( 'urlencode', explode( '/', $path ) ) ) ); |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return mixed|string |
||
| 197 | */ |
||
| 198 | 1 | private function getFilePath() |
|
| 199 | { |
||
| 200 | // in the file: scheme, a path must start with a '/' even if no host is set. This contradicts with the email: scheme. |
||
| 201 | 1 | $path = $this->getPath(); |
|
| 202 | 1 | if ($path && $path[0]!=='/') { |
|
| 203 | 1 | $path = '/' . $path; |
|
| 204 | } |
||
| 205 | |||
| 206 | 1 | return $path; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 15 | private function getQuery() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 1 | private function getLdapQuery() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 15 | private function getFragment() |
|
| 238 | |||
| 239 | } |
||
| 240 |