| Conditions | 11 |
| Paths | 120 |
| Total Lines | 52 |
| Code Lines | 32 |
| 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 |
||
| 69 | public static function setMultiSiteVariables($siteHandle, &$siteId, array &$variables): void |
||
| 70 | { |
||
| 71 | // Enabled sites |
||
| 72 | $sites = Craft::$app->getSites(); |
||
| 73 | if (Craft::$app->getIsMultiSite()) { |
||
| 74 | // Set defaults based on the section settings |
||
| 75 | $variables['enabledSiteIds'] = []; |
||
| 76 | $variables['siteIds'] = []; |
||
| 77 | |||
| 78 | foreach ($sites->getEditableSiteIds() as $editableSiteId) { |
||
| 79 | $variables['enabledSiteIds'][] = $editableSiteId; |
||
| 80 | $variables['siteIds'][] = $editableSiteId; |
||
| 81 | } |
||
| 82 | |||
| 83 | // Make sure the $siteId they are trying to edit is in our array of editable sites |
||
| 84 | if (!in_array($siteId, $variables['enabledSiteIds'], false)) { |
||
| 85 | if (!empty($variables['enabledSiteIds'])) { |
||
| 86 | if ($siteId !== 0) { |
||
| 87 | $siteId = reset($variables['enabledSiteIds']); |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | self::requirePermission('editSite:' . $siteId); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | // Set the currentSiteId and currentSiteHandle |
||
| 95 | $variables['currentSiteId'] = empty($siteId) ? 0 : $siteId; |
||
| 96 | $variables['currentSiteHandle'] = empty($siteHandle) |
||
| 97 | ? Craft::$app->getSites()->currentSite->handle |
||
| 98 | : $siteHandle; |
||
| 99 | |||
| 100 | // Page title |
||
| 101 | $variables['showSites'] = ( |
||
| 102 | Craft::$app->getIsMultiSite() && |
||
| 103 | count($variables['enabledSiteIds']) |
||
| 104 | ); |
||
| 105 | |||
| 106 | if ($variables['showSites']) { |
||
| 107 | if ($variables['currentSiteId'] === 0) { |
||
| 108 | $variables['sitesMenuLabel'] = Craft::t( |
||
| 109 | 'retour', |
||
| 110 | 'All Sites' |
||
| 111 | ); |
||
| 112 | } else { |
||
| 113 | $variables['sitesMenuLabel'] = Craft::t( |
||
| 114 | 'site', |
||
| 115 | $sites->getSiteById((int)$variables['currentSiteId'])->name |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $variables['currentSiteId'] = 0; |
||
| 120 | $variables['sitesMenuLabel'] = ''; |
||
| 121 | } |
||
| 163 |