Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | class AuthController extends Controller |
||
| 13 | { |
||
| 14 | /* |
||
| 15 | |-------------------------------------------------------------------------- |
||
| 16 | | Registration & Login Controller |
||
| 17 | |-------------------------------------------------------------------------- |
||
| 18 | | |
||
| 19 | | This controller handles the registration of new users, as well as the |
||
| 20 | | authentication of existing users. By default, this controller uses |
||
| 21 | | a simple trait to add these behaviors. Why don't you explore it? |
||
| 22 | | |
||
| 23 | */ |
||
| 24 | |||
| 25 | use AuthenticatesAndRegistersUsers, ThrottlesLogins; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Redirect after logout URL |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $redirectAfterLogout = 'admin'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new authentication controller instance. |
||
| 36 | * |
||
| 37 | * @return void |
||
|
|
|||
| 38 | */ |
||
| 39 | public function __construct() |
||
| 40 | { |
||
| 41 | $this->redirectAfterLogout = env('APP_ADMIN_URL', 'admin'); |
||
| 42 | $this->middleware('guest', ['except' => 'getLogout']); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get a validator for an incoming registration request. |
||
| 47 | * |
||
| 48 | * @param array $data |
||
| 49 | * @return \Illuminate\Contracts\Validation\Validator |
||
| 50 | */ |
||
| 51 | protected function validator(array $data) |
||
| 52 | { |
||
| 53 | return Validator::make($data, [ |
||
| 54 | 'name' => 'required|max:255', |
||
| 55 | 'email' => 'required|email|max:255|unique:users', |
||
| 56 | 'password' => 'required|confirmed|min:6', |
||
| 57 | ]); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create a new user instance after a valid registration. |
||
| 62 | * |
||
| 63 | * @param array $data |
||
| 64 | * @return User |
||
| 65 | */ |
||
| 66 | View Code Duplication | protected function create(array $data) |
|
| 67 | { |
||
| 68 | return User::create([ |
||
| 69 | 'name' => $data['name'], |
||
| 70 | 'email' => $data['email'], |
||
| 71 | 'password' => bcrypt($data['password']), |
||
| 72 | ]); |
||
| 73 | } |
||
| 74 | |||
| 75 | View Code Duplication | public function prePostLogin(Request $request) { |
|
| 86 | |||
| 87 | |||
| 88 | } |
||
| 89 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.