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 | 48 | public function __construct($url, $queryObject = null) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | 42 | public function __toString() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param $name |
||
| 78 | * @return mixed |
||
| 79 | */ |
||
| 80 | 20 | public function __get($name) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param $name |
||
| 94 | * @param $value |
||
| 95 | */ |
||
| 96 | public function __set($name, $value) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * |
||
| 114 | */ |
||
| 115 | public function __clone() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $components |
||
| 124 | * @param $validComponents |
||
| 125 | */ |
||
| 126 | private function importUrlComponents($components, $validComponents) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 42 | private function getSchemeAndAuthority() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 38 | private function getScheme() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 38 | private function getAuthority() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 38 | private function getUser() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | 6 | private function getPassword() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 38 | private function getPort() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return mixed|string |
||
| 184 | */ |
||
| 185 | 42 | private function getPath() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @return mixed|string |
||
| 201 | */ |
||
| 202 | 2 | private function getFilePath() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | 42 | private function getQuery() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 2 | private function getLdapQuery() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | 42 | private function getFragment() |
|
| 242 | |||
| 243 | } |
||
| 244 |