| Total Complexity | 49 |
| Total Lines | 242 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SubsiteHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SubsiteHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SubsiteHelper |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var boolean |
||
| 22 | */ |
||
| 23 | protected static $previousState; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected static $previousSubsite; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Return current subsite id (even if module is not installed, which returns 0) |
||
| 32 | * |
||
| 33 | * @return int |
||
| 34 | */ |
||
| 35 | public static function currentSubsiteID() |
||
| 36 | { |
||
| 37 | if (self::usesSubsite() && class_exists(SubsiteState::class)) { |
||
| 38 | return SubsiteState::singleton()->getSubsiteId(); |
||
| 39 | } |
||
| 40 | return 0; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return Subsite|null |
||
| 45 | */ |
||
| 46 | public static function currentSubsite() |
||
| 47 | { |
||
| 48 | $id = self::currentSubsiteID(); |
||
| 49 | if (self::usesSubsite() && class_exists(Subsite::class)) { |
||
| 50 | /** @var Subsite|null */ |
||
| 51 | return DataObject::get_by_id(Subsite::class, $id); |
||
| 52 | } |
||
| 53 | return null; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Do we have the subsite module installed |
||
| 58 | * TODO: check if it might be better to use module manifest instead? |
||
| 59 | * |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public static function usesSubsite() |
||
| 63 | { |
||
| 64 | return class_exists(SubsiteState::class) && class_exists(Subsite::class); |
||
| 65 | } |
||
| 66 | |||
| 67 | public static function safeAbsoluteURL($absUrl) |
||
| 68 | { |
||
| 69 | if (!self::usesSubsite() || !class_exists(Subsite::class)) { |
||
| 70 | return $absUrl; |
||
| 71 | } |
||
| 72 | |||
| 73 | $subsite = SubsiteHelper::currentSubsite(); |
||
| 74 | if ($subsite->hasMethod('getPrimarySubsiteDomain')) { |
||
| 75 | $domain = $subsite->getPrimarySubsiteDomain(); |
||
| 76 | $link = $subsite->domain(); |
||
| 77 | $protocol = $domain->getFullProtocol(); |
||
| 78 | } else { |
||
| 79 | $protocol = Director::protocol(); |
||
| 80 | $link = $subsite->domain(); |
||
| 81 | } |
||
| 82 | $absUrl = preg_replace('/\/\/[^\/]+\//', '//' . $link . '/', $absUrl); |
||
| 83 | $absUrl = preg_replace('/http(s)?:\/\//', $protocol, $absUrl); |
||
| 84 | |||
| 85 | return $absUrl; |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public static function subsiteFilterDisabled() |
||
| 93 | { |
||
| 94 | if (!self::usesSubsite() && class_exists(Subsite::class)) { |
||
| 95 | return true; |
||
| 96 | } |
||
| 97 | return Subsite::$disable_subsite_filter; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Enable subsite filter and store previous state |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public static function enableFilter() |
||
| 106 | { |
||
| 107 | if (!self::usesSubsite() || !class_exists(Subsite::class)) { |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | self::$previousState = Subsite::$disable_subsite_filter; |
||
| 111 | Subsite::$disable_subsite_filter = false; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Disable subsite filter and store previous state |
||
| 116 | * |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | public static function disableFilter() |
||
| 120 | { |
||
| 121 | if (!self::usesSubsite() || !class_exists(Subsite::class)) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | self::$previousState = Subsite::$disable_subsite_filter; |
||
| 125 | Subsite::$disable_subsite_filter = true; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Restore subsite filter based on previous set (set when called enableFilter or disableFilter) |
||
| 130 | */ |
||
| 131 | public static function restoreFilter() |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public static function SubsiteIDFromSession() |
||
| 143 | { |
||
| 144 | $session = Controller::curr()->getRequest()->getSession(); |
||
| 145 | if ($session) { |
||
| 146 | return $session->get('SubsiteID'); |
||
| 147 | } |
||
| 148 | return 0; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Typically call this on PageController::init |
||
| 153 | * This is due to InitStateMiddleware not using session in front end and not persisting get var parameters |
||
| 154 | * |
||
| 155 | * @param HTTPRequest $request |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | public static function forceSubsiteFromRequest(HTTPRequest $request) |
||
| 159 | { |
||
| 160 | $subsiteID = $request->getVar('SubsiteID'); |
||
| 161 | if ($subsiteID) { |
||
| 162 | $request->getSession()->set('ForcedSubsiteID', $subsiteID); |
||
| 163 | } else { |
||
| 164 | $subsiteID = $request->getSession()->get('ForcedSubsiteID'); |
||
| 165 | } |
||
| 166 | if ($subsiteID) { |
||
| 167 | self::changeSubsite($subsiteID, true); |
||
| 168 | } |
||
| 169 | return $subsiteID; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $ID |
||
| 174 | * @param bool $flush |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public static function changeSubsite($ID, $flush = null) |
||
| 178 | { |
||
| 179 | if (!self::usesSubsite() || !class_exists(Subsite::class) || !class_exists(SubsiteState::class)) { |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | self::$previousSubsite = self::currentSubsiteID(); |
||
| 183 | |||
| 184 | // Do this otherwise changeSubsite has no effect if false |
||
| 185 | SubsiteState::singleton()->setUseSessions(true); |
||
| 186 | Subsite::changeSubsite($ID); |
||
| 187 | // This can help avoiding getting static objects like SiteConfig |
||
| 188 | if ($flush !== null && $flush) { |
||
| 189 | DataObject::reset(); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public static function restoreSubsite() |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public static function listSubsites() |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Execute the callback in given subsite |
||
| 217 | * |
||
| 218 | * @param int $ID Subsite ID or 0 for main site |
||
| 219 | * @param callable $cb |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | public static function withSubsite($ID, $cb) |
||
| 223 | { |
||
| 224 | if (!class_exists(SubsiteState::class)) { |
||
| 225 | return; |
||
| 226 | } |
||
| 227 | $currentID = self::currentSubsiteID(); |
||
| 228 | SubsiteState::singleton()->setSubsiteId($ID); |
||
| 229 | $cb(); |
||
| 230 | SubsiteState::singleton()->setSubsiteId($currentID); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Execute the callback in all subsites |
||
| 235 | * |
||
| 236 | * @param callable $cb |
||
| 237 | * @param bool $includeMainSite |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public static function withSubsites($cb, $includeMainSite = true) |
||
| 260 | } |
||
| 261 | } |
||
| 262 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths