| Conditions | 14 |
| Paths | 1529 |
| Total Lines | 70 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 149 | public static function getDomainURL(string $domain, Client $client = null) |
||
| 150 | { |
||
| 151 | $testDomain = $domain; |
||
| 152 | |||
| 153 | // Pings via guzzle |
||
| 154 | $client = $client ?: new Client(); |
||
| 155 | |||
| 156 | $scheme = parse_url($testDomain, PHP_URL_SCHEME); |
||
| 157 | |||
| 158 | // if user entered a URL -> test if available |
||
| 159 | if ($scheme) { |
||
| 160 | try { |
||
| 161 | $testURL = $testDomain; |
||
| 162 | $response = $client->request('GET', $testURL, ['verify' => false]); |
||
| 163 | if ($response->getStatusCode() === 200) { |
||
| 164 | return $testURL; |
||
| 165 | } |
||
| 166 | } catch (\Exception $e) { |
||
| 167 | // if not available, remove scheme from domain |
||
| 168 | // scheme = https; + 3 for :// |
||
| 169 | $testDomain = substr($domain, strlen($scheme) + 3); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | // Domain is available via https:// |
||
| 174 | try { |
||
| 175 | $testURL = 'https://'.$testDomain; |
||
| 176 | $response = $client->request('GET', $testURL, ['verify' => false]); |
||
| 177 | if ($response->getStatusCode() === 200) { |
||
| 178 | return $testURL; |
||
| 179 | } |
||
| 180 | } catch (\Exception $e) { |
||
| 181 | } |
||
| 182 | |||
| 183 | // Domain is available via http:// |
||
| 184 | try { |
||
| 185 | $testURL = 'http://'.$testDomain; |
||
| 186 | $response = $client->request('GET', $testURL, ['verify' => false]); |
||
| 187 | if ($response->getStatusCode() === 200) { |
||
| 188 | return $testURL; |
||
| 189 | } |
||
| 190 | } catch (\Exception $e) { |
||
| 191 | } |
||
| 192 | |||
| 193 | // Domain is available with or without www |
||
| 194 | // if www. is there, than remove it, otherwise add it |
||
| 195 | $testDomain = substr($testDomain, 0, 4) === 'www.' ? substr($testDomain, 4) : 'www.'.$testDomain; |
||
| 196 | |||
| 197 | try { |
||
| 198 | $testURL = 'https://'.$testDomain; |
||
| 199 | $response = $client->request('GET', $testURL, ['verify' => false]); |
||
| 200 | if ($response->getStatusCode() === 200) { |
||
| 201 | return collect([ |
||
| 202 | 'notAvailable' => $domain, |
||
| 203 | 'alternativeAvailable' => $testDomain, |
||
| 204 | ]); |
||
| 205 | } |
||
| 206 | } catch (\Exception $e) { |
||
| 207 | } |
||
| 208 | |||
| 209 | try { |
||
| 210 | $testURL = 'http://'.$testDomain; |
||
| 211 | $response = $client->request('GET', $testURL, ['verify' => false]); |
||
| 212 | if ($response->getStatusCode() === 200) { |
||
| 213 | return collect([ |
||
| 214 | 'notAvailable' => $domain, |
||
| 215 | 'alternativeAvailable' => $testDomain, |
||
| 216 | ]); |
||
| 217 | } |
||
| 218 | } catch (\Exception $e) { |
||
| 219 | } |
||
| 222 |