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 |
||
27 | class NotesApiController extends ApiController { |
||
28 | |||
29 | use Errors; |
||
30 | |||
31 | /** @var NotesService */ |
||
32 | private $service; |
||
33 | /** @var IUserSession */ |
||
34 | private $userSession; |
||
35 | |||
36 | /** |
||
37 | * @param string $AppName |
||
38 | * @param IRequest $request |
||
39 | * @param NotesService $service |
||
40 | * @param IUserSession $userSession |
||
41 | 10 | */ |
|
42 | public function __construct($AppName, IRequest $request, |
||
49 | |||
50 | |||
51 | /** |
||
52 | * @param Note $note |
||
53 | * @param string[] $exclude the fields that should be removed from the |
||
54 | * notes |
||
55 | 4 | * @return Note |
|
56 | 4 | */ |
|
57 | 4 | private function excludeFields(Note $note, array $exclude) { |
|
67 | |||
68 | |||
69 | /** |
||
70 | * @NoAdminRequired |
||
71 | * @CORS |
||
72 | * @NoCSRFRequired |
||
73 | * |
||
74 | * @param string $exclude |
||
75 | 2 | * @return DataResponse |
|
76 | 2 | */ |
|
77 | 2 | public function index($exclude='') { |
|
85 | |||
86 | |||
87 | /** |
||
88 | * @NoAdminRequired |
||
89 | * @CORS |
||
90 | * @NoCSRFRequired |
||
91 | * |
||
92 | * @param int $id |
||
93 | * @param string $exclude |
||
94 | 3 | * @return DataResponse |
|
95 | 3 | */ |
|
96 | public function get($id, $exclude='') { |
||
105 | |||
106 | |||
107 | /** |
||
108 | * @NoAdminRequired |
||
109 | * @CORS |
||
110 | * @NoCSRFRequired |
||
111 | * |
||
112 | * @param string $content |
||
113 | 1 | * @param boolean $favorite |
|
114 | * @return DataResponse |
||
115 | 1 | */ |
|
116 | 1 | public function create($content, $favorite=null) { |
|
122 | |||
123 | |||
124 | /** |
||
125 | * @NoAdminRequired |
||
126 | * @CORS |
||
127 | * @NoCSRFRequired |
||
128 | * |
||
129 | * @param int $id |
||
130 | * @param string $content |
||
131 | 2 | * @param boolean $favorite |
|
132 | 2 | * @return DataResponse |
|
133 | */ |
||
134 | public function update($id, $content=null, $favorite=null) { |
||
139 | 2 | ||
140 | /** |
||
141 | 2 | * Updates a note, used by create and update |
|
142 | * @param int $id |
||
143 | * @param string $content |
||
144 | * @param boolean $favorite |
||
145 | * @return Note |
||
146 | */ |
||
147 | private function updateData($id, $content, $favorite) { |
||
157 | 2 | ||
158 | |||
159 | /** |
||
160 | * @NoAdminRequired |
||
161 | * @CORS |
||
162 | * @NoCSRFRequired |
||
163 | * |
||
164 | * @param int $id |
||
165 | * @return DataResponse |
||
166 | */ |
||
167 | public function destroy($id) { |
||
173 | |||
174 | |||
175 | } |
||
176 |
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.