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 | 14 | public function __construct($url, $queryObject = null) |
|
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | 12 | public function __toString() |
|
71 | |||
72 | /** |
||
73 | * @param $name |
||
74 | * @return mixed |
||
75 | */ |
||
76 | 5 | public function __get($name) |
|
87 | |||
88 | /** |
||
89 | * @param $name |
||
90 | * @param $value |
||
91 | */ |
||
92 | public function __set($name, $value) |
||
107 | |||
108 | /** |
||
109 | * |
||
110 | */ |
||
111 | public function __clone() |
||
117 | |||
118 | /** |
||
119 | * @param $components |
||
120 | * @param $validComponents |
||
121 | */ |
||
122 | private function importUrlComponents($components, $validComponents) |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | 12 | private function getSchemeAndAuthority() |
|
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | 10 | private function getScheme() |
|
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | 10 | private function getAuthority() |
|
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | 10 | private function getUser() |
|
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | 1 | private function getPassword() |
|
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | 10 | private function getPort() |
|
177 | |||
178 | /** |
||
179 | * @return mixed|string |
||
180 | */ |
||
181 | 12 | private function getPath() |
|
194 | |||
195 | /** |
||
196 | * @return mixed|string |
||
197 | */ |
||
198 | 1 | private function getFilePath() |
|
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | 12 | private function getQuery() |
|
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | 1 | private function getLdapQuery() |
|
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | 12 | private function getFragment() |
|
238 | |||
239 | } |
||
240 |