| Total Complexity | 74 |
| Total Lines | 296 |
| Duplicated Lines | 0 % |
| Changes | 21 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Loader 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 Loader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Loader extends Component |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Loads the configurator and field manipulator code in all the |
||
| 36 | * core supported contexts as well as providing a hook for |
||
| 37 | * third-party contexts. |
||
| 38 | * |
||
| 39 | * @throws InvalidConfigException |
||
| 40 | */ |
||
| 41 | public function run() |
||
| 42 | { |
||
| 43 | $request = Craft::$app->getRequest(); |
||
| 44 | |||
| 45 | // Check the conditions are right to run |
||
| 46 | if ($request->getIsCpRequest() && !$request->getAcceptsJson()) |
||
| 47 | { |
||
| 48 | |||
| 49 | $segments = Craft::$app->request->getSegments(); |
||
| 50 | $entries = Craft::$app->getEntries(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Check third party plugin support |
||
| 54 | */ |
||
| 55 | $commercePlugin = Craft::$app->plugins->getPlugin('commerce'); |
||
| 56 | $calendarPlugin = Craft::$app->plugins->getPlugin('calendar'); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Work out the context for the block type groups configuration |
||
| 60 | */ |
||
| 61 | // Entry types |
||
| 62 | if (\count($segments) === 5 |
||
| 63 | && $segments[0] === 'settings' |
||
| 64 | && $segments[1] === 'sections' |
||
| 65 | && $segments[3] === 'entrytypes' |
||
| 66 | && $segments[4] !== 'new' |
||
| 67 | ) |
||
| 68 | { |
||
| 69 | $uid = Db::uidById('{{%entrytypes}}', $segments[4]); |
||
| 70 | $this->configurator('#fieldlayoutform', 'entrytype:'.$uid); |
||
| 71 | } |
||
| 72 | |||
| 73 | // Category groups |
||
| 74 | if (\count($segments) === 3 |
||
| 75 | && $segments[0] === 'settings' |
||
| 76 | && $segments[1] === 'categories' |
||
| 77 | && $segments[2] !== 'new' |
||
| 78 | ) |
||
| 79 | { |
||
| 80 | $uid = Db::uidById('{{%categorygroups}}', $segments[2]); |
||
| 81 | $this->configurator('#fieldlayoutform', 'categorygroup:'.$uid); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Global sets |
||
| 85 | if (\count($segments) === 3 |
||
| 86 | && $segments[0] === 'settings' |
||
| 87 | && $segments[1] === 'globals' |
||
| 88 | && $segments[2] !== 'new' |
||
| 89 | ) |
||
| 90 | { |
||
| 91 | $uid = Db::uidById('{{%globalsets}}', $segments[2]); |
||
| 92 | $this->configurator('#fieldlayoutform', 'globalset:'.$uid); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Users |
||
| 96 | if (\count($segments) === 2 |
||
| 97 | && $segments[0] === 'settings' |
||
| 98 | && $segments[1] === 'users' |
||
| 99 | ) |
||
| 100 | { |
||
| 101 | $this->configurator('#fieldlayoutform', 'users'); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Solpace Calendar |
||
| 105 | if (\count($segments) === 3 |
||
| 106 | && $segments[0] === 'calendar' |
||
| 107 | && $segments[1] === 'calendars' |
||
| 108 | && $segments[2] !== 'new' |
||
| 109 | && $calendarPlugin |
||
| 110 | ) |
||
| 111 | { |
||
| 112 | $calendarService = new \Solspace\Calendar\Services\CalendarsService(); |
||
|
|
|||
| 113 | if ($calendarService) { |
||
| 114 | $calendar = $calendarService->getCalendarByHandle(end($segments)); |
||
| 115 | if ($calendar) { |
||
| 116 | $uid = Db::uidById(\Solspace\Calendar\Records\CalendarRecord::TABLE, $calendar->id); |
||
| 117 | $this->configurator('#fieldlayoutform', 'calendar:'.$uid); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // Commerce |
||
| 123 | if (\count($segments) === 4 |
||
| 124 | && $segments[0] === 'commerce' |
||
| 125 | && $segments[1] === 'settings' |
||
| 126 | && $segments[2] === 'producttypes' |
||
| 127 | && $segments[3] !== 'new' |
||
| 128 | && $commercePlugin |
||
| 129 | ) { |
||
| 130 | $productTypesService = new \craft\commerce\services\ProductTypes(); |
||
| 131 | if ($productTypesService) { |
||
| 132 | $productType = $productTypesService->getProductTypeById($segments[3]); |
||
| 133 | if ($productType) { |
||
| 134 | $this->configurator('#fieldlayoutform', 'producttype:'.$productType->uid); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Work out the context for the Matrix field manipulation |
||
| 141 | */ |
||
| 142 | // Global |
||
| 143 | $context = 'global'; |
||
| 144 | $versioned = false; |
||
| 145 | |||
| 146 | // Entry types |
||
| 147 | if (\count($segments) >= 3 && $segments[0] === 'entries') { |
||
| 148 | |||
| 149 | if ($segments[2] === 'new') { |
||
| 150 | /** @var Section $section */ |
||
| 151 | $section = Craft::$app->getSections()->getSectionByHandle($segments[1]); |
||
| 152 | $sectionEntryTypes = $section->getEntryTypes(); |
||
| 153 | $entryType = reset($sectionEntryTypes); |
||
| 154 | } else { |
||
| 155 | $entryId = (integer)explode('-', $segments[2])[0]; |
||
| 156 | |||
| 157 | // Check if we have a site handle in the URL |
||
| 158 | if (isset($segments[3])) { |
||
| 159 | // If we do, get the site and fetch the entry with it |
||
| 160 | $site = Craft::$app->getSites()->getSiteByHandle($segments[3]); |
||
| 161 | $entry = $entries->getEntryById($entryId, $site !== null ? $site->id : null); |
||
| 162 | } else { |
||
| 163 | $entry = $entries->getEntryById($entryId); |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($entry) |
||
| 167 | { |
||
| 168 | $entryType = $entry->type; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | $revisionId = $request->getParam('revisionId'); |
||
| 173 | if ($revisionId) { |
||
| 174 | $versioned = true; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (isset($entryType) && $entryType) { |
||
| 178 | $context = 'entrytype:'.$entryType->uid; |
||
| 179 | } |
||
| 180 | |||
| 181 | } |
||
| 182 | // Category groups |
||
| 183 | else if (\count($segments) >= 3 && $segments[0] === 'categories') |
||
| 184 | { |
||
| 185 | $group = Craft::$app->getCategories()->getGroupByHandle($segments[1]); |
||
| 186 | if ($group) |
||
| 187 | { |
||
| 188 | $context = 'categorygroup:'.$group->uid; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | // Global sets |
||
| 192 | else if (\count($segments) >= 2 && $segments[0] === 'globals') |
||
| 193 | { |
||
| 194 | $set = Craft::$app->getGlobals()->getSetByHandle(end($segments)); |
||
| 195 | if ($set) |
||
| 196 | { |
||
| 197 | $context = 'globalset:'.$set->uid; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | // Users |
||
| 201 | else if ((\count($segments) === 1 && $segments[0] === 'myaccount') || (\count($segments) == 2 && $segments[0] === 'users')) |
||
| 202 | { |
||
| 203 | $context = 'users'; |
||
| 204 | } |
||
| 205 | // Solspace Calendar |
||
| 206 | else if (\count($segments) >= 4 |
||
| 207 | && $segments[0] === 'calendar' |
||
| 208 | && $segments[1] === 'events' |
||
| 209 | && $calendarPlugin |
||
| 210 | ) { |
||
| 211 | |||
| 212 | if ($segments[2] === 'new') { |
||
| 213 | $calendarService = new \Solspace\Calendar\Services\CalendarsService(); |
||
| 214 | $calendar = $calendarService->getCalendarByHandle($segments[3]); |
||
| 215 | if ($calendar) { |
||
| 216 | $uid = Db::uidById(\Solspace\Calendar\Records\CalendarRecord::TABLE, $calendar->id); |
||
| 217 | $context = 'calendar:'.$uid; |
||
| 218 | } |
||
| 219 | } else { |
||
| 220 | $calendarEventsService = new \Solspace\Calendar\Services\EventsService(); |
||
| 221 | if ($calendarEventsService) { |
||
| 222 | $event = $calendarEventsService->getEventById($segments[2]); |
||
| 223 | if ($event) { |
||
| 224 | $uid = Db::uidById(\Solspace\Calendar\Records\CalendarRecord::TABLE, $event->getCalendar()->id); |
||
| 225 | $context = 'calendar:'.$uid; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | // Commerce |
||
| 231 | else if (\count($segments) >= 3 |
||
| 232 | && $segments[0] === 'commerce' |
||
| 233 | && $segments[1] === 'products' |
||
| 234 | && $commercePlugin |
||
| 235 | ) { |
||
| 236 | $productTypesService = new \craft\commerce\services\ProductTypes(); |
||
| 237 | if ($productTypesService) { |
||
| 238 | $productType = $productTypesService->getProductTypeByHandle($segments[2]); |
||
| 239 | if ($productType) { |
||
| 240 | $context = 'producttype:'.$productType->uid; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | // Run the field manipulation code |
||
| 246 | $this->fieldManipulator($context, $versioned); |
||
| 247 | |||
| 248 | } |
||
| 249 | |||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Loads a Spoon.Configurator() for the correct context |
||
| 254 | * |
||
| 255 | * @param $container |
||
| 256 | * @param $context |
||
| 257 | * |
||
| 258 | * @throws InvalidConfigException |
||
| 259 | */ |
||
| 260 | public function configurator($container, $context) |
||
| 273 | |||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Loads a Spoon.FieldManipulator() for the correct context |
||
| 278 | * |
||
| 279 | * @param $context |
||
| 280 | * @param bool $versioned |
||
| 281 | * |
||
| 282 | * @throws InvalidConfigException |
||
| 283 | */ |
||
| 284 | public function fieldManipulator($context, $versioned = false) |
||
| 332 |
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