| Conditions | 14 |
| Paths | 1529 |
| Total Lines | 76 |
| Code Lines | 44 |
| 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 |
||
| 146 | public static function getDomainURL(string $domain, Client $client = null) |
||
| 147 | { |
||
| 148 | $testDomain = $domain; |
||
| 149 | |||
| 150 | // Pings via guzzle |
||
| 151 | $client = $client ?: new Client([ |
||
| 152 | 'headers' => [ |
||
| 153 | 'User-Agent' => config('app.userAgent'), |
||
| 154 | ], |
||
| 155 | 'timeout' => 25, |
||
| 156 | 'verify' => false, |
||
| 157 | ]); |
||
| 158 | |||
| 159 | $scheme = parse_url($testDomain, PHP_URL_SCHEME); |
||
| 160 | |||
| 161 | // if user entered a URL -> test if available |
||
| 162 | if ($scheme) { |
||
| 163 | try { |
||
| 164 | $testURL = $testDomain; |
||
| 165 | $response = $client->request('GET', $testURL); |
||
| 166 | if ($response->getStatusCode() === 200) { |
||
| 167 | return $testURL; |
||
| 168 | } |
||
| 169 | } catch (\Exception $e) { |
||
| 170 | // if not available, remove scheme from domain |
||
| 171 | // scheme = https; + 3 for :// |
||
| 172 | $testDomain = substr($domain, strlen($scheme) + 3); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | // Domain is available via https:// |
||
| 177 | try { |
||
| 178 | $testURL = 'https://'.$testDomain; |
||
| 179 | $response = $client->request('GET', $testURL); |
||
| 180 | if ($response->getStatusCode() === 200) { |
||
| 181 | return $testURL; |
||
| 182 | } |
||
| 183 | } catch (\Exception $e) { |
||
| 184 | } |
||
| 185 | |||
| 186 | // Domain is available via http:// |
||
| 187 | try { |
||
| 188 | $testURL = 'http://'.$testDomain; |
||
| 189 | $response = $client->request('GET', $testURL); |
||
| 190 | if ($response->getStatusCode() === 200) { |
||
| 191 | return $testURL; |
||
| 192 | } |
||
| 193 | } catch (\Exception $e) { |
||
| 194 | } |
||
| 195 | |||
| 196 | // Domain is available with or without www |
||
| 197 | // if www. is there, than remove it, otherwise add it |
||
| 198 | $testDomain = substr($testDomain, 0, 4) === 'www.' ? substr($testDomain, 4) : 'www.'.$testDomain; |
||
| 199 | |||
| 200 | try { |
||
| 201 | $testURL = 'https://'.$testDomain; |
||
| 202 | $response = $client->request('GET', $testURL); |
||
| 203 | if ($response->getStatusCode() === 200) { |
||
| 204 | return collect([ |
||
| 205 | 'notAvailable' => $domain, |
||
| 206 | 'alternativeAvailable' => $testDomain, |
||
| 207 | ]); |
||
| 208 | } |
||
| 209 | } catch (\Exception $e) { |
||
| 210 | } |
||
| 211 | |||
| 212 | try { |
||
| 213 | $testURL = 'http://'.$testDomain; |
||
| 214 | $response = $client->request('GET', $testURL); |
||
| 215 | if ($response->getStatusCode() === 200) { |
||
| 216 | return collect([ |
||
| 217 | 'notAvailable' => $domain, |
||
| 218 | 'alternativeAvailable' => $testDomain, |
||
| 219 | ]); |
||
| 220 | } |
||
| 221 | } catch (\Exception $e) { |
||
| 222 | } |
||
| 225 |