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 |
||
26 | class NotesController extends Controller { |
||
27 | use Errors; |
||
28 | |||
29 | /** @var NotesService */ |
||
30 | private $notesService; |
||
31 | /** @var IConfig */ |
||
32 | private $settings; |
||
33 | /** @var string */ |
||
34 | private $userId; |
||
35 | |||
36 | /** |
||
37 | * @param string $AppName |
||
38 | * @param IRequest $request |
||
39 | * @param NotesService $service |
||
40 | * @param IConfig $settings |
||
41 | * @param string $UserId |
||
42 | */ |
||
43 | View Code Duplication | public function __construct($AppName, IRequest $request, |
|
51 | 8 | ||
52 | /** |
||
53 | * @NoAdminRequired |
||
54 | */ |
||
55 | public function index() { |
||
58 | 1 | ||
59 | /** |
||
60 | * @NoAdminRequired |
||
61 | * |
||
62 | * @param int $id |
||
63 | * @return DataResponse |
||
64 | */ |
||
65 | public function get($id) { |
||
75 | 2 | ||
76 | 2 | /** |
|
77 | * @NoAdminRequired |
||
78 | * |
||
79 | * @param string $content |
||
80 | */ |
||
81 | public function create($content) { |
||
88 | 1 | ||
89 | 1 | /** |
|
90 | 1 | * @NoAdminRequired |
|
91 | * |
||
92 | * @param int $id |
||
93 | * @param string $content |
||
94 | * @return DataResponse |
||
95 | */ |
||
96 | public function update($id, $content) { |
||
101 | 2 | ||
102 | /** |
||
103 | 2 | * @NoAdminRequired |
|
104 | 2 | * |
|
105 | * @param int $id |
||
106 | * @param boolean $favorite |
||
107 | * @return DataResponse |
||
108 | */ |
||
109 | public function favorite($id, $favorite) { |
||
114 | |||
115 | /** |
||
116 | * @NoAdminRequired |
||
117 | * |
||
118 | * @param int $id |
||
119 | * @return DataResponse |
||
120 | */ |
||
121 | public function destroy($id) { |
||
127 | } |
||
128 |
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.