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