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 |
||
25 | class Url |
||
26 | { |
||
27 | /** |
||
28 | * All parts of the URL format, as returned by parse_url. |
||
29 | * scheme://user:pass@host:port/path?query#fragment |
||
30 | */ |
||
31 | public $scheme, $user, $pass, $host, $port, $path, $fragment; |
||
1 ignored issue
–
show
|
|||
32 | private $query; |
||
33 | |||
34 | /** |
||
35 | * @param string $url The URL to parse, the query part will remain a string. |
||
36 | * @param QueryInterface queryObject Optional. An object that parses the query string. |
||
37 | */ |
||
38 | 14 | public function __construct($url, $queryObject = null) |
|
48 | |||
49 | /** |
||
50 | * @return string |
||
51 | */ |
||
52 | 12 | public function __toString() |
|
70 | |||
71 | /** |
||
72 | * @param $name |
||
73 | * @return mixed |
||
74 | */ |
||
75 | 5 | public function __get($name) |
|
86 | |||
87 | /** |
||
88 | * @param $name |
||
89 | * @param $value |
||
90 | */ |
||
91 | public function __set($name, $value) |
||
106 | |||
107 | /** |
||
108 | * |
||
109 | */ |
||
110 | public function __clone() |
||
116 | |||
117 | /** |
||
118 | * @param $components |
||
119 | * @param $validComponents |
||
120 | */ |
||
121 | private function importUrlComponents($components, $validComponents) |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | 12 | private function getSchemeAndAuthority() |
|
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | 10 | private function getScheme() |
|
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | 10 | private function getAuthority() |
|
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | 10 | private function getUser() |
|
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | 1 | private function getPassword() |
|
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | 10 | private function getPort() |
|
176 | |||
177 | /** |
||
178 | * @return mixed|string |
||
179 | */ |
||
180 | 12 | private function getPath() |
|
193 | |||
194 | /** |
||
195 | * @return mixed|string |
||
196 | */ |
||
197 | 1 | private function getFilePath() |
|
207 | |||
208 | /** |
||
209 | * @return string |
||
210 | */ |
||
211 | 12 | private function getQuery() |
|
218 | |||
219 | /** |
||
220 | * @return string |
||
221 | */ |
||
222 | 1 | private function getLdapQuery() |
|
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | 12 | private function getFragment() |
|
237 | |||
238 | } |
||
239 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.