| Conditions | 18 |
| Paths | 560 |
| Total Lines | 65 |
| Code Lines | 33 |
| 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 |
||
| 157 | public static function getSiteFromUrl(string $url): Site |
||
| 158 | { |
||
| 159 | $sites = Craft::$app->getSites()->getAllSites(); |
||
| 160 | $request = Craft::$app->getRequest(); |
||
| 161 | |||
| 162 | $hostName = parse_url($url, PHP_URL_HOST); |
||
| 163 | $fullUri = trim(parse_url($url, PHP_URL_PATH), '/'); |
||
| 164 | $secure = parse_url($url, PHP_URL_SCHEME) === 'https'; |
||
| 165 | $scheme = $secure ? 'https' : 'http'; |
||
| 166 | $port = $secure ? $request->getSecurePort() : $request->getPort(); |
||
| 167 | |||
| 168 | $scores = []; |
||
| 169 | foreach ($sites as $i => $site) { |
||
| 170 | if (!$site->baseUrl) { |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | |||
| 174 | if (($parsed = parse_url($site->getBaseUrl())) === false) { |
||
| 175 | Craft::warning('Unable to parse the site base URL: ' . $site->baseUrl); |
||
| 176 | continue; |
||
| 177 | } |
||
| 178 | |||
| 179 | // Does the site URL specify a host name? |
||
| 180 | if (!empty($parsed['host']) && $hostName && $parsed['host'] !== $hostName) { |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | // Does the site URL specify a base path? |
||
| 185 | $parsedPath = !empty($parsed['path']) ? self::normalizePath($parsed['path']) : ''; |
||
| 186 | if ($parsedPath && strpos($fullUri . '/', $parsedPath . '/') !== 0) { |
||
| 187 | continue; |
||
| 188 | } |
||
| 189 | |||
| 190 | // It's a possible match! |
||
| 191 | $scores[$i] = 8 + strlen($parsedPath); |
||
| 192 | |||
| 193 | $parsedScheme = !empty($parsed['scheme']) ? strtolower($parsed['scheme']) : $scheme; |
||
| 194 | $parsedPort = $parsed['port'] ?? ($parsedScheme === 'https' ? 443 : 80); |
||
| 195 | |||
| 196 | // Do the ports match? |
||
| 197 | if ($parsedPort == $port) { |
||
| 198 | $scores[$i] += 4; |
||
| 199 | } |
||
| 200 | |||
| 201 | // Do the schemes match? |
||
| 202 | if ($parsedScheme === $scheme) { |
||
| 203 | $scores[$i] += 2; |
||
| 204 | } |
||
| 205 | |||
| 206 | // One Pence point if it's the primary site in case we need a tiebreaker |
||
| 207 | if ($site->primary) { |
||
| 208 | $scores[$i]++; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | if (empty($scores)) { |
||
| 213 | // Default to the primary site |
||
| 214 | return Craft::$app->getSites()->getPrimarySite(); |
||
| 215 | } |
||
| 216 | |||
| 217 | // Sort by scores descending |
||
| 218 | arsort($scores, SORT_NUMERIC); |
||
| 219 | $first = ArrayHelper::firstKey($scores); |
||
| 220 | |||
| 221 | return $sites[$first]; |
||
| 222 | } |
||
| 250 |