Conditions | 20 |
Paths | > 20000 |
Total Lines | 115 |
Code Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
107 | protected function getUserData(string $userId, bool $includeScopes = false): array { |
||
108 | $currentLoggedInUser = $this->userSession->getUser(); |
||
109 | assert($currentLoggedInUser !== null, 'No user logged in'); |
||
110 | |||
111 | $data = []; |
||
112 | |||
113 | // Check if the target user exists |
||
114 | $targetUserObject = $this->userManager->get($userId); |
||
115 | if ($targetUserObject === null) { |
||
116 | throw new OCSNotFoundException('User does not exist'); |
||
117 | } |
||
118 | |||
119 | $isAdmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID()); |
||
120 | if ($isAdmin |
||
121 | || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) { |
||
122 | $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true'; |
||
123 | } else { |
||
124 | // Check they are looking up themselves |
||
125 | if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) { |
||
126 | return $data; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | // Get groups data |
||
131 | $userAccount = $this->accountManager->getAccount($targetUserObject); |
||
132 | $groups = $this->groupManager->getUserGroups($targetUserObject); |
||
133 | $gids = []; |
||
134 | foreach ($groups as $group) { |
||
135 | $gids[] = $group->getGID(); |
||
136 | } |
||
137 | |||
138 | if ($isAdmin) { |
||
139 | try { |
||
140 | # might be thrown by LDAP due to handling of users disappears |
||
141 | # from the external source (reasons unknown to us) |
||
142 | # cf. https://github.com/nextcloud/server/issues/12991 |
||
143 | $data['storageLocation'] = $targetUserObject->getHome(); |
||
144 | } catch (NoUserException $e) { |
||
145 | throw new OCSNotFoundException($e->getMessage(), $e); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // Find the data |
||
150 | $data['id'] = $targetUserObject->getUID(); |
||
151 | $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000; |
||
152 | $data['backend'] = $targetUserObject->getBackendClassName(); |
||
153 | $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID()); |
||
154 | $data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID()); |
||
155 | $managerUids = $targetUserObject->getManagerUids(); |
||
156 | $data[self::USER_FIELD_MANAGER] = empty($managerUids) ? '' : $managerUids[0]; |
||
157 | |||
158 | try { |
||
159 | if ($includeScopes) { |
||
160 | $data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope(); |
||
161 | } |
||
162 | |||
163 | $data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getSystemEMailAddress(); |
||
164 | if ($includeScopes) { |
||
165 | $data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(); |
||
166 | } |
||
167 | |||
168 | $additionalEmails = $additionalEmailScopes = []; |
||
169 | $emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL); |
||
170 | foreach ($emailCollection->getProperties() as $property) { |
||
171 | $additionalEmails[] = $property->getValue(); |
||
172 | if ($includeScopes) { |
||
173 | $additionalEmailScopes[] = $property->getScope(); |
||
174 | } |
||
175 | } |
||
176 | $data[IAccountManager::COLLECTION_EMAIL] = $additionalEmails; |
||
177 | if ($includeScopes) { |
||
178 | $data[IAccountManager::COLLECTION_EMAIL . self::SCOPE_SUFFIX] = $additionalEmailScopes; |
||
179 | } |
||
180 | |||
181 | $data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName(); |
||
182 | $data[IAccountManager::PROPERTY_DISPLAYNAME_LEGACY] = $data[IAccountManager::PROPERTY_DISPLAYNAME]; |
||
183 | if ($includeScopes) { |
||
184 | $data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(); |
||
185 | } |
||
186 | |||
187 | foreach ([ |
||
188 | IAccountManager::PROPERTY_PHONE, |
||
189 | IAccountManager::PROPERTY_ADDRESS, |
||
190 | IAccountManager::PROPERTY_WEBSITE, |
||
191 | IAccountManager::PROPERTY_TWITTER, |
||
192 | IAccountManager::PROPERTY_FEDIVERSE, |
||
193 | IAccountManager::PROPERTY_ORGANISATION, |
||
194 | IAccountManager::PROPERTY_ROLE, |
||
195 | IAccountManager::PROPERTY_HEADLINE, |
||
196 | IAccountManager::PROPERTY_BIOGRAPHY, |
||
197 | IAccountManager::PROPERTY_PROFILE_ENABLED, |
||
198 | ] as $propertyName) { |
||
199 | $property = $userAccount->getProperty($propertyName); |
||
200 | $data[$propertyName] = $property->getValue(); |
||
201 | if ($includeScopes) { |
||
202 | $data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope(); |
||
203 | } |
||
204 | } |
||
205 | } catch (PropertyDoesNotExistException $e) { |
||
206 | // hard coded properties should exist |
||
207 | throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR, $e); |
||
208 | } |
||
209 | |||
210 | $data['groups'] = $gids; |
||
211 | $data[self::USER_FIELD_LANGUAGE] = $this->l10nFactory->getUserLanguage($targetUserObject); |
||
212 | $data[self::USER_FIELD_LOCALE] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale'); |
||
213 | $data[self::USER_FIELD_NOTIFICATION_EMAIL] = $targetUserObject->getPrimaryEMailAddress(); |
||
214 | |||
215 | $backend = $targetUserObject->getBackend(); |
||
216 | $data['backendCapabilities'] = [ |
||
217 | 'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME), |
||
218 | 'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD), |
||
219 | ]; |
||
220 | |||
221 | return $data; |
||
222 | } |
||
295 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.