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