| Conditions | 17 |
| Paths | 280 |
| Total Lines | 61 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 35 | public static function resolve(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): mixed |
||
| 36 | { |
||
| 37 | // If our source is an Element, extract the URI and siteId from it |
||
| 38 | if ($source instanceof Element) { |
||
| 39 | /** Element $source */ |
||
| 40 | $uri = $source->uri; |
||
| 41 | $siteId = $source->siteId; |
||
| 42 | } else { |
||
| 43 | // Otherwise, use the passed in arguments, or defaults |
||
| 44 | $uri = $arguments['uri'] ?? '/'; |
||
| 45 | $siteId = $arguments['siteId'] ?? null; |
||
| 46 | if (isset($arguments['site'])) { |
||
| 47 | $site = Craft::$app->getSites()->getSiteByHandle($arguments['site']); |
||
| 48 | if ($site !== null) { |
||
| 49 | $siteId = $site->id; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | $uri = trim($uri === '/' ? '__home__' : $uri); |
||
| 54 | |||
| 55 | $redirect = null; |
||
| 56 | |||
| 57 | // Strip the query string if `alwaysStripQueryString` is set |
||
| 58 | if (Retour::$settings->alwaysStripQueryString) { |
||
| 59 | $uri = UrlHelper::stripQueryString($uri); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!Retour::$plugin->redirects->excludeUri($uri)) { |
||
| 63 | $redirect = Retour::$plugin->redirects->findRedirectMatch($uri, $uri, $siteId); |
||
| 64 | |||
| 65 | if ($redirect === null && Craft::$app->getElements()->getElementByUri(trim($uri, '/'), $siteId) === null) { |
||
| 66 | // Set the `site` virtual field |
||
| 67 | $redirect['site'] = null; |
||
| 68 | $redirect['siteId'] = $siteId; |
||
| 69 | if (isset($redirect['siteId']) && (int)$redirect['siteId'] !== 0) { |
||
| 70 | $site = Craft::$app->getSites()->getSiteById((int)$redirect['siteId']); |
||
| 71 | if ($site !== null) { |
||
| 72 | $redirect['site'] = $site->handle; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | // Increment the stats |
||
| 76 | Retour::$plugin->statistics->incrementStatistics($uri, false, $siteId); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | if ($redirect !== null && isset($redirect['redirectDestUrl'])) { |
||
| 80 | $dest = $redirect['redirectDestUrl']; |
||
| 81 | $path = $redirect['redirectDestUrl']; |
||
| 82 | // Combine the URL and path together, merging them as appropriate |
||
| 83 | try { |
||
| 84 | if (!UrlHelper::isAbsoluteUrl($dest) && !UrlHelper::pathHasSitePrefix($path)) { |
||
| 85 | $dest = UrlHelper::siteUrl('/', null, null, $siteId); |
||
| 86 | $dest = UrlHelper::mergeUrlWithPath($dest, $path); |
||
| 87 | $dest = parse_url($dest, PHP_URL_PATH); |
||
| 88 | } |
||
| 89 | } catch (Throwable $e) { |
||
| 90 | // That's ok |
||
| 91 | } |
||
| 92 | $redirect['redirectDestUrl'] = $dest; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $redirect; |
||
| 96 | } |
||
| 117 |