| Total Complexity | 58 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class User implements \JsonSerializable { |
||
| 29 | |||
| 30 | const TYPE_USER = 'user'; |
||
| 31 | const TYPE_GROUP = 'group'; |
||
| 32 | const TYPE_CONTACTGROUP = 'contactGroup'; |
||
| 33 | const TYPE_CONTACT = 'contact'; |
||
| 34 | const TYPE_EMAIL = 'email'; |
||
| 35 | const TYPE_CIRCLE = 'circle'; |
||
| 36 | const TYPE_EXTERNAL = 'external'; |
||
| 37 | |||
| 38 | /** @var IL10N */ |
||
| 39 | private $l10n; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | private $userId; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $type; |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | private $displayName = ''; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $desc = ''; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $emailAddress = ''; |
||
| 55 | |||
| 56 | private $contact; |
||
| 57 | private $circlesEnabled = false; |
||
| 58 | private $contactsEnabled = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * User constructor. |
||
| 62 | * @param $type |
||
| 63 | * @param $userId |
||
| 64 | * @param $emailAddress |
||
| 65 | * @param $displayName |
||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 68 | $type, |
||
| 69 | $userId, |
||
| 70 | $emailAddress = '', |
||
| 71 | $displayName = '' |
||
| 72 | ) { |
||
| 73 | $this->l10n = \OC::$server->getL10N('polls'); |
||
| 74 | $this->type = $type; |
||
| 75 | $this->userId = $userId; |
||
| 76 | $this->emailAddress = $emailAddress; |
||
| 77 | $this->displayName = $displayName; |
||
| 78 | $this->loadContact(); |
||
| 79 | $this->circlesEnabled = \OC::$server->getAppManager()->isEnabledForUser('circles') && |
||
| 80 | (version_compare(\OC::$server->getAppManager()->getAppVersion('circles'), '0.17.1') >= 0); |
||
| 81 | $this->contactsEnabled = \OC::$server->getContactsManager()->isEnabled(); |
||
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | public function setDisplayName($displayName) { |
||
| 86 | $this->displayName = $displayName; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function setEmailAddress($emailAddress) { |
||
| 90 | $this->emailAddress = $emailAddress; |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getUserId() { |
||
| 94 | return $this->userId; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getType() { |
||
| 99 | } |
||
| 100 | |||
| 101 | public function getLanguage() { |
||
| 102 | if ($this->type === self::TYPE_USER) { |
||
| 103 | // Variant: $this->config->getUserValue($this->userId, 'core', 'lang') |
||
| 104 | return \OC::$server->getConfig()->getUserValue($this->userId, 'core', 'lang'); |
||
| 105 | } else { |
||
| 106 | return ''; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getDisplayName() { |
||
| 111 | if ($this->type === self::TYPE_USER) { |
||
| 112 | return \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); |
||
| 113 | |||
| 114 | } elseif ($this->type === self::TYPE_GROUP) { |
||
| 115 | try { |
||
| 116 | // since NC19 |
||
| 117 | return \OC::$server->getGroupManager()->get($this->userId)->getDisplayName(); |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | // until NC18 |
||
| 120 | return $this->userId; |
||
| 121 | } |
||
| 122 | |||
| 123 | } elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) { |
||
| 124 | return $this->userId; |
||
| 125 | |||
| 126 | } elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) { |
||
| 127 | return \OCA\Circles\Api\v1\Circles::detailsCircle($this->userId)->getName(); |
||
|
|
|||
| 128 | |||
| 129 | } elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) { |
||
| 130 | return isset($this->contact['FN']) ? $this->contact['FN'] : ''; |
||
| 131 | |||
| 132 | } elseif ($this->displayName) { |
||
| 133 | return $this->displayName; |
||
| 134 | |||
| 135 | } else { |
||
| 136 | return $this->userId; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | public function getOrganisation() { |
||
| 141 | if ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) { |
||
| 142 | return isset($this->contact['ORG']) ? $this->contact['ORG'] : ''; |
||
| 143 | } else { |
||
| 144 | return ''; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getEmailAddress() { |
||
| 149 | if ($this->type === self::TYPE_USER) { |
||
| 150 | // Variant: \OC::$server->getConfig()->getUserValue($this->userId, 'settings', 'email'), |
||
| 151 | return \OC::$server->getUserManager()->get($this->userId)->getEMailAddress(); |
||
| 152 | |||
| 153 | } elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) { |
||
| 154 | return isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : ''; |
||
| 155 | |||
| 156 | } elseif ($this->type === self::TYPE_EMAIL) { |
||
| 157 | return $this->userId; |
||
| 158 | |||
| 159 | } else { |
||
| 160 | return $this->emailAddress; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | public function getDesc() { |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | public function getIcon() { |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | private function loadContact() { |
||
| 227 | if ($this->type === self::TYPE_CONTACT && \OC::$server->getContactsManager()->isEnabled()) { |
||
| 228 | // TODO: remove FN in a later version than 1.5 |
||
| 229 | $contacts = \OC::$server->getContactsManager()->search($this->userId, ['UID', 'FN']); |
||
| 230 | if (!$contacts) { |
||
| 231 | $this->contact = []; |
||
| 232 | } else { |
||
| 233 | $this->contact = $contacts[0]; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | public function jsonSerialize(): array { |
||
| 252 | ]; |
||
| 253 | } |
||
| 254 | } |
||
| 255 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths