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 |
||
36 | class EndpointController extends OCSController { |
||
37 | /** @var Handler */ |
||
38 | private $handler; |
||
39 | |||
40 | /** @var IManager */ |
||
41 | private $manager; |
||
42 | |||
43 | /** @var IUserSession */ |
||
44 | private $session; |
||
45 | |||
46 | /** @var IConfig */ |
||
47 | private $config; |
||
48 | |||
49 | /** |
||
50 | * @param string $appName |
||
51 | * @param IRequest $request |
||
52 | * @param Handler $handler |
||
53 | * @param IManager $manager |
||
54 | * @param IConfig $config |
||
55 | * @param IUserSession $session |
||
56 | */ |
||
57 | 25 | public function __construct($appName, IRequest $request, Handler $handler, IManager $manager, IConfig $config, IUserSession $session) { |
|
65 | |||
66 | /** |
||
67 | * @NoAdminRequired |
||
68 | * @NoCSRFRequired |
||
69 | * |
||
70 | * @param string $apiVersion |
||
71 | * @return DataResponse |
||
72 | */ |
||
73 | 6 | public function listNotifications($apiVersion) { |
|
74 | // When there are no apps registered that use the notifications |
||
75 | // We stop polling for them. |
||
76 | 6 | if (!$this->manager->hasNotifiers()) { |
|
77 | 2 | return new DataResponse(null, Http::STATUS_NO_CONTENT); |
|
78 | } |
||
79 | |||
80 | 4 | $filter = $this->manager->createNotification(); |
|
81 | 4 | $filter->setUser($this->getCurrentUser()); |
|
82 | 4 | $language = $this->config->getUserValue($this->getCurrentUser(), 'core', 'lang', null); |
|
83 | |||
84 | 4 | $notifications = $this->handler->get($filter); |
|
85 | |||
86 | 4 | $data = []; |
|
87 | 4 | $notificationIds = []; |
|
88 | 4 | foreach ($notifications as $notificationId => $notification) { |
|
89 | /** @var INotification $notification */ |
||
90 | try { |
||
91 | 3 | $notification = $this->manager->prepare($notification, $language); |
|
92 | 1 | } catch (\InvalidArgumentException $e) { |
|
93 | // The app was disabled, skip the notification |
||
94 | 1 | continue; |
|
95 | } |
||
96 | |||
97 | 3 | $notificationIds[] = $notificationId; |
|
98 | 3 | $data[] = $this->notificationToArray($notificationId, $notification, $apiVersion); |
|
99 | } |
||
100 | |||
101 | 4 | $etag = $this->generateEtag($notificationIds); |
|
102 | 4 | if ($apiVersion !== 'v1' && $this->request->getHeader('If-None-Match') === $etag) { |
|
103 | return new DataResponse([], Http::STATUS_NOT_MODIFIED); |
||
104 | } |
||
105 | |||
106 | 4 | return new DataResponse($data, Http::STATUS_OK, ['ETag' => $etag]); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @NoAdminRequired |
||
111 | * @NoCSRFRequired |
||
112 | * |
||
113 | * @param string $apiVersion |
||
114 | * @param int $id |
||
115 | * @return DataResponse |
||
116 | */ |
||
117 | 6 | public function getNotification($apiVersion, $id) { |
|
143 | |||
144 | /** |
||
145 | * @NoAdminRequired |
||
146 | * |
||
147 | * @param int $id |
||
148 | * @return DataResponse |
||
149 | */ |
||
150 | 3 | public function deleteNotification($id = 0) { |
|
159 | |||
160 | /** |
||
161 | * @NoAdminRequired |
||
162 | * |
||
163 | * @return DataResponse |
||
164 | */ |
||
165 | 2 | public function deleteAllNotifications(): DataResponse { |
|
169 | |||
170 | /** |
||
171 | * Get an Etag for the notification ids |
||
172 | * |
||
173 | * @param array $notifications |
||
174 | * @return string |
||
175 | */ |
||
176 | 4 | protected function generateEtag(array $notifications) { |
|
179 | |||
180 | /** |
||
181 | * @param int $notificationId |
||
182 | * @param INotification $notification |
||
183 | * @param string $apiVersion |
||
184 | * @return array |
||
185 | */ |
||
186 | 4 | protected function notificationToArray($notificationId, INotification $notification, $apiVersion) { |
|
216 | |||
217 | /** |
||
218 | * @param IAction $action |
||
219 | * @return array |
||
220 | */ |
||
221 | 2 | protected function actionToArray(IAction $action) { |
|
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | 13 | protected function getCurrentUser() { |
|
241 | } |
||
242 |
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.