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 |
||
| 22 | class PageCloseRequest extends RequestActionBase |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Sets up the security for this page. If certain actions have different permissions, this should be reflected in |
||
| 26 | * the return value from this function. |
||
| 27 | * |
||
| 28 | * If this page even supports actions, you will need to check the route |
||
| 29 | * |
||
| 30 | * @return SecurityConfiguration |
||
| 31 | * @category Security-Critical |
||
| 32 | */ |
||
| 33 | protected function getSecurityConfiguration() |
||
| 37 | |||
| 38 | protected function main() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Main function for this page, when no specific actions are called. |
||
| 45 | * @throws ApplicationLogicException |
||
| 46 | */ |
||
| 47 | final protected function processClose() |
||
| 48 | { |
||
| 49 | $this->checkPosted(); |
||
| 50 | $database = $this->getDatabase(); |
||
| 51 | |||
| 52 | $currentUser = User::getCurrent($database); |
||
| 53 | $template = $this->getTemplate($database); |
||
| 54 | $request = $this->getRequest($database); |
||
| 55 | |||
| 56 | if ($request->getStatus() === 'Closed') { |
||
| 57 | throw new ApplicationLogicException('Request is already closed'); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($this->confirmEmailAlreadySent($request, $template)) { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($this->confirmReserveOverride($request, $template, $currentUser, $database)) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($this->confirmAccountCreated($request, $template)) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | // I think we're good here... |
||
| 73 | $request->setStatus('Closed'); |
||
| 74 | $request->setReserved(null); |
||
| 75 | |||
| 76 | Logger::closeRequest($database, $request, $template->getId(), null); |
||
| 77 | |||
| 78 | $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
||
| 79 | $request->save(); |
||
| 80 | |||
| 81 | // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
||
| 82 | // be rolled back. |
||
| 83 | |||
| 84 | $this->getNotificationHelper()->requestClosed($request, $template->getName()); |
||
| 85 | SessionAlert::success("Request {$request->getId()} has been closed"); |
||
| 86 | |||
| 87 | $this->sendMail($request, $template->getText(), $currentUser, false); |
||
| 88 | |||
| 89 | $this->redirect(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param PdoDatabase $database |
||
| 94 | * |
||
| 95 | * @return EmailTemplate |
||
| 96 | * @throws ApplicationLogicException |
||
| 97 | */ |
||
| 98 | View Code Duplication | protected function getTemplate(PdoDatabase $database) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param Request $request |
||
| 116 | * @param EmailTemplate $template |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | View Code Duplication | protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template) |
|
| 130 | |||
| 131 | protected function checkEmailAlreadySent(Request $request) |
||
| 139 | |||
| 140 | protected function checkReserveOverride(Request $request, User $currentUser) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Request $request |
||
| 157 | * @param EmailTemplate $template |
||
| 158 | * @param User $currentUser |
||
| 159 | * @param PdoDatabase $database |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | protected function confirmReserveOverride( |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param Request $request |
||
| 181 | * @param EmailTemplate $template |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | * @throws \Waca\Exceptions\CurlException |
||
| 185 | */ |
||
| 186 | View Code Duplication | protected function confirmAccountCreated(Request $request, EmailTemplate $template) |
|
| 196 | |||
| 197 | protected function checkAccountCreated(Request $request, EmailTemplate $template) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param Request $request |
||
| 223 | * @param string $mailText |
||
| 224 | * @param User $currentUser |
||
| 225 | * @param boolean $ccMailingList |
||
| 226 | */ |
||
| 227 | protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param Request $request |
||
| 255 | * @param EmailTemplate $template |
||
| 256 | * @param string $templateName |
||
| 257 | * |
||
| 258 | * @throws Exception |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | protected function showConfirmation(Request $request, EmailTemplate $template, $templateName) |
||
| 274 | } |
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.