| Conditions | 51 |
| Paths | > 20000 |
| Total Lines | 209 |
| Code Lines | 129 |
| 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 |
||
| 101 | public function edit(Request $request) |
||
| 102 | { |
||
| 103 | |||
| 104 | $action = $request->input('action') ?? 'view'; |
||
| 105 | |||
| 106 | $userid = $this->userdata->id; |
||
| 107 | if (! $this->userdata) { |
||
| 108 | $this->show404('No such user!'); |
||
| 109 | } |
||
| 110 | |||
| 111 | $errorStr = ''; |
||
| 112 | $success_2fa = $request->session()->get('success'); |
||
| 113 | $error_2fa = $request->session()->get('error'); |
||
| 114 | |||
| 115 | // Generate 2FA QR code URL if 2FA is set up but not enabled |
||
| 116 | $google2fa_url = ''; |
||
| 117 | if ($this->userdata->passwordSecurity()->exists() && ! $this->userdata->passwordSecurity->google2fa_enable) { |
||
| 118 | $google2fa_url = \Google2FA::getQRCodeInline( |
||
| 119 | config('app.name'), |
||
| 120 | $this->userdata->email, |
||
| 121 | $this->userdata->passwordSecurity->google2fa_secret |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | switch ($action) { |
||
| 126 | case 'newapikey': |
||
| 127 | User::updateRssKey($userid); |
||
| 128 | |||
| 129 | return redirect()->to('profile'); |
||
| 130 | case 'clearcookies': |
||
| 131 | return redirect()->to('profileedit'); |
||
| 132 | case 'submit': |
||
| 133 | $validator = Validator::make($request->all(), [ |
||
| 134 | 'email' => ['nullable', 'string', 'email', 'max:255', 'unique:users,email,'.$userid, 'indisposable'], |
||
| 135 | 'password' => ['nullable', 'string', 'min:8', 'confirmed', 'regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/'], |
||
| 136 | ]); |
||
| 137 | |||
| 138 | if ($validator->fails()) { |
||
| 139 | $errorStr = implode('', Arr::collapse($validator->errors()->toArray())); |
||
| 140 | } else { |
||
| 141 | User::updateUser( |
||
| 142 | $userid, |
||
| 143 | $this->userdata->username, |
||
| 144 | $request->input('email'), |
||
| 145 | $this->userdata->grabs, |
||
| 146 | $this->userdata->roles_id, |
||
| 147 | $this->userdata->notes, |
||
| 148 | $this->userdata->invites, |
||
| 149 | $request->has('viewmovies') ? 1 : 0, |
||
| 150 | $request->has('viewaudio') ? 1 : 0, |
||
| 151 | $request->has('viewpc') ? 1 : 0, |
||
| 152 | $request->has('viewadult') ? 1 : 0, |
||
| 153 | $request->has('viewconsole') ? 1 : 0, |
||
| 154 | $request->has('viewbooks') ? 1 : 0, |
||
| 155 | 'None', |
||
| 156 | ); |
||
| 157 | |||
| 158 | // Update theme preference |
||
| 159 | if ($request->has('theme_preference')) { |
||
| 160 | $themeValue = $request->input('theme_preference'); |
||
| 161 | if (in_array($themeValue, ['light', 'dark', 'system'])) { |
||
| 162 | User::where('id', $userid)->update(['theme_preference' => $themeValue]); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // Update timezone preference |
||
| 167 | if ($request->has('timezone')) { |
||
| 168 | $timezoneValue = $request->input('timezone'); |
||
| 169 | $validTimezones = array_merge(['UTC'], ...array_values(getAvailableTimezones())); |
||
| 170 | if (in_array($timezoneValue, $validTimezones)) { |
||
| 171 | User::where('id', $userid)->update(['timezone' => $timezoneValue]); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | // Handle Console permission |
||
| 176 | if ($request->has('viewconsole')) { |
||
| 177 | if (! $this->userdata->hasDirectPermission('view console')) { |
||
| 178 | $this->userdata->givePermissionTo('view console'); |
||
| 179 | } |
||
| 180 | } else { |
||
| 181 | if ($this->userdata->hasPermissionTo('view console')) { |
||
| 182 | $this->userdata->revokePermissionTo('view console'); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | // Handle Movies permission |
||
| 187 | if ($request->has('viewmovies')) { |
||
| 188 | if (! $this->userdata->hasDirectPermission('view movies')) { |
||
| 189 | $this->userdata->givePermissionTo('view movies'); |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | if ($this->userdata->hasPermissionTo('view movies')) { |
||
| 193 | $this->userdata->revokePermissionTo('view movies'); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | // Handle Audio permission |
||
| 198 | if ($request->has('viewaudio')) { |
||
| 199 | if (! $this->userdata->hasDirectPermission('view audio')) { |
||
| 200 | $this->userdata->givePermissionTo('view audio'); |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | if ($this->userdata->hasPermissionTo('view audio')) { |
||
| 204 | $this->userdata->revokePermissionTo('view audio'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | // Handle PC/Games permission |
||
| 209 | if ($request->has('viewpc')) { |
||
| 210 | if (! $this->userdata->hasDirectPermission('view pc')) { |
||
| 211 | $this->userdata->givePermissionTo('view pc'); |
||
| 212 | } |
||
| 213 | } else { |
||
| 214 | if ($this->userdata->hasPermissionTo('view pc')) { |
||
| 215 | $this->userdata->revokePermissionTo('view pc'); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | // Handle TV permission |
||
| 220 | if ($request->has('viewtv')) { |
||
| 221 | if (! $this->userdata->hasDirectPermission('view tv')) { |
||
| 222 | $this->userdata->givePermissionTo('view tv'); |
||
| 223 | } |
||
| 224 | } else { |
||
| 225 | if ($this->userdata->hasPermissionTo('view tv')) { |
||
| 226 | $this->userdata->revokePermissionTo('view tv'); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | // Handle Adult permission |
||
| 231 | if ($request->has('viewadult')) { |
||
| 232 | if (! $this->userdata->hasDirectPermission('view adult')) { |
||
| 233 | $this->userdata->givePermissionTo('view adult'); |
||
| 234 | } |
||
| 235 | } else { |
||
| 236 | if ($this->userdata->hasPermissionTo('view adult')) { |
||
| 237 | $this->userdata->revokePermissionTo('view adult'); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | // Handle Books permission |
||
| 242 | if ($request->has('viewbooks')) { |
||
| 243 | if (! $this->userdata->hasDirectPermission('view books')) { |
||
| 244 | $this->userdata->givePermissionTo('view books'); |
||
| 245 | } |
||
| 246 | } else { |
||
| 247 | if ($this->userdata->hasPermissionTo('view books')) { |
||
| 248 | $this->userdata->revokePermissionTo('view books'); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // Handle Other permission |
||
| 253 | if ($request->has('viewother')) { |
||
| 254 | if (! $this->userdata->hasDirectPermission('view other')) { |
||
| 255 | $this->userdata->givePermissionTo('view other'); |
||
| 256 | } |
||
| 257 | } else { |
||
| 258 | if ($this->userdata->hasPermissionTo('view other')) { |
||
| 259 | $this->userdata->revokePermissionTo('view other'); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($request->has('password') && ! empty($request->input('password'))) { |
||
| 264 | User::updatePassword($userid, $request->input('password')); |
||
| 265 | } |
||
| 266 | |||
| 267 | if (! $this->userdata->hasRole('Admin')) { |
||
| 268 | if (! empty($request->input('email')) && $this->userdata->email !== $request->input('email')) { |
||
| 269 | $this->userdata->email = $request->input('email'); |
||
| 270 | |||
| 271 | $verify_user = $this->userdata; |
||
| 272 | |||
| 273 | UserVerification::generate($verify_user); |
||
| 274 | |||
| 275 | UserVerification::send($verify_user, 'User email verification required'); |
||
| 276 | |||
| 277 | Auth::logout(); |
||
| 278 | |||
| 279 | return redirect()->to('login')->with('info', 'You will be able to login after you verify your new email address'); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return redirect()->to('profile')->with('success', 'Profile changes saved'); |
||
| 284 | } |
||
| 285 | break; |
||
| 286 | |||
| 287 | case 'view': |
||
| 288 | default: |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | |||
| 292 | $this->viewData = array_merge($this->viewData, [ |
||
| 293 | 'error' => $errorStr, |
||
| 294 | 'user' => $this->userdata, |
||
| 295 | 'userexccat' => User::getCategoryExclusionById($userid), |
||
| 296 | 'success_2fa' => $success_2fa, |
||
| 297 | 'error_2fa' => $error_2fa, |
||
| 298 | 'google2fa_url' => $google2fa_url, |
||
| 299 | 'yesno_ids' => [1, 0], |
||
| 300 | 'yesno_names' => ['Yes', 'No'], |
||
| 301 | 'publicview' => false, |
||
| 302 | 'privileged' => $this->userdata->hasRole('Admin') || $this->userdata->hasRole('Moderator'), |
||
| 303 | 'userinvitedby' => ($this->userdata->invitedby && $this->userdata->invitedby !== '') ? User::find($this->userdata->invitedby) : null, |
||
| 304 | 'meta_title' => 'Edit User Profile', |
||
| 305 | 'meta_keywords' => 'edit,profile,user,details', |
||
| 306 | 'meta_description' => 'Edit User Profile for '.$this->userdata->username, |
||
| 307 | ]); |
||
| 308 | |||
| 309 | return view('profile.edit', $this->viewData); |
||
| 310 | } |
||
| 358 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: