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 MyAccountController extends Controller |
||
12 | { |
||
13 | protected $data = []; |
||
14 | |||
15 | public function __construct() |
||
19 | |||
20 | /** |
||
21 | * Show the user a form to change his personal information. |
||
22 | */ |
||
23 | View Code Duplication | public function getAccountInfoForm() |
|
30 | |||
31 | /** |
||
32 | * Save the modified personal information for a user. |
||
33 | */ |
||
34 | public function postAccountInfoForm(AccountInfoRequest $request) |
||
46 | |||
47 | /** |
||
48 | * Show the user a form to change his login password. |
||
49 | */ |
||
50 | View Code Duplication | public function getChangePasswordForm() |
|
57 | |||
58 | /** |
||
59 | * Save the new password for a user. |
||
60 | */ |
||
61 | public function postChangePasswordForm(ChangePasswordRequest $request) |
||
74 | |||
75 | /** |
||
76 | * Get the guard to be used for account manipulation. |
||
77 | * |
||
78 | * @return \Illuminate\Contracts\Auth\StatefulGuard |
||
79 | */ |
||
80 | protected function guard() |
||
84 | } |
||
85 |
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.