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:
| 1 | <?php |
||
| 64 | class SettingsManager implements ISettingsManager { |
||
| 65 | |||
| 66 | /** @var IL10N */ |
||
| 67 | protected $l; |
||
| 68 | /** @var IAppManager */ |
||
| 69 | protected $appManager; |
||
| 70 | /** @var ILogger */ |
||
| 71 | protected $logger; |
||
| 72 | /** @var IURLGenerator */ |
||
| 73 | protected $urlGenerator; |
||
| 74 | /** @var Defaults */ |
||
| 75 | protected $defaults; |
||
| 76 | /** @var IUserSession */ |
||
| 77 | protected $userSession; |
||
| 78 | /** @var ILogger */ |
||
| 79 | protected $log; |
||
| 80 | /** @var IConfig */ |
||
| 81 | protected $config; |
||
| 82 | /** @var IGroupManager */ |
||
| 83 | protected $groupManager; |
||
| 84 | /** @var Helper */ |
||
| 85 | protected $helper; |
||
| 86 | /** @var IFactory */ |
||
| 87 | protected $lfactory; |
||
| 88 | /** @var IDBConnection */ |
||
| 89 | protected $dbconnection; |
||
| 90 | /** @var ILockingProvider */ |
||
| 91 | protected $lockingProvider; |
||
| 92 | /** @var CertificateManager */ |
||
| 93 | protected $certificateManager; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Holds a cache of ISettings with keys for type |
||
| 97 | */ |
||
| 98 | protected $panels = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Holds an array of sections |
||
| 102 | */ |
||
| 103 | protected $sections = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param IL10N $l |
||
| 107 | * @param IAppManager $appManager |
||
| 108 | * @param IUserSession $userSession |
||
| 109 | * @param ILogger $logger |
||
| 110 | * @param IGroupManager $groupManager |
||
| 111 | * @param IConfig $config |
||
| 112 | * @param Defaults $defaults |
||
| 113 | * @param IURLGenerator $urlGenerator |
||
| 114 | * @param Helper $helper |
||
| 115 | * @param ILockingProvider $lockingProvider |
||
| 116 | * @param IDBConnection $dbconnection |
||
| 117 | * @param CertificateManager $certificateManager |
||
| 118 | * @param IFactory $lfactory |
||
| 119 | */ |
||
| 120 | public function __construct(IL10N $l, |
||
| 147 | |||
| 148 | public function getPersonalSections() { |
||
| 153 | |||
| 154 | public function getAdminSections() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns ISettings for the personal settings in the given section |
||
| 162 | * @param string $sectionID |
||
| 163 | * @return array of ISection |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function getPersonalPanels($sectionID) { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Returns ISettings for the admin settings in the given section |
||
| 177 | * @param string $sectionID |
||
| 178 | * @return array of ISection |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function getAdminPanels($sectionID) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the default set of ISections used in core |
||
| 192 | * @param string $type the type of sections to return |
||
| 193 | * @return array of ISection |
||
| 194 | */ |
||
| 195 | private function getBuiltInSections($type) { |
||
| 196 | if($type === 'admin') { |
||
| 197 | return [ |
||
| 198 | new Section('general', $this->l->t('General'), 100), |
||
| 199 | new Section('storage', $this->l->t('Storage'), 95, 'folder'), |
||
| 200 | new Section('security', $this->l->t('Security'), 90, 'password'), |
||
| 201 | new Section('authentication', $this->l->t('Authentication'), 87, 'user'), |
||
| 202 | new Section('encryption', $this->l->t('Encryption'), 85, 'password'), |
||
| 203 | new Section('sharing', $this->l->t('Sharing'), 80, 'share'), |
||
| 204 | new Section('monitoring', $this->l->t('Monitoring'), 75, 'search'), |
||
| 205 | new Section('apps', $this->l->t('Apps'), 70, 'list'), |
||
| 206 | new Section('updates', $this->l->t('Updates'), 20, 'update'), |
||
| 207 | new Section('additional', $this->l->t('Additional'), -10, 'more'), |
||
| 208 | ]; |
||
| 209 | } else if($type === 'personal') { |
||
| 210 | return [ |
||
| 211 | new Section('general', $this->l->t('General'), 100, 'user'), |
||
| 212 | new Section('storage', $this->l->t('Storage'), 50, 'folder'), |
||
| 213 | new Section('security', $this->l->t('Security'), 30, 'password'), |
||
| 214 | new Section('encryption', $this->l->t('Encryption'), 20), |
||
| 215 | new Section('additional', $this->l->t('Additional'), 5, 'more'), |
||
| 216 | ]; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns an array of classnames for built in settings panels |
||
| 222 | * @return array of strings |
||
| 223 | */ |
||
| 224 | private function getBuiltInPanels() { |
||
| 225 | return [ |
||
| 226 | 'personal' => [ |
||
| 227 | Profile::class, |
||
| 228 | Clients::class, |
||
| 229 | LegacyPersonal::class, |
||
| 230 | Version::class, |
||
| 231 | Tokens::class, |
||
| 232 | Quota::class, |
||
| 233 | ], |
||
| 234 | 'admin' => [ |
||
| 235 | LegacyAdmin::class, |
||
| 236 | BackgroundJobs::class, |
||
| 237 | Logging::class, |
||
| 238 | Tips::class, |
||
| 239 | SecurityWarning::class, |
||
| 240 | Mail::class, |
||
| 241 | FileSharing::class, |
||
| 242 | Encryption::class, |
||
| 243 | Certificates::class, |
||
| 244 | Apps::class, |
||
| 245 | Status::class |
||
| 246 | ] |
||
| 247 | ]; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Gets panel objects with dependencies instantiated from the container |
||
| 252 | * @param string $className |
||
| 253 | * @return array|false |
||
| 254 | */ |
||
| 255 | public function getBuiltInPanel($className) { |
||
| 256 | $panels = [ |
||
| 257 | // Personal |
||
| 258 | Profile::class => new Profile( |
||
| 259 | $this->config, |
||
| 260 | $this->groupManager, |
||
| 261 | $this->userSession, |
||
| 262 | $this->lfactory), |
||
| 263 | LegacyPersonal::class => new LegacyPersonal($this->helper), |
||
| 264 | Clients::class => new Clients($this->config, $this->defaults), |
||
| 265 | Version::class => new Version(), |
||
| 266 | Tokens::class => new Tokens(), |
||
| 267 | Quota::class => new Quota($this->helper), |
||
| 268 | // Admin |
||
| 269 | BackgroundJobs::class => new BackgroundJobs($this->config), |
||
| 270 | Certificates::class => new Certificates( |
||
| 271 | $this->config, |
||
| 272 | $this->urlGenerator, |
||
| 273 | $this->certificateManager), |
||
| 274 | Encryption::class => new Encryption(), |
||
| 275 | FileSharing::class => new FileSharing($this->config, $this->helper), |
||
| 276 | Logging::class => new Logging($this->config, $this->urlGenerator, $this->helper), |
||
| 277 | Mail::class => new Mail($this->config, $this->helper), |
||
| 278 | SecurityWarning::class => new SecurityWarning( |
||
| 279 | $this->l, |
||
| 280 | $this->config, |
||
| 281 | $this->dbconnection, |
||
| 282 | $this->helper, |
||
| 283 | $this->lockingProvider), |
||
| 284 | Tips::class => new Tips(), |
||
| 285 | LegacyAdmin::class => new LegacyAdmin($this->helper), |
||
| 286 | Apps::class => new Apps($this->config) |
||
| 287 | ]; |
||
| 288 | if(isset($panels[$className])) { |
||
| 289 | return $panels[$className]; |
||
| 290 | } else { |
||
| 291 | return false; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Gets all the panels for ownCloud |
||
| 297 | * @param string $type the type of sections to return |
||
| 298 | * @return array of strings |
||
| 299 | */ |
||
| 300 | public function getPanelsList($type) { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * Searches through the currently enabled apps and returns the panels registered |
||
| 309 | * @return array of strings |
||
| 310 | */ |
||
| 311 | protected function findRegisteredPanels() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Attempts to load a ISettings using the class name |
||
| 325 | * @param string $className |
||
| 326 | * @throws QueryException |
||
| 327 | * @return ISettings |
||
| 328 | */ |
||
| 329 | protected function loadPanel($className) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Find and return ISettings for the given type |
||
| 354 | * @param string $type of panels to load |
||
| 355 | * @return array of ISettings |
||
| 356 | */ |
||
| 357 | public function loadPanels($type) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return the section object for the corresponding type and sectionID |
||
| 391 | * @param string $type |
||
| 392 | * @param string $sectionID |
||
| 393 | * @throws QueryException |
||
| 394 | * @return ISection |
||
| 395 | */ |
||
| 396 | protected function loadSection($type, $sectionID) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Sort the array of ISettings or ISections by their priority attribute |
||
| 411 | * @param array $objects (ISections of ISettings) |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected function sortOrder($objects) { |
||
| 422 | |||
| 423 | } |
||
| 424 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: