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 |
||
8 | class CommentsController implements InjectionAwareInterface |
||
9 | { |
||
10 | use InjectionAwareTrait; |
||
11 | |||
12 | private $db; |
||
13 | private $session; |
||
14 | private $response; |
||
15 | |||
16 | 1 | public function init($database) |
|
21 | |||
22 | 1 | public function inject($session) |
|
27 | |||
28 | 1 | public function add() |
|
36 | |||
37 | 1 | View Code Duplication | public function delete() |
44 | |||
45 | 1 | View Code Duplication | public function edit() |
52 | |||
53 | 1 | public function get($id) |
|
57 | |||
58 | 1 | public function addCommentSection() |
|
65 | |||
66 | public function renderMain() |
||
73 | |||
74 | public function renderEdit() |
||
81 | } |
||
82 |
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.