Total Complexity | 41 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Domain 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.
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 Domain, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Domain |
||
24 | { |
||
25 | /** |
||
26 | * Domain::$string |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $string; |
||
31 | |||
32 | /** |
||
33 | * Domain::$origin |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $origin; |
||
38 | |||
39 | /** |
||
40 | * Domain::$scheme |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $scheme = 'http'; |
||
45 | |||
46 | /** |
||
47 | * Domain::$www |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | protected $www = false; |
||
52 | |||
53 | /** |
||
54 | * Domain::$port |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | protected $port = 80; |
||
59 | |||
60 | /** |
||
61 | * Domain::$parentDomain |
||
62 | * |
||
63 | * @var string|null |
||
64 | */ |
||
65 | protected $parentDomain = null; |
||
66 | |||
67 | /** |
||
68 | * Domain::$subDomains |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $subDomains = []; |
||
73 | |||
74 | /** |
||
75 | * Domain::$tlds |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $tlds = []; |
||
80 | |||
81 | /** |
||
82 | * Domain::$path |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | protected $path; |
||
87 | |||
88 | // ------------------------------------------------------------------------ |
||
89 | |||
90 | /** |
||
91 | * Domain::__construct |
||
92 | * |
||
93 | * @param string|null $string |
||
94 | */ |
||
95 | public function __construct($string = null) |
||
96 | { |
||
97 | $this->origin = isset($_SERVER[ 'HTTP_HOST' ]) |
||
98 | ? $_SERVER[ 'HTTP_HOST' ] |
||
99 | : $_SERVER[ 'SERVER_NAME' ]; |
||
100 | $this->scheme = is_https() |
||
101 | ? 'https' |
||
102 | : 'http'; |
||
103 | |||
104 | $paths = explode('.php', $_SERVER[ 'PHP_SELF' ]); |
||
105 | $paths = explode('/', trim($paths[ 0 ], '/')); |
||
106 | array_pop($paths); |
||
107 | |||
108 | $this->path = empty($paths) |
||
109 | ? null |
||
110 | : implode('/', $paths); |
||
111 | |||
112 | if (isset($string)) { |
||
113 | $this->string = trim($string, '/'); |
||
114 | $metadata = parse_url($string); |
||
115 | $metadata[ 'path' ] = empty($metadata[ 'path' ]) |
||
116 | ? null |
||
117 | : $metadata[ 'path' ]; |
||
118 | |||
119 | $this->scheme = empty($metadata[ 'scheme' ]) |
||
120 | ? $this->scheme |
||
121 | : $metadata[ 'scheme' ]; |
||
122 | |||
123 | if ($metadata[ 'path' ] === $this->string) { |
||
124 | $paths = explode('/', $this->string); |
||
125 | $this->origin = $paths[ 0 ]; |
||
126 | |||
127 | $this->path = implode('/', array_slice($paths, 1)); |
||
128 | } elseif (isset($metadata[ 'host' ])) { |
||
129 | $this->path = trim($metadata[ 'path' ]); |
||
130 | $this->origin = $metadata[ 'host' ]; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | $directories = explode('/', str_replace('\\', '/', dirname($_SERVER[ 'SCRIPT_FILENAME' ]))); |
||
135 | $paths = explode('/', $this->path); |
||
136 | $paths = array_intersect($paths, $directories); |
||
137 | |||
138 | $this->path = '/' . trim(implode('/', $paths), '/'); |
||
139 | |||
140 | if (strpos($this->origin, 'www') !== false) { |
||
141 | $this->www = true; |
||
142 | $this->origin = ltrim($this->origin, 'www.'); |
||
143 | } |
||
144 | |||
145 | if (preg_match('/(:)([0-9]+)/', $this->string, $matches)) { |
||
146 | $this->port = $matches[ 2 ]; |
||
147 | } |
||
148 | |||
149 | if (filter_var($this->origin, FILTER_VALIDATE_IP) !== false) { |
||
150 | $tlds = [$this->origin]; |
||
151 | } else { |
||
152 | $tlds = explode('.', $this->origin); |
||
153 | } |
||
154 | |||
155 | if (count($tlds) > 1) { |
||
156 | foreach ($tlds as $key => $tld) { |
||
157 | if (strlen($tld) <= 3 AND $key >= 1) { |
||
158 | $this->tlds[] = $tld; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | if (empty($this->tlds)) { |
||
163 | $this->tlds[] = end($tlds); |
||
164 | } |
||
165 | |||
166 | $this->tld = '.' . implode('.', $this->tlds); |
||
|
|||
167 | |||
168 | $this->subDomains = array_diff($tlds, $this->tlds); |
||
169 | $this->subDomains = count($this->subDomains) == 0 |
||
170 | ? $this->tlds |
||
171 | : $this->subDomains; |
||
172 | |||
173 | $this->parentDomain = end($this->subDomains); |
||
174 | array_pop($this->subDomains); |
||
175 | |||
176 | $this->parentDomain = implode('.', array_slice($this->subDomains, 1)) |
||
177 | . '.' |
||
178 | . $this->parentDomain |
||
179 | . $this->tld; |
||
180 | $this->parentDomain = ltrim($this->parentDomain, '.'); |
||
181 | |||
182 | if (count($this->subDomains) > 0) { |
||
183 | $this->subDomain = reset($this->subDomains); |
||
184 | } |
||
185 | } else { |
||
186 | $this->parentDomain = $this->origin; |
||
187 | } |
||
188 | |||
189 | $ordinalEnds = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']; |
||
190 | |||
191 | foreach ($this->subDomains as $key => $subdomain) { |
||
192 | $ordinalNumber = count($tlds) - $key; |
||
193 | |||
194 | if ((($ordinalNumber % 100) >= 11) && (($ordinalNumber % 100) <= 13)) { |
||
195 | $ordinalKey = $ordinalNumber . 'th'; |
||
196 | } else { |
||
197 | $ordinalKey = $ordinalNumber . $ordinalEnds[ $ordinalNumber % 10 ]; |
||
198 | } |
||
199 | |||
200 | $this->subDomains[ $ordinalKey ] = $subdomain; |
||
201 | |||
202 | unset($this->subDomains[ $key ]); |
||
203 | } |
||
204 | |||
205 | foreach ($this->tlds as $key => $tld) { |
||
206 | $ordinalNumber = count($this->tlds) - $key; |
||
207 | |||
208 | if ((($ordinalNumber % 100) >= 11) && (($ordinalNumber % 100) <= 13)) { |
||
209 | $ordinalKey = $ordinalNumber . 'th'; |
||
210 | } else { |
||
211 | $ordinalKey = $ordinalNumber . $ordinalEnds[ $ordinalNumber % 10 ]; |
||
212 | } |
||
213 | |||
214 | $this->tlds[ $ordinalKey ] = $tld; |
||
215 | |||
216 | unset($this->tlds[ $key ]); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | // ------------------------------------------------------------------------ |
||
221 | |||
222 | /** |
||
223 | * Domain::getString |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getString() |
||
228 | { |
||
229 | return $this->string; |
||
230 | } |
||
231 | |||
232 | // ------------------------------------------------------------------------ |
||
233 | |||
234 | /** |
||
235 | * Domain::getOrigin |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getOrigin() |
||
240 | { |
||
241 | return $this->origin; |
||
242 | } |
||
243 | |||
244 | // ------------------------------------------------------------------------ |
||
245 | |||
246 | /** |
||
247 | * Domain::getScheme |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getScheme() |
||
254 | } |
||
255 | |||
256 | // ------------------------------------------------------------------------ |
||
257 | |||
258 | /** |
||
259 | * Domain::isWWWW |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isWWW() |
||
264 | { |
||
265 | return $this->www; |
||
266 | } |
||
267 | |||
268 | // ------------------------------------------------------------------------ |
||
269 | |||
270 | /** |
||
271 | * Domain::getIpAddress |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getIpAddress() |
||
276 | { |
||
277 | return gethostbyname($this->origin); |
||
278 | } |
||
279 | |||
280 | // ------------------------------------------------------------------------ |
||
281 | |||
282 | /** |
||
283 | * Domain::getPort |
||
284 | * |
||
285 | * @return int |
||
286 | */ |
||
287 | public function getPort() |
||
288 | { |
||
289 | return $this->port; |
||
290 | } |
||
291 | |||
292 | // ------------------------------------------------------------------------ |
||
293 | |||
294 | /** |
||
295 | * Domain::getParentDomain |
||
296 | * |
||
297 | * @return string|null |
||
298 | */ |
||
299 | public function getParentDomain() |
||
300 | { |
||
301 | return $this->parentDomain; |
||
302 | } |
||
303 | |||
304 | // ------------------------------------------------------------------------ |
||
305 | |||
306 | /** |
||
307 | * Domain::getSubDomain |
||
308 | * |
||
309 | * @param string $level |
||
310 | * |
||
311 | * @return bool|mixed |
||
312 | */ |
||
313 | public function getSubDomain($level = '3rd') |
||
314 | { |
||
315 | if (isset($this->subDomains[ $level ])) { |
||
316 | return $this->subDomains[ $level ]; |
||
317 | } |
||
318 | |||
319 | return false; |
||
320 | } |
||
321 | |||
322 | // ------------------------------------------------------------------------ |
||
323 | |||
324 | /** |
||
325 | * Domain::getSubDomains |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | public function getSubDomains() |
||
330 | { |
||
331 | return $this->subDomains; |
||
332 | } |
||
333 | |||
334 | // ------------------------------------------------------------------------ |
||
335 | |||
336 | /** |
||
337 | * Domain::getTotalSubDOmains |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | public function getTotalSubDomains() |
||
342 | { |
||
343 | return count($this->subDomains); |
||
344 | } |
||
345 | |||
346 | // ------------------------------------------------------------------------ |
||
347 | |||
348 | /** |
||
349 | * Domain::getTld |
||
350 | * |
||
351 | * @param string|null $level |
||
352 | * |
||
353 | * @return bool|mixed|string |
||
354 | */ |
||
355 | public function getTld($level = null) |
||
364 | } |
||
365 | |||
366 | // ------------------------------------------------------------------------ |
||
367 | |||
368 | /** |
||
369 | * Domain::getTlds |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | public function getTlds() |
||
374 | { |
||
375 | return $this->tlds; |
||
376 | } |
||
377 | |||
378 | // ------------------------------------------------------------------------ |
||
379 | |||
380 | /** |
||
381 | * Domain::getTotalTlds |
||
382 | * |
||
383 | * @return int |
||
384 | */ |
||
385 | public function getTotalTlds() |
||
388 | } |
||
389 | } |