| Total Complexity | 56 |
| Total Lines | 314 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Manager implements IManager { |
||
| 49 | |||
| 50 | /** @var ILogger */ |
||
| 51 | private $log; |
||
| 52 | |||
| 53 | /** @var IL10N */ |
||
| 54 | private $l; |
||
| 55 | |||
| 56 | /** @var IFactory */ |
||
| 57 | private $l10nFactory; |
||
| 58 | |||
| 59 | /** @var IURLGenerator */ |
||
| 60 | private $url; |
||
| 61 | |||
| 62 | /** @var IServerContainer */ |
||
| 63 | private $container; |
||
| 64 | |||
| 65 | public function __construct( |
||
| 66 | ILogger $log, |
||
| 67 | IFactory $l10nFactory, |
||
| 68 | IURLGenerator $url, |
||
| 69 | IServerContainer $container |
||
| 70 | ) { |
||
| 71 | $this->log = $log; |
||
| 72 | $this->l10nFactory = $l10nFactory; |
||
| 73 | $this->url = $url; |
||
| 74 | $this->container = $container; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** @var array */ |
||
| 78 | protected $sectionClasses = []; |
||
| 79 | |||
| 80 | /** @var array */ |
||
| 81 | protected $sections = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $type 'admin' or 'personal' |
||
| 85 | * @param string $section Class must implement OCP\Settings\ISection |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function registerSection(string $type, string $section) { |
||
| 90 | if (!isset($this->sectionClasses[$type])) { |
||
| 91 | $this->sectionClasses[$type] = []; |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->sectionClasses[$type][] = $section; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $type 'admin' or 'personal' |
||
| 99 | * |
||
| 100 | * @return ISection[] |
||
| 101 | */ |
||
| 102 | protected function getSections(string $type): array { |
||
| 103 | if (!isset($this->sections[$type])) { |
||
| 104 | $this->sections[$type] = []; |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!isset($this->sectionClasses[$type])) { |
||
| 108 | return $this->sections[$type]; |
||
| 109 | } |
||
| 110 | |||
| 111 | foreach ($this->sectionClasses[$type] as $index => $class) { |
||
| 112 | try { |
||
| 113 | /** @var ISection $section */ |
||
| 114 | $section = \OC::$server->query($class); |
||
| 115 | } catch (QueryException $e) { |
||
| 116 | $this->log->logException($e, ['level' => ILogger::INFO]); |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | if (!$section instanceof ISection) { |
||
| 121 | $this->log->logException(new \InvalidArgumentException('Invalid settings section registered'), ['level' => ILogger::INFO]); |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | |||
| 125 | $sectionID = $section->getID(); |
||
| 126 | |||
| 127 | if (isset($this->sections[$type][$sectionID])) { |
||
| 128 | $this->log->logException(new \InvalidArgumentException('Section with the same ID already registered'), ['level' => ILogger::INFO]); |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | $this->sections[$type][$sectionID] = $section; |
||
| 133 | |||
| 134 | unset($this->sectionClasses[$type][$index]); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $this->sections[$type]; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** @var array */ |
||
| 141 | protected $settingClasses = []; |
||
| 142 | |||
| 143 | /** @var array */ |
||
| 144 | protected $settings = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $type 'admin' or 'personal' |
||
| 148 | * @param string $setting Class must implement OCP\Settings\ISetting |
||
| 149 | * |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function registerSetting(string $type, string $setting) { |
||
| 153 | $this->settingClasses[$setting] = $type; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $type 'admin' or 'personal' |
||
| 158 | * @param string $section |
||
| 159 | * @param Closure $filter optional filter to apply on all loaded ISettings |
||
| 160 | * |
||
| 161 | * @return ISettings[] |
||
| 162 | */ |
||
| 163 | protected function getSettings(string $type, string $section, Closure $filter = null): array { |
||
| 164 | if (!isset($this->settings[$type])) { |
||
| 165 | $this->settings[$type] = []; |
||
| 166 | } |
||
| 167 | if (!isset($this->settings[$type][$section])) { |
||
| 168 | $this->settings[$type][$section] = []; |
||
| 169 | } |
||
| 170 | |||
| 171 | foreach ($this->settingClasses as $class => $settingsType) { |
||
| 172 | if ($type !== $settingsType) { |
||
| 173 | continue; |
||
| 174 | } |
||
| 175 | |||
| 176 | try { |
||
| 177 | /** @var ISettings $setting */ |
||
| 178 | $setting = $this->container->query($class); |
||
| 179 | } catch (QueryException $e) { |
||
| 180 | $this->log->logException($e, ['level' => ILogger::INFO]); |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | if (!$setting instanceof ISettings) { |
||
| 185 | $this->log->logException(new \InvalidArgumentException('Invalid settings setting registered (' . $class . ')'), ['level' => ILogger::INFO]); |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($filter !== null && !$filter($setting)) { |
||
| 190 | continue; |
||
| 191 | } |
||
| 192 | if ($setting->getSection() === null) { |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | |||
| 196 | if (!isset($this->settings[$settingsType][$setting->getSection()])) { |
||
| 197 | $this->settings[$settingsType][$setting->getSection()] = []; |
||
| 198 | } |
||
| 199 | $this->settings[$settingsType][$setting->getSection()][] = $setting; |
||
| 200 | |||
| 201 | unset($this->settingClasses[$class]); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $this->settings[$type][$section]; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @inheritdoc |
||
| 209 | */ |
||
| 210 | public function getAdminSections(): array { |
||
| 211 | // built-in sections |
||
| 212 | $sections = []; |
||
| 213 | |||
| 214 | $appSections = $this->getSections('admin'); |
||
| 215 | |||
| 216 | foreach ($appSections as $section) { |
||
| 217 | /** @var ISection $section */ |
||
| 218 | if (!isset($sections[$section->getPriority()])) { |
||
| 219 | $sections[$section->getPriority()] = []; |
||
| 220 | } |
||
| 221 | |||
| 222 | $sections[$section->getPriority()][] = $section; |
||
| 223 | } |
||
| 224 | |||
| 225 | ksort($sections); |
||
| 226 | |||
| 227 | return $sections; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $section |
||
| 232 | * @param Closure $filter |
||
| 233 | * |
||
| 234 | * @return ISection[] |
||
| 235 | */ |
||
| 236 | private function getBuiltInAdminSettings($section, Closure $filter = null): array { |
||
| 237 | $forms = []; |
||
| 238 | |||
| 239 | if ($section === 'overview') { |
||
| 240 | /** @var ISettings $form */ |
||
| 241 | $form = $this->container->query(\OCA\Settings\Admin\Overview::class); |
||
| 242 | if ($filter === null || $filter($form)) { |
||
| 243 | $forms[$form->getPriority()] = [$form]; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | if ($section === 'server') { |
||
| 247 | /** @var ISettings $form */ |
||
| 248 | $form = $this->container->query(\OCA\Settings\Admin\Server::class); |
||
| 249 | if ($filter === null || $filter($form)) { |
||
| 250 | $forms[$form->getPriority()] = [$form]; |
||
| 251 | } |
||
| 252 | $form = $this->container->query(\OCA\Settings\Admin\Mail::class); |
||
| 253 | if ($filter === null || $filter($form)) { |
||
| 254 | $forms[$form->getPriority()] = [$form]; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | if ($section === 'security') { |
||
| 258 | /** @var ISettings $form */ |
||
| 259 | $form = $this->container->query(\OCA\Settings\Admin\Security::class); |
||
| 260 | if ($filter === null || $filter($form)) { |
||
| 261 | $forms[$form->getPriority()] = [$form]; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | if ($section === 'sharing') { |
||
| 265 | /** @var ISettings $form */ |
||
| 266 | $form = $this->container->query(\OCA\Settings\Admin\Sharing::class); |
||
| 267 | if ($filter === null || $filter($form)) { |
||
| 268 | $forms[$form->getPriority()] = [$form]; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | return $forms; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | public function getAdminSettings($section, bool $subAdminOnly = false): array { |
||
| 279 | if ($subAdminOnly) { |
||
| 280 | $subAdminSettingsFilter = function(ISettings $settings) { |
||
| 281 | return $settings instanceof ISubAdminSettings; |
||
| 282 | }; |
||
| 283 | $settings = $this->getBuiltInAdminSettings($section, $subAdminSettingsFilter); |
||
| 284 | $appSettings = $this->getSettings('admin', $section, $subAdminSettingsFilter); |
||
| 285 | } else { |
||
| 286 | $settings = $this->getBuiltInAdminSettings($section); |
||
| 287 | $appSettings = $this->getSettings('admin', $section); |
||
| 288 | } |
||
| 289 | |||
| 290 | foreach ($appSettings as $setting) { |
||
| 291 | if (!isset($settings[$setting->getPriority()])) { |
||
| 292 | $settings[$setting->getPriority()] = []; |
||
| 293 | } |
||
| 294 | $settings[$setting->getPriority()][] = $setting; |
||
| 295 | } |
||
| 296 | |||
| 297 | ksort($settings); |
||
| 298 | return $settings; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @inheritdoc |
||
| 303 | */ |
||
| 304 | public function getPersonalSections(): array { |
||
| 305 | if ($this->l === null) { |
||
| 306 | $this->l = $this->l10nFactory->get('lib'); |
||
| 307 | } |
||
| 308 | |||
| 309 | $sections = []; |
||
| 310 | |||
| 311 | $legacyForms = \OC_App::getForms('personal'); |
||
| 312 | if (!empty($legacyForms) && $this->hasLegacyPersonalSettingsToRender($legacyForms)) { |
||
| 313 | $sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))]; |
||
| 314 | } |
||
| 315 | |||
| 316 | $appSections = $this->getSections('personal'); |
||
| 317 | |||
| 318 | foreach ($appSections as $section) { |
||
| 319 | /** @var ISection $section */ |
||
| 320 | if (!isset($sections[$section->getPriority()])) { |
||
| 321 | $sections[$section->getPriority()] = []; |
||
| 322 | } |
||
| 323 | |||
| 324 | $sections[$section->getPriority()][] = $section; |
||
| 325 | } |
||
| 326 | |||
| 327 | ksort($sections); |
||
| 328 | |||
| 329 | return $sections; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string[] $forms |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | private function hasLegacyPersonalSettingsToRender(array $forms): bool { |
||
| 338 | foreach ($forms as $form) { |
||
| 339 | if (trim($form) !== '') { |
||
| 340 | return true; |
||
| 341 | } |
||
| 342 | } |
||
| 343 | return false; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @inheritdoc |
||
| 348 | */ |
||
| 349 | public function getPersonalSettings($section): array { |
||
| 362 | } |
||
| 363 | } |
||
| 364 |
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