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 |
||
| 46 | class AppManager implements IAppManager { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Apps with these types can not be enabled for certain groups only |
||
| 50 | * @var string[] |
||
| 51 | */ |
||
| 52 | protected $protectedAppTypes = [ |
||
| 53 | 'filesystem', |
||
| 54 | 'prelogin', |
||
| 55 | 'authentication', |
||
| 56 | 'logging', |
||
| 57 | 'prevent_group_restriction', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** @var \OCP\IUserSession */ |
||
| 61 | private $userSession; |
||
| 62 | /** @var \OCP\IAppConfig */ |
||
| 63 | private $appConfig; |
||
| 64 | /** @var \OCP\IGroupManager */ |
||
| 65 | private $groupManager; |
||
| 66 | /** @var \OCP\ICacheFactory */ |
||
| 67 | private $memCacheFactory; |
||
| 68 | /** @var string[] $appId => $enabled */ |
||
| 69 | private $installedAppsCache; |
||
| 70 | /** @var string[] */ |
||
| 71 | private $shippedApps; |
||
| 72 | /** @var string[] */ |
||
| 73 | private $alwaysEnabled; |
||
| 74 | /** @var EventDispatcherInterface */ |
||
| 75 | private $dispatcher; |
||
| 76 | /** @var IConfig */ |
||
| 77 | private $config; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param IUserSession $userSession |
||
| 81 | * @param IAppConfig $appConfig |
||
| 82 | * @param IGroupManager $groupManager |
||
| 83 | * @param ICacheFactory $memCacheFactory |
||
| 84 | * @param EventDispatcherInterface $dispatcher |
||
| 85 | * @param IConfig $config |
||
| 86 | */ |
||
| 87 | public function __construct(IUserSession $userSession = null, |
||
| 88 | IAppConfig $appConfig, |
||
| 89 | IGroupManager $groupManager, |
||
| 90 | ICacheFactory $memCacheFactory, |
||
| 91 | EventDispatcherInterface $dispatcher, |
||
| 92 | IConfig $config) { |
||
| 93 | $this->userSession = $userSession; |
||
| 94 | $this->appConfig = $appConfig; |
||
| 95 | $this->groupManager = $groupManager; |
||
| 96 | $this->memCacheFactory = $memCacheFactory; |
||
| 97 | $this->dispatcher = $dispatcher; |
||
| 98 | $this->config = $config; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return string[] $appId => $enabled |
||
| 103 | */ |
||
| 104 | private function getInstalledAppsValues() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * List all installed apps |
||
| 123 | * |
||
| 124 | * @return string[] |
||
| 125 | */ |
||
| 126 | public function getInstalledApps() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * List all apps enabled for a user |
||
| 132 | * |
||
| 133 | * @param \OCP\IUser $user |
||
| 134 | * @return string[] |
||
| 135 | */ |
||
| 136 | public function getEnabledAppsForUser(IUser $user) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check if an app is enabled for user |
||
| 146 | * |
||
| 147 | * @param string $appId |
||
| 148 | * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function isEnabledForUser($appId, $user = null) { |
||
| 152 | if ($this->isAlwaysEnabled($appId)) { |
||
| 153 | return true; |
||
| 154 | } |
||
| 155 | if (is_null($user) && !is_null($this->userSession)) { |
||
| 156 | $user = $this->userSession->getUser(); |
||
| 157 | } |
||
| 158 | $installedApps = $this->getInstalledAppsValues(); |
||
| 159 | if (isset($installedApps[$appId])) { |
||
| 160 | return $this->checkAppForUser($installedApps[$appId], $user); |
||
| 161 | } else { |
||
| 162 | return false; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $enabled |
||
| 168 | * @param IUser $user |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | private function checkAppForUser($enabled, $user) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Check if an app is installed in the instance |
||
| 201 | * |
||
| 202 | * @param string $appId |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function isInstalled($appId) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Enable an app for every user |
||
| 212 | * |
||
| 213 | * @param string $appId |
||
| 214 | * @throws \Exception |
||
| 215 | */ |
||
| 216 | public function enableApp($appId) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Enable an app only for specific groups |
||
| 230 | * |
||
| 231 | * @param string $appId |
||
| 232 | * @param \OCP\IGroup[] $groups |
||
| 233 | * @throws \Exception if app can't be enabled for groups |
||
| 234 | */ |
||
| 235 | public function enableAppForGroups($appId, $groups) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Disable an app for every user |
||
| 258 | * |
||
| 259 | * @param string $appId |
||
| 260 | * @throws \Exception if app can't be disabled |
||
| 261 | */ |
||
| 262 | public function disableApp($appId) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Clear the cached list of apps when enabling/disabling an app |
||
| 276 | */ |
||
| 277 | public function clearAppsCache() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns a list of apps that need upgrade |
||
| 284 | * |
||
| 285 | * @param array $ocVersion ownCloud version as array of version components |
||
| 286 | * @return array list of app info from apps that need an upgrade |
||
| 287 | * |
||
| 288 | * @internal |
||
| 289 | */ |
||
| 290 | public function getAppsNeedingUpgrade($ocVersion) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the app information from "appinfo/info.xml". |
||
| 310 | * |
||
| 311 | * @param string $appId app id |
||
| 312 | * |
||
| 313 | * @return array app info |
||
| 314 | * |
||
| 315 | * @internal |
||
| 316 | */ |
||
| 317 | View Code Duplication | public function getAppInfo($appId) { |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Returns a list of apps incompatible with the given version |
||
| 331 | * |
||
| 332 | * @param array $version ownCloud version as array of version components |
||
| 333 | * |
||
| 334 | * @return array list of app info from incompatible apps |
||
| 335 | * |
||
| 336 | * @internal |
||
| 337 | */ |
||
| 338 | public function getIncompatibleApps($version) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @inheritdoc |
||
| 352 | */ |
||
| 353 | public function isShipped($appId) { |
||
| 357 | |||
| 358 | private function isAlwaysEnabled($appId) { |
||
| 362 | |||
| 363 | private function loadShippedJson() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @inheritdoc |
||
| 377 | */ |
||
| 378 | public function getAlwaysEnabledApps() { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $package package path |
||
| 385 | * @param bool $skipMigrations whether to skip migrations, which would only install the code |
||
| 386 | * @return string|false app id or false in case of error |
||
| 387 | * @since 10.0 |
||
| 388 | */ |
||
| 389 | public function installApp($package, $skipMigrations = false) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $package |
||
| 399 | * @return mixed |
||
| 400 | * @since 10.0 |
||
| 401 | */ |
||
| 402 | public function updateApp($package) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Returns the list of all apps, enabled and disabled |
||
| 411 | * |
||
| 412 | * @return string[] |
||
| 413 | * @since 10.0 |
||
| 414 | */ |
||
| 415 | public function getAllApps() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $path |
||
| 421 | * @return string[] app info |
||
| 422 | */ |
||
| 423 | public function readAppPackage($path) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Indicates if app installation is supported. Usually it is but in certain |
||
| 436 | * environments it is disallowed because of hardening. In a clustered setup |
||
| 437 | * apps need to be installed on each cluster node which is out of scope of |
||
| 438 | * ownCloud itself. |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | * @since 10.0.3 |
||
| 442 | */ |
||
| 443 | public function canInstall() { |
||
| 451 | } |
||
| 452 |
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.