| Conditions | 14 | 
| Paths | 42 | 
| Total Lines | 86 | 
| Code Lines | 41 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 29 | protected function main() | ||
| 30 |     { | ||
| 31 |         $id = WebRequest::getInt('id'); | ||
| 32 |         $si = WebRequest::getString('si'); | ||
| 33 | |||
| 34 |         if ($id === null || $si === null) { | ||
| 35 |             throw new ApplicationLogicException('Link incomplete - please double check the link you received.'); | ||
| 36 | } | ||
| 37 | |||
| 38 | /** @var Request|false $request */ | ||
| 39 | $request = Request::getById($id, $this->getDatabase()); | ||
| 40 | |||
| 41 |         if ($request === false) { | ||
|  | |||
| 42 |             throw new ApplicationLogicException('Request not found'); | ||
| 43 | } | ||
| 44 | |||
| 45 |         if ($request->getEmailConfirm() === 'Confirmed') { | ||
| 46 | // request has already been confirmed. Bomb out silently. | ||
| 47 |             $this->redirect('requestSubmitted'); | ||
| 48 | |||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 |         if ($request->getEmailConfirm() === $si) { | ||
| 53 |             $request->setEmailConfirm('Confirmed'); | ||
| 54 | } | ||
| 55 |         else { | ||
| 56 |             throw new ApplicationLogicException('The confirmation value does not appear to match the expected value'); | ||
| 57 | } | ||
| 58 | |||
| 59 |         try { | ||
| 60 | $request->save(); | ||
| 61 | } | ||
| 62 |         catch (OptimisticLockFailedException $ex) { | ||
| 63 | // Okay. Someone's edited this in the time between us loading this page and doing the checks, and us getting | ||
| 64 | // to saving the page. We *do not* want to show an optimistic lock failure, the most likely problem is they | ||
| 65 | // double-loaded this page (see #255). Let's confirm this, and bomb out with a success message if it's the | ||
| 66 | // case. | ||
| 67 | |||
| 68 | $request = Request::getById($id, $this->getDatabase()); | ||
| 69 |             if ($request->getEmailConfirm() === 'Confirmed') { | ||
| 70 | // we've already done the sanity checks above | ||
| 71 | |||
| 72 |                 $this->redirect('requestSubmitted'); | ||
| 73 | |||
| 74 | // skip the log and notification | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | // something really weird happened. Another race condition? | ||
| 79 | throw $ex; | ||
| 80 | } | ||
| 81 | |||
| 82 | Logger::emailConfirmed($this->getDatabase(), $request); | ||
| 83 | |||
| 84 |         if ($request->getStatus() != RequestStatus::CLOSED) { | ||
| 85 | $this->getNotificationHelper()->requestReceived($request); | ||
| 86 | } | ||
| 87 | |||
| 88 | $userAgent = WebRequest::userAgent(); | ||
| 89 |         if ($userAgent !== null) { | ||
| 90 | RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_USERAGENT, $userAgent); | ||
| 91 | } | ||
| 92 | |||
| 93 | $xffProvider = $this->getXffTrustProvider(); | ||
| 94 | $trustedIp = $xffProvider->getTrustedClientIp(WebRequest::remoteAddress(), WebRequest::forwardedAddress()); | ||
| 95 | |||
| 96 |         if (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | ||
| 97 | RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV4, $trustedIp); | ||
| 98 | } | ||
| 99 |         elseif (filter_var($trustedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | ||
| 100 | RequestData::saveForRequest($request, RequestData::TYPE_CONFIRM_IPV6, $trustedIp); | ||
| 101 | } | ||
| 102 | |||
| 103 |         foreach ($this->getSiteConfiguration()->getAcceptClientHints() as $header) { | ||
| 104 | $value = WebRequest::httpHeader($header); | ||
| 105 | |||
| 106 |             if ($value === null) { | ||
| 107 | continue; | ||
| 108 | } | ||
| 109 | |||
| 110 | RequestData::saveForRequest($request, | ||
| 111 | RequestData::TYPE_CONFIRM_CLIENTHINT, $value, $header); | ||
| 112 | } | ||
| 113 | |||
| 114 |         $this->redirect('requestSubmitted'); | ||
| 115 | } | ||
| 116 | } |