Conditions | 9 |
Paths | 8 |
Total Lines | 60 |
Code Lines | 26 |
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 |
||
28 | protected function main() |
||
29 | { |
||
30 | $id = WebRequest::getInt('id'); |
||
31 | $si = WebRequest::getString('si'); |
||
32 | |||
33 | if ($id === null || $si === null) { |
||
34 | throw new ApplicationLogicException('Link incomplete - please double check the link you received.'); |
||
35 | } |
||
36 | |||
37 | /** @var Request|false $request */ |
||
38 | $request = Request::getById($id, $this->getDatabase()); |
||
39 | |||
40 | if ($request === false) { |
||
|
|||
41 | throw new ApplicationLogicException('Request not found'); |
||
42 | } |
||
43 | |||
44 | if ($request->getEmailConfirm() === 'Confirmed') { |
||
45 | // request has already been confirmed. Bomb out silently. |
||
46 | $this->redirect('requestSubmitted'); |
||
47 | |||
48 | return; |
||
49 | } |
||
50 | |||
51 | if ($request->getEmailConfirm() === $si) { |
||
52 | $request->setEmailConfirm('Confirmed'); |
||
53 | } |
||
54 | else { |
||
55 | throw new ApplicationLogicException('The confirmation value does not appear to match the expected value'); |
||
56 | } |
||
57 | |||
58 | try { |
||
59 | $request->save(); |
||
60 | } |
||
61 | catch (OptimisticLockFailedException $ex) { |
||
62 | // Okay. Someone's edited this in the time between us loading this page and doing the checks, and us getting |
||
63 | // to saving the page. We *do not* want to show an optimistic lock failure, the most likely problem is they |
||
64 | // double-loaded this page (see #255). Let's confirm this, and bomb out with a success message if it's the |
||
65 | // case. |
||
66 | |||
67 | $request = Request::getById($id, $this->getDatabase()); |
||
68 | if ($request->getEmailConfirm() === 'Confirmed') { |
||
69 | // we've already done the sanity checks above |
||
70 | |||
71 | $this->redirect('requestSubmitted'); |
||
72 | |||
73 | // skip the log and notification |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | // something really weird happened. Another race condition? |
||
78 | throw $ex; |
||
79 | } |
||
80 | |||
81 | Logger::emailConfirmed($this->getDatabase(), $request); |
||
82 | |||
83 | if ($request->getStatus() != RequestStatus::CLOSED) { |
||
84 | $this->getNotificationHelper()->requestReceived($request); |
||
85 | } |
||
86 | |||
87 | $this->redirect('requestSubmitted'); |
||
88 | } |
||
89 | } |