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 |
||
48 | class AccountsController extends Controller { |
||
49 | |||
50 | /** @var AccountService */ |
||
51 | private $accountService; |
||
52 | |||
53 | /** @var string */ |
||
54 | private $currentUserId; |
||
55 | |||
56 | /** @var AutoConfig */ |
||
57 | private $autoConfig; |
||
58 | |||
59 | /** @var \OCP\Files\Folder */ |
||
60 | private $userFolder; |
||
61 | |||
62 | /** @var ILogger */ |
||
63 | private $logger; |
||
64 | |||
65 | /** @var IL10N */ |
||
66 | private $l10n; |
||
67 | |||
68 | /** @var ICrypto */ |
||
69 | private $crypto; |
||
70 | |||
71 | /** |
||
72 | * @param string $appName |
||
73 | * @param IRequest $request |
||
74 | * @param AccountService $accountService |
||
75 | * @param $UserId |
||
76 | * @param $userFolder |
||
77 | * @param AutoConfig $autoConfig |
||
78 | * @param Logger $logger |
||
79 | * @param IL10N $l10n |
||
80 | * @param ICrypto $crypto |
||
81 | */ |
||
82 | 15 | View Code Duplication | public function __construct($appName, |
|
|||
83 | IRequest $request, |
||
84 | AccountService $accountService, |
||
85 | $UserId, |
||
86 | $userFolder, |
||
87 | AutoConfig $autoConfig, |
||
88 | Logger $logger, |
||
89 | IL10N $l10n, |
||
90 | ICrypto $crypto |
||
91 | ) { |
||
92 | 15 | parent::__construct($appName, $request); |
|
93 | 15 | $this->accountService = $accountService; |
|
94 | 15 | $this->currentUserId = $UserId; |
|
95 | 15 | $this->userFolder = $userFolder; |
|
96 | 15 | $this->autoConfig = $autoConfig; |
|
97 | 15 | $this->logger = $logger; |
|
98 | 15 | $this->l10n = $l10n; |
|
99 | 15 | $this->crypto = $crypto; |
|
100 | 15 | } |
|
101 | |||
102 | /** |
||
103 | * @NoAdminRequired |
||
104 | * @NoCSRFRequired |
||
105 | * |
||
106 | * @return JSONResponse |
||
107 | */ |
||
108 | 1 | public function index() { |
|
109 | 1 | $mailAccounts = $this->accountService->findByUserId($this->currentUserId); |
|
110 | |||
111 | 1 | $json = []; |
|
112 | 1 | foreach ($mailAccounts as $mailAccount) { |
|
113 | 1 | $json[] = $mailAccount->getConfiguration(); |
|
114 | 1 | } |
|
115 | |||
116 | 1 | return new JSONResponse($json); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * @NoAdminRequired |
||
121 | * |
||
122 | * @param int $accountId |
||
123 | * @return JSONResponse |
||
124 | */ |
||
125 | 2 | public function show($accountId) { |
|
126 | try { |
||
127 | 2 | $account = $this->accountService->find($this->currentUserId, $accountId); |
|
128 | |||
129 | 2 | return new JSONResponse($account->getConfiguration()); |
|
130 | 1 | } catch (DoesNotExistException $e) { |
|
131 | 1 | return new JSONResponse([], 404); |
|
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @NoAdminRequired |
||
137 | */ |
||
138 | public function update() { |
||
143 | |||
144 | /** |
||
145 | * @NoAdminRequired |
||
146 | * |
||
147 | * @param int $accountId |
||
148 | * @return JSONResponse |
||
149 | */ |
||
150 | 2 | public function destroy($accountId) { |
|
159 | |||
160 | /** |
||
161 | * @NoAdminRequired |
||
162 | * |
||
163 | * @param string $accountName |
||
164 | * @param string $emailAddress |
||
165 | * @param string $password |
||
166 | * @param string $imapHost |
||
167 | * @param int $imapPort |
||
168 | * @param string $imapSslMode |
||
169 | * @param string $imapUser |
||
170 | * @param string $imapPassword |
||
171 | * @param string $smtpHost |
||
172 | * @param int $smtpPort |
||
173 | * @param string $smtpSslMode |
||
174 | * @param string $smtpUser |
||
175 | * @param string $smtpPassword |
||
176 | * @param bool $autoDetect |
||
177 | * @return JSONResponse |
||
178 | */ |
||
179 | 2 | public function create($accountName, $emailAddress, $password, |
|
239 | |||
240 | /** |
||
241 | * @NoAdminRequired |
||
242 | * |
||
243 | * @param int $accountId |
||
244 | * @param string $folderId |
||
245 | * @param string $subject |
||
246 | * @param string $body |
||
247 | * @param string $to |
||
248 | * @param string $cc |
||
249 | * @param string $bcc |
||
250 | * @param int $draftUID |
||
251 | * @param string $messageId |
||
252 | * @param mixed $attachments |
||
253 | * @return JSONResponse |
||
254 | */ |
||
255 | 4 | public function send($accountId, $folderId, $subject, $body, $to, $cc, |
|
331 | |||
332 | /** |
||
333 | * @NoAdminRequired |
||
334 | * |
||
335 | * @param int $accountId |
||
336 | * @param string $subject |
||
337 | * @param string $body |
||
338 | * @param string $to |
||
339 | * @param string $cc |
||
340 | * @param string $bcc |
||
341 | * @param int $uid |
||
342 | * @param string $messageId |
||
343 | * @return JSONResponse |
||
344 | */ |
||
345 | 4 | public function draft($accountId, $subject, $body, $to, $cc, $bcc, $uid, $messageId) { |
|
388 | |||
389 | } |
||
390 |
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.