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