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 |
||
45 | class NoteApiController extends ApiController { |
||
46 | |||
47 | private $config; |
||
48 | private $noteService; |
||
49 | private $shareBackend; |
||
50 | private $userManager; |
||
51 | private $shareManager; |
||
52 | private $notebookService; |
||
53 | |||
54 | public function __construct($appName, IRequest $request, |
||
65 | |||
66 | /** |
||
67 | * @NoAdminRequired |
||
68 | * @NoCSRFRequired |
||
69 | * @TODO Add etag / lastmodified |
||
70 | * @param int|bool $deleted |
||
71 | * @param string|bool $notebook_id |
||
72 | * @return JSONResponse |
||
73 | */ |
||
74 | public function index($deleted = false, $notebook_id = false) { |
||
91 | |||
92 | /** |
||
93 | * @NoAdminRequired |
||
94 | * @NoCSRFRequired |
||
95 | * @TODO Add etag / lastmodified |
||
96 | */ |
||
97 | public function get($id) { |
||
98 | $result = $this->noteService->find($id); |
||
99 | if (!$result) { |
||
100 | return new NotFoundJSONResponse(); |
||
101 | } |
||
102 | //@todo Check access |
||
103 | $result = $result->jsonSerialize(); |
||
104 | return new JSONResponse($this->formatApiResponse($result)); |
||
105 | } |
||
106 | |||
107 | |||
108 | /** |
||
109 | * @NoAdminRequired |
||
110 | * @NoCSRFRequired |
||
111 | */ |
||
112 | public function create($title, $notebook_id, $content) { |
||
113 | if ($title == "" || !$title) { |
||
114 | return new JSONResponse(['error' => 'title is missing']); |
||
115 | } |
||
116 | |||
117 | $uid = \OC::$server->getUserSession()->getUser()->getUID(); |
||
118 | $note = new Note(); |
||
119 | $note->setName($title); |
||
120 | $note->setUid($uid); |
||
121 | $note->setGuid(Utils::GUID()); |
||
122 | $note->setNote($content); |
||
123 | $note->setMtime(time()); |
||
124 | $note->setDeleted(0); |
||
125 | |||
126 | View Code Duplication | if(!empty($notebook_id)){ |
|
|
|||
127 | $notebook = $this->notebookService->find($notebook_id); |
||
128 | if($notebook instanceof Notebook) { |
||
129 | $note->setNotebook($notebook->getId()); |
||
130 | } else { |
||
131 | return new JSONResponse(['error' => 'Notebook not found']); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | $result = $this->noteService->create($note)->jsonSerialize(); |
||
136 | \OC_Hook::emit('OCA\NextNote', 'post_create_note', ['note' => $note]); |
||
137 | return new JSONResponse($this->formatApiResponse($result)); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @NoAdminRequired |
||
142 | * @NoCSRFRequired |
||
143 | */ |
||
144 | public function update($id, $title, $content, $deleted, $notebook_id) { |
||
145 | if ($title == "" || !$title) { |
||
146 | return new JSONResponse(['error' => 'title is missing']); |
||
147 | } |
||
148 | |||
149 | $note = $this->noteService->find($id); |
||
150 | if (!$note) { |
||
151 | return new NotFoundJSONResponse(); |
||
152 | } |
||
153 | |||
154 | if(!$note->getGuid()){ |
||
155 | $note->setGuid(Utils::GUID()); |
||
156 | } |
||
157 | |||
158 | if (!$this->shareBackend->checkPermissions(Constants::PERMISSION_UPDATE, $note)) { |
||
159 | return new UnauthorizedJSONResponse(); |
||
160 | } |
||
161 | View Code Duplication | if(!empty($notebook_id)){ |
|
162 | $notebook = $this->notebookService->find($notebook_id); |
||
163 | if($notebook instanceof Notebook) { |
||
164 | $note->setNotebook($notebook->getId()); |
||
165 | } else { |
||
166 | return new JSONResponse(['error' => 'Notebook not found']); |
||
167 | } |
||
168 | } |
||
169 | $note->setName($title); |
||
170 | $note->setNote($content); |
||
171 | $note->setDeleted($deleted); |
||
172 | |||
173 | $results = $this->noteService->update($note)->jsonSerialize(); |
||
174 | \OC_Hook::emit('OCA\NextNote', 'post_update_note', ['note' => $note]); |
||
175 | return new JSONResponse($this->formatApiResponse($results)); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @NoAdminRequired |
||
180 | * @NoCSRFRequired |
||
181 | */ |
||
182 | View Code Duplication | public function delete($id) { |
|
197 | |||
198 | /** |
||
199 | * @param $note array |
||
200 | * @return array |
||
201 | */ |
||
202 | private function formatApiResponse($note) { |
||
226 | } |
||
227 |
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.