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; |
||
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) |
|
39 | { |
||
40 | $componentList = [ |
||
41 | 14 | 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment' |
|
42 | ]; |
||
43 | 14 | $this->importUrlComponents( parse_url( $url ), $componentList ); |
|
44 | 14 | if ( isset( $queryObject ) ) { |
|
45 | 14 | $this->query = $queryObject->import( $this->query ); |
|
46 | } |
||
47 | 14 | } |
|
48 | |||
49 | /** |
||
50 | * @return string |
||
51 | */ |
||
52 | 12 | public function __toString() |
|
53 | { |
||
54 | 12 | switch ($this->scheme) { |
|
55 | case 'file': |
||
56 | 1 | return $this->getScheme() . '//' . $this->host . $this->getFilePath(); |
|
57 | break; |
||
58 | case 'mailto': |
||
59 | case 'news': |
||
60 | 1 | return ( $this->path ? $this->getScheme() . $this->getPath() : '' ); |
|
61 | break; |
||
62 | 12 | case 'ldap': |
|
63 | 1 | return $this->getSchemeAndAuthority() . $this->getPath() . $this->getLdapQuery(); |
|
64 | break; |
||
65 | default: |
||
66 | 12 | return $this->getSchemeAndAuthority() . $this->getPath() . $this->getQuery() . $this->getFragment(); |
|
67 | break; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param $name |
||
73 | * @return mixed |
||
74 | */ |
||
75 | 5 | public function __get($name) |
|
76 | { |
||
77 | 5 | switch ( (string) $name ) { |
|
78 | case 'password': |
||
79 | return $this->pass; |
||
80 | break; |
||
81 | case 'query': |
||
82 | 5 | return $this->query; |
|
83 | break; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param $name |
||
89 | * @param $value |
||
90 | */ |
||
91 | public function __set($name, $value) |
||
92 | { |
||
93 | switch ( (string) $name ) { |
||
94 | case 'password': |
||
95 | $this->pass = $value; |
||
96 | break; |
||
97 | case 'query': |
||
98 | if ( is_object( $this->query ) ) { |
||
99 | $this->query->reset()->import( $value ); |
||
100 | } else { |
||
101 | $this->query = $value; |
||
102 | } |
||
103 | break; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * |
||
109 | */ |
||
110 | public function __clone() |
||
111 | { |
||
112 | if ( is_object( $this->query ) ) { |
||
113 | $this->query = clone $this->query; |
||
114 | } |
||
115 | } |
||
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() |
|
181 | { |
||
182 | 12 | if (!$this->path) { |
|
183 | 1 | return ''; |
|
184 | } |
||
185 | 12 | $path = $this->path; |
|
186 | 12 | if ( $this->host && ( !$path || $path[0] !== '/' ) ) { |
|
187 | // note: if a host is set, the path _must_ be made absolute or the URL will be invalid |
||
188 | $path = '/' . $path; |
||
189 | } |
||
190 | // urlencode encodes too many characters for the path part, so we decode them back to get readable urls. |
||
191 | 12 | return str_replace( [ '%3D', '%2B', '%3A', '%40' ], [ '=', '+', ':', '@' ], join( '/', array_map( 'urlencode', explode( '/', $path ) ) ) ); |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return mixed|string |
||
196 | */ |
||
197 | 1 | private function getFilePath() |
|
198 | { |
||
199 | // in the file: scheme, a path must start with a '/' even if no host is set. This contradicts with the email: scheme. |
||
200 | 1 | $path = $this->getPath(); |
|
201 | 1 | if ($path && $path[0]!=='/') { |
|
202 | 1 | $path = '/' . $path; |
|
203 | } |
||
204 | |||
205 | 1 | return $path; |
|
206 | } |
||
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 |