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 |
||
11 | class ForgotPasswordController extends Controller |
||
12 | { |
||
13 | /* |
||
14 | |-------------------------------------------------------------------------- |
||
15 | | Password Reset Controller |
||
16 | |-------------------------------------------------------------------------- |
||
17 | | |
||
18 | | This controller is responsible for handling password reset emails and |
||
19 | | includes a trait which assists in sending these notifications from |
||
20 | | your application to your users. Feel free to explore this trait. |
||
21 | | |
||
22 | */ |
||
23 | |||
24 | use SendsPasswordResetEmails; |
||
25 | |||
26 | /** |
||
27 | * Send a reset link to the given user. |
||
28 | * |
||
29 | * @param \Illuminate\Http\Request $request |
||
30 | * @return \Illuminate\Http\RedirectResponse |
||
31 | */ |
||
32 | public function sendResetLinkEmail(Request $request) |
||
47 | |||
48 | /** |
||
49 | * Get the response for a successful password reset link. |
||
50 | * |
||
51 | * @param string $response |
||
52 | * @return mixed |
||
53 | */ |
||
54 | View Code Duplication | protected function sendResetLinkResponse(Request $request, $response) |
|
|
|||
55 | { |
||
56 | if ($request->expectsJson()) { |
||
57 | return response()->json([ |
||
58 | 'status' => trans($response) |
||
59 | ]); |
||
60 | } |
||
61 | return back()->with('status', trans($response)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the response for a failed password reset link. |
||
66 | * |
||
67 | * @param Request $request |
||
68 | * @param $response |
||
69 | * @return mixed |
||
70 | */ |
||
71 | protected function sendResetLinkFailedResponse(Request $request, $response) |
||
85 | |||
86 | /** |
||
87 | * Display the form to request a password reset link. |
||
88 | * |
||
89 | * @return \Illuminate\Http\Response |
||
90 | */ |
||
91 | public function showLinkRequestForm() |
||
95 | |||
96 | /** |
||
97 | * Create a new controller instance. |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | public function __construct() |
||
105 | } |
||
106 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.