| Total Complexity | 58 |
| Total Lines | 203 |
| 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 |
||
| 29 | class User implements \JsonSerializable { |
||
| 30 | public const TYPE_USER = 'user'; |
||
| 31 | public const TYPE_GROUP = 'group'; |
||
| 32 | public const TYPE_CONTACTGROUP = 'contactGroup'; |
||
| 33 | public const TYPE_CONTACT = 'contact'; |
||
| 34 | public const TYPE_EMAIL = 'email'; |
||
| 35 | public const TYPE_CIRCLE = 'circle'; |
||
| 36 | public 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() { |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | public function getDisplayName() { |
||
| 111 | if ($this->type === self::TYPE_USER) { |
||
| 112 | return \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); |
||
| 113 | } elseif ($this->type === self::TYPE_GROUP) { |
||
| 114 | try { |
||
| 115 | // since NC19 |
||
| 116 | return \OC::$server->getGroupManager()->get($this->userId)->getDisplayName(); |
||
| 117 | } catch (\Exception $e) { |
||
| 118 | // until NC18 |
||
| 119 | return $this->userId; |
||
| 120 | } |
||
| 121 | } elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) { |
||
| 122 | return $this->userId; |
||
| 123 | } elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) { |
||
| 124 | return \OCA\Circles\Api\v1\Circles::detailsCircle($this->userId)->getName(); |
||
|
|
|||
| 125 | } elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) { |
||
| 126 | return isset($this->contact['FN']) ? $this->contact['FN'] : ''; |
||
| 127 | } elseif ($this->displayName) { |
||
| 128 | return $this->displayName; |
||
| 129 | } else { |
||
| 130 | return $this->userId; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public function getOrganisation() { |
||
| 135 | if ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) { |
||
| 136 | return isset($this->contact['ORG']) ? $this->contact['ORG'] : ''; |
||
| 137 | } else { |
||
| 138 | return ''; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | public function getEmailAddress() { |
||
| 143 | if ($this->type === self::TYPE_USER) { |
||
| 144 | // Variant: \OC::$server->getConfig()->getUserValue($this->userId, 'settings', 'email'), |
||
| 145 | return \OC::$server->getUserManager()->get($this->userId)->getEMailAddress(); |
||
| 146 | } elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) { |
||
| 147 | return isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : ''; |
||
| 148 | } elseif ($this->type === self::TYPE_EMAIL) { |
||
| 149 | return $this->userId; |
||
| 150 | } else { |
||
| 151 | return $this->emailAddress; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getDesc() { |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getIcon() { |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | private function loadContact() { |
||
| 207 | if ($this->type === self::TYPE_CONTACT && \OC::$server->getContactsManager()->isEnabled()) { |
||
| 208 | // TODO: remove FN in a later version than 1.5 |
||
| 209 | $contacts = \OC::$server->getContactsManager()->search($this->userId, ['UID', 'FN']); |
||
| 210 | if (!$contacts) { |
||
| 211 | $this->contact = []; |
||
| 212 | } else { |
||
| 213 | $this->contact = $contacts[0]; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function jsonSerialize(): array { |
||
| 232 | ]; |
||
| 233 | } |
||
| 235 |
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