Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 51 | class Manager implements IManager { |
||
| 52 | /** @var ILogger */ |
||
| 53 | private $log; |
||
| 54 | /** @var IDBConnection */ |
||
| 55 | private $dbc; |
||
| 56 | /** @var IL10N */ |
||
| 57 | private $l; |
||
| 58 | /** @var IConfig */ |
||
| 59 | private $config; |
||
| 60 | /** @var EncryptionManager */ |
||
| 61 | private $encryptionManager; |
||
| 62 | /** @var IUserManager */ |
||
| 63 | private $userManager; |
||
| 64 | /** @var ILockingProvider */ |
||
| 65 | private $lockingProvider; |
||
| 66 | /** @var IRequest */ |
||
| 67 | private $request; |
||
| 68 | /** @var IURLGenerator */ |
||
| 69 | private $url; |
||
| 70 | /** @var AccountManager */ |
||
| 71 | private $accountManager; |
||
| 72 | /** @var IGroupManager */ |
||
| 73 | private $groupManager; |
||
| 74 | /** @var IFactory */ |
||
| 75 | private $l10nFactory; |
||
| 76 | /** @var IAppManager */ |
||
| 77 | private $appManager; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param ILogger $log |
||
| 81 | * @param IDBConnection $dbc |
||
| 82 | * @param IL10N $l |
||
| 83 | * @param IConfig $config |
||
| 84 | * @param EncryptionManager $encryptionManager |
||
| 85 | * @param IUserManager $userManager |
||
| 86 | * @param ILockingProvider $lockingProvider |
||
| 87 | * @param IRequest $request |
||
| 88 | * @param IURLGenerator $url |
||
| 89 | * @param AccountManager $accountManager |
||
| 90 | * @param IGroupManager $groupManager |
||
| 91 | * @param IFactory $l10nFactory |
||
| 92 | * @param IAppManager $appManager |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function __construct( |
|
| 123 | |||
| 124 | /** @var array */ |
||
| 125 | protected $sectionClasses = []; |
||
| 126 | |||
| 127 | /** @var array */ |
||
| 128 | protected $sections = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $type 'admin' or 'personal' |
||
| 132 | * @param string $section Class must implement OCP\Settings\ISection |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function registerSection(string $type, string $section) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $type 'admin' or 'personal' |
||
| 145 | * @return ISection[] |
||
| 146 | */ |
||
| 147 | protected function getSections(string $type): array { |
||
| 148 | if (!isset($this->sections[$type])) { |
||
| 149 | $this->sections[$type] = []; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (!isset($this->sectionClasses[$type])) { |
||
| 153 | return $this->sections[$type]; |
||
| 154 | } |
||
| 155 | |||
| 156 | foreach ($this->sectionClasses[$type] as $index => $class) { |
||
| 157 | try { |
||
| 158 | /** @var ISection $section */ |
||
| 159 | $section = \OC::$server->query($class); |
||
| 160 | } catch (QueryException $e) { |
||
| 161 | $this->log->logException($e, ['level' => ILogger::INFO]); |
||
|
|
|||
| 162 | continue; |
||
| 163 | } |
||
| 164 | |||
| 165 | View Code Duplication | if (!$section instanceof ISection) { |
|
| 166 | $this->log->logException(new \InvalidArgumentException('Invalid settings section registered'), ['level' => ILogger::INFO]); |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | $sectionID = $section->getID(); |
||
| 171 | |||
| 172 | if (isset($this->sections[$type][$sectionID])) { |
||
| 173 | $this->log->logException(new \InvalidArgumentException('Section with the same ID already registered'), ['level' => ILogger::INFO]); |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->sections[$type][$sectionID] = $section; |
||
| 178 | |||
| 179 | unset($this->sectionClasses[$type][$index]); |
||
| 180 | } |
||
| 181 | |||
| 182 | return $this->sections[$type]; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** @var array */ |
||
| 186 | protected $settingClasses = []; |
||
| 187 | |||
| 188 | /** @var array */ |
||
| 189 | protected $settings = []; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $type 'admin' or 'personal' |
||
| 193 | * @param string $setting Class must implement OCP\Settings\ISetting |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public function registerSetting(string $type, string $setting) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $type 'admin' or 'personal' |
||
| 202 | * @param string $section |
||
| 203 | * @return ISettings[] |
||
| 204 | */ |
||
| 205 | protected function getSettings(string $type, string $section): array { |
||
| 206 | if (!isset($this->settings[$type])) { |
||
| 207 | $this->settings[$type] = []; |
||
| 208 | } |
||
| 209 | if (!isset($this->settings[$type][$section])) { |
||
| 210 | $this->settings[$type][$section] = []; |
||
| 211 | } |
||
| 212 | |||
| 213 | foreach ($this->settingClasses as $class => $settingsType) { |
||
| 214 | try { |
||
| 215 | /** @var ISettings $setting */ |
||
| 216 | $setting = \OC::$server->query($class); |
||
| 217 | } catch (QueryException $e) { |
||
| 218 | $this->log->logException($e, ['level' => ILogger::INFO]); |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | View Code Duplication | if (!$setting instanceof ISettings) { |
|
| 223 | $this->log->logException(new \InvalidArgumentException('Invalid settings setting registered'), ['level' => ILogger::INFO]); |
||
| 224 | continue; |
||
| 225 | } |
||
| 226 | |||
| 227 | if (!isset($this->settings[$settingsType][$setting->getSection()])) { |
||
| 228 | $this->settings[$settingsType][$setting->getSection()] = []; |
||
| 229 | } |
||
| 230 | $this->settings[$settingsType][$setting->getSection()][] = $setting; |
||
| 231 | |||
| 232 | unset($this->settingClasses[$class]); |
||
| 233 | } |
||
| 234 | |||
| 235 | return $this->settings[$type][$section]; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @inheritdoc |
||
| 240 | */ |
||
| 241 | public function getAdminSections(): array { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $section |
||
| 270 | * @return ISection[] |
||
| 271 | */ |
||
| 272 | private function getBuiltInAdminSettings($section): array { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $section |
||
| 303 | * @return ISection[] |
||
| 304 | */ |
||
| 305 | private function getBuiltInPersonalSettings($section): array { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @inheritdoc |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function getAdminSettings($section): array { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @inheritdoc |
||
| 357 | */ |
||
| 358 | public function getPersonalSections(): array { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string[] $forms |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | private function hasLegacyPersonalSettingsToRender(array $forms): bool { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @inheritdoc |
||
| 401 | */ |
||
| 402 | View Code Duplication | public function getPersonalSettings($section): array { |
|
| 416 | } |
||
| 417 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: