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 AppManager 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 AppManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class AppManager implements IAppManager { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Apps with these types can not be enabled for certain groups only |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | protected $protectedAppTypes = [ |
||
| 54 | 'filesystem', |
||
| 55 | 'prelogin', |
||
| 56 | 'authentication', |
||
| 57 | 'logging', |
||
| 58 | 'prevent_group_restriction', |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** @var \OCP\IUserSession */ |
||
| 62 | private $userSession; |
||
| 63 | /** @var \OCP\IAppConfig */ |
||
| 64 | private $appConfig; |
||
| 65 | /** @var \OCP\IGroupManager */ |
||
| 66 | private $groupManager; |
||
| 67 | /** @var \OCP\ICacheFactory */ |
||
| 68 | private $memCacheFactory; |
||
| 69 | /** @var string[] $appId => $enabled */ |
||
| 70 | private $installedAppsCache; |
||
| 71 | /** @var string[] */ |
||
| 72 | private $shippedApps; |
||
| 73 | /** @var string[] */ |
||
| 74 | private $alwaysEnabled; |
||
| 75 | /** @var EventDispatcherInterface */ |
||
| 76 | private $dispatcher; |
||
| 77 | /** @var IConfig */ |
||
| 78 | private $config; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param IUserSession $userSession |
||
| 82 | * @param IAppConfig $appConfig |
||
| 83 | * @param IGroupManager $groupManager |
||
| 84 | * @param ICacheFactory $memCacheFactory |
||
| 85 | * @param EventDispatcherInterface $dispatcher |
||
| 86 | * @param IConfig $config |
||
| 87 | */ |
||
| 88 | public function __construct(IUserSession $userSession = null, |
||
| 89 | IAppConfig $appConfig, |
||
| 90 | IGroupManager $groupManager, |
||
| 91 | ICacheFactory $memCacheFactory, |
||
| 92 | EventDispatcherInterface $dispatcher, |
||
| 93 | IConfig $config) { |
||
| 94 | $this->userSession = $userSession; |
||
| 95 | $this->appConfig = $appConfig; |
||
| 96 | $this->groupManager = $groupManager; |
||
| 97 | $this->memCacheFactory = $memCacheFactory; |
||
| 98 | $this->dispatcher = $dispatcher; |
||
| 99 | $this->config = $config; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string[] $appId => $enabled |
||
| 104 | */ |
||
| 105 | private function getInstalledAppsValues() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * List all installed apps |
||
| 124 | * |
||
| 125 | * @return string[] |
||
| 126 | */ |
||
| 127 | public function getInstalledApps() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * List all apps enabled for a user |
||
| 133 | * |
||
| 134 | * @param \OCP\IUser|null $user |
||
| 135 | * @return string[] |
||
| 136 | */ |
||
| 137 | public function getEnabledAppsForUser(IUser $user = null) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Check if an app is enabled for user |
||
| 147 | * |
||
| 148 | * @param string $appId |
||
| 149 | * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function isEnabledForUser($appId, $user = null) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $enabled |
||
| 169 | * @param IUser $user |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | private function checkAppForUser($enabled, $user) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Check if an app is installed in the instance |
||
| 202 | * |
||
| 203 | * @param string $appId |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isInstalled($appId) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Enable an app for every user |
||
| 213 | * |
||
| 214 | * @param string $appId |
||
| 215 | * @throws \Exception |
||
| 216 | */ |
||
| 217 | public function enableApp($appId) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Do not allow more than one active app-theme |
||
| 233 | * |
||
| 234 | * @param $appId |
||
| 235 | * @throws AppManagerException |
||
| 236 | */ |
||
| 237 | protected function canEnableTheme($appId) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Wrapper for OC_App for easy mocking |
||
| 255 | * |
||
| 256 | * @param string $appId |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | protected function isTheme($appId) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Enable an app only for specific groups |
||
| 265 | * |
||
| 266 | * @param string $appId |
||
| 267 | * @param \OCP\IGroup[] $groups |
||
| 268 | * @throws \Exception if app can't be enabled for groups |
||
| 269 | */ |
||
| 270 | public function enableAppForGroups($appId, $groups) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Disable an app for every user |
||
| 293 | * |
||
| 294 | * @param string $appId |
||
| 295 | * @throws \Exception if app can't be disabled |
||
| 296 | */ |
||
| 297 | public function disableApp($appId) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Clear the cached list of apps when enabling/disabling an app |
||
| 311 | */ |
||
| 312 | public function clearAppsCache() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns a list of apps that need upgrade |
||
| 319 | * |
||
| 320 | * @param array $ocVersion ownCloud version as array of version components |
||
| 321 | * @return array list of app info from apps that need an upgrade |
||
| 322 | * |
||
| 323 | * @internal |
||
| 324 | */ |
||
| 325 | public function getAppsNeedingUpgrade($ocVersion) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns the app information from "appinfo/info.xml". |
||
| 345 | * |
||
| 346 | * @param string $appId app id |
||
| 347 | * |
||
| 348 | * @return array app info |
||
| 349 | * |
||
| 350 | * @internal |
||
| 351 | */ |
||
| 352 | View Code Duplication | public function getAppInfo($appId) { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a list of apps incompatible with the given version |
||
| 366 | * |
||
| 367 | * @param array $version ownCloud version as array of version components |
||
| 368 | * |
||
| 369 | * @return array list of app info from incompatible apps |
||
| 370 | * |
||
| 371 | * @internal |
||
| 372 | */ |
||
| 373 | public function getIncompatibleApps($version) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @inheritdoc |
||
| 387 | */ |
||
| 388 | public function isShipped($appId) { |
||
| 392 | |||
| 393 | private function isAlwaysEnabled($appId) { |
||
| 397 | |||
| 398 | private function loadShippedJson() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @inheritdoc |
||
| 412 | */ |
||
| 413 | public function getAlwaysEnabledApps() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param string $package package path |
||
| 420 | * @param bool $skipMigrations whether to skip migrations, which would only install the code |
||
| 421 | * @return string|false app id or false in case of error |
||
| 422 | * @since 10.0 |
||
| 423 | */ |
||
| 424 | public function installApp($package, $skipMigrations = false) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param string $package |
||
| 434 | * @return mixed |
||
| 435 | * @since 10.0 |
||
| 436 | */ |
||
| 437 | public function updateApp($package) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Returns the list of all apps, enabled and disabled |
||
| 446 | * |
||
| 447 | * @return string[] |
||
| 448 | * @since 10.0 |
||
| 449 | */ |
||
| 450 | public function getAllApps() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $path |
||
| 456 | * @return string[] app info |
||
| 457 | */ |
||
| 458 | public function readAppPackage($path) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Indicates if app installation is supported. Usually it is but in certain |
||
| 471 | * environments it is disallowed because of hardening. In a clustered setup |
||
| 472 | * apps need to be installed on each cluster node which is out of scope of |
||
| 473 | * ownCloud itself. |
||
| 474 | * |
||
| 475 | * @return bool |
||
| 476 | * @since 10.0.3 |
||
| 477 | */ |
||
| 478 | public function canInstall() { |
||
| 486 | } |
||
| 487 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.