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 |
||
19 | class UserController extends AbstractController |
||
20 | { |
||
21 | /** |
||
22 | * @var Template |
||
23 | */ |
||
24 | private $template; |
||
25 | |||
26 | /** |
||
27 | * @var Router |
||
28 | */ |
||
29 | private $router; |
||
30 | |||
31 | /** |
||
32 | * @var AdminUserService |
||
33 | */ |
||
34 | private $adminUserService; |
||
35 | |||
36 | /** |
||
37 | * @var SessionManager |
||
38 | */ |
||
39 | private $session; |
||
40 | |||
41 | /** |
||
42 | * UserController constructor. |
||
43 | * |
||
44 | * @param Template $template |
||
45 | * @param Router $router |
||
46 | * @param AdminUserService $adminUserService |
||
47 | * @param SessionManager $session |
||
48 | */ |
||
49 | View Code Duplication | public function __construct( |
|
60 | |||
61 | /** |
||
62 | * Users list, exclude current logged in user from the list. |
||
63 | * |
||
64 | * @return \Psr\Http\Message\ResponseInterface |
||
65 | */ |
||
66 | public function index(): \Psr\Http\Message\ResponseInterface |
||
80 | |||
81 | /** |
||
82 | * Edit one user by givven UUID from route. |
||
83 | * |
||
84 | * @return \Psr\Http\Message\ResponseInterface |
||
85 | */ |
||
86 | View Code Duplication | public function edit($errors = []): \Psr\Http\Message\ResponseInterface |
|
106 | |||
107 | View Code Duplication | public function save() |
|
127 | |||
128 | View Code Duplication | public function delete() |
|
139 | } |
||
140 |
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.