| Conditions | 23 |
| Paths | 191 |
| Total Lines | 92 |
| 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 |
||
| 93 | public function createUser($username, $password, array $groups= [], $email='') { |
||
| 94 | if ($email !== '' && !$this->mailer->validateMailAddress($email)) { |
||
| 95 | throw new InvalidEmailException("Invalid mail address"); |
||
| 96 | } |
||
| 97 | |||
| 98 | $currentUser = $this->userSession->getUser(); |
||
| 99 | |||
| 100 | if (!$this->isAdmin()) { |
||
| 101 | if (!empty($groups)) { |
||
| 102 | foreach ($groups as $key => $group) { |
||
| 103 | $groupObject = $this->groupManager->get($group); |
||
| 104 | if ($groupObject === null) { |
||
| 105 | unset($groups[$key]); |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) { |
||
|
|
|||
| 110 | unset($groups[$key]); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | if (empty($groups)) { |
||
| 116 | $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($currentUser); |
||
| 117 | // New class returns IGroup[] so convert back |
||
| 118 | $gids = []; |
||
| 119 | foreach ($groups as $group) { |
||
| 120 | $gids[] = $group->getGID(); |
||
| 121 | } |
||
| 122 | $groups = $gids; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($this->userManager->userExists($username)) { |
||
| 127 | throw new UserAlreadyExistsException('A user with that name already exists.'); |
||
| 128 | } |
||
| 129 | |||
| 130 | try { |
||
| 131 | if (($password === '') && ($email !== '')) { |
||
| 132 | /** |
||
| 133 | * Generate a random password as we are going to have this |
||
| 134 | * use one time. The new user has to reset it using the link |
||
| 135 | * from email. |
||
| 136 | */ |
||
| 137 | $event = new GenericEvent(); |
||
| 138 | $this->eventDispatcher->dispatch('OCP\User::createPassword', $event); |
||
| 139 | if ($event->hasArgument('password')) { |
||
| 140 | $password = $event->getArgument('password'); |
||
| 141 | } else { |
||
| 142 | $password = $this->secureRandom->generate(20); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | $user = $this->userManager->createUser($username, $password); |
||
| 146 | } catch (\Exception $exception) { |
||
| 147 | $message = $exception->getMessage(); |
||
| 148 | if (!$message) { |
||
| 149 | $message = 'Unable to create user.'; |
||
| 150 | } |
||
| 151 | throw new CannotCreateUserException($message); |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($user === null) { |
||
| 155 | throw new CannotCreateUserException('Unable to create user.'); |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($groups !== null) { |
||
| 159 | foreach ($groups as $groupName) { |
||
| 160 | if ($groupName !== null) { |
||
| 161 | $group = $this->groupManager->get($groupName); |
||
| 162 | |||
| 163 | if (empty($group)) { |
||
| 164 | $group = $this->groupManager->createGroup($groupName); |
||
| 165 | } |
||
| 166 | $group->addUser($user); |
||
| 167 | $this->logger->info('Added userid ' . $user->getUID() . ' to group ' . $group->getGID()); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | /** |
||
| 172 | * Send new user mail only if a mail is set |
||
| 173 | */ |
||
| 174 | if ($email !== '') { |
||
| 175 | $user->setEMailAddress($email); |
||
| 176 | try { |
||
| 177 | $this->userSendMail->generateTokenAndSendMail($username, $email); |
||
| 178 | } catch (\Exception $e) { |
||
| 179 | $this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), ['app' => 'settings']); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | return $user; |
||
| 184 | } |
||
| 185 | |||
| 209 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: