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 |
||
10 | class Url extends Object |
||
11 | { |
||
12 | static protected $queryEncodingType = PHP_QUERY_RFC1738; |
||
13 | static protected $validSchemes = ['http', 'https', 'file']; |
||
|
|||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $scheme; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $host; |
||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $port; |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $path; |
||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $query = []; |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $fragment; |
||
39 | |||
40 | 1 | public function __toString() { |
|
43 | |||
44 | /** |
||
45 | * @param string $scheme |
||
46 | */ |
||
47 | 20 | public function setScheme($scheme) { |
|
52 | |||
53 | /** |
||
54 | * @param string $host |
||
55 | */ |
||
56 | 2 | public function setHost($host) { |
|
64 | |||
65 | /** |
||
66 | * @param int $port |
||
67 | */ |
||
68 | 1 | public function setPort($port) { |
|
71 | |||
72 | /** |
||
73 | * @param string $path |
||
74 | */ |
||
75 | 18 | public function setPath($path) { |
|
78 | /** |
||
79 | * @param [] $query |
||
80 | */ |
||
81 | 1 | public function setQuery($query) { |
|
84 | /** |
||
85 | * @param string $query |
||
86 | */ |
||
87 | 1 | public function setRawQuery($query) { |
|
90 | |||
91 | /** |
||
92 | * @param string $fragment |
||
93 | */ |
||
94 | 1 | public function setFragment($fragment) { |
|
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 1 | public function getScheme() { |
|
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 1 | public function getHost() { |
|
118 | |||
119 | /** |
||
120 | * @return int |
||
121 | */ |
||
122 | 1 | public function getPort() { |
|
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | 18 | public function getPath() { |
|
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | 2 | public function getQuery() { |
|
149 | |||
150 | /** |
||
151 | * @param bool $encoded |
||
152 | * @return string |
||
153 | */ |
||
154 | 1 | public function getRawQueryString($encoded = true) { |
|
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | 1 | public function getFragment() { |
|
170 | |||
171 | /** |
||
172 | * @param string $schema |
||
173 | * @return bool |
||
174 | */ |
||
175 | 19 | static public function isValidScheme($schema) { |
|
178 | |||
179 | /** |
||
180 | * @return bool |
||
181 | */ |
||
182 | 1 | public function hasScheme() { |
|
185 | |||
186 | /** |
||
187 | * @return bool |
||
188 | */ |
||
189 | 18 | public function hasHost() { |
|
192 | |||
193 | /** |
||
194 | * @return bool |
||
195 | */ |
||
196 | 1 | public function hasPort() { |
|
199 | |||
200 | /** |
||
201 | * @return bool |
||
202 | */ |
||
203 | 18 | public function hasPath() { |
|
206 | |||
207 | /** |
||
208 | * @return bool |
||
209 | */ |
||
210 | 1 | public function hasQuery() { |
|
213 | |||
214 | /** |
||
215 | * @return bool |
||
216 | */ |
||
217 | 1 | public function hasFragment() { |
|
220 | |||
221 | /** |
||
222 | * @return bool |
||
223 | */ |
||
224 | 3 | public function isLocal() { |
|
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 2 | public function getUrl() { |
|
239 | |||
240 | /** |
||
241 | * @param string $sPath |
||
242 | * @returns UrlParserA |
||
243 | */ |
||
244 | 3 | public function addPath($sPath) { |
|
252 | |||
253 | } |
||
254 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.