Conditions | 12 |
Paths | 9 |
Total Lines | 58 |
Code Lines | 31 |
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 |
||
26 | protected function main() |
||
27 | { |
||
28 | if (!WebRequest::wasPosted()) { |
||
29 | throw new ApplicationLogicException('This page does not support GET methods.'); |
||
30 | } |
||
31 | |||
32 | $this->validateCSRFToken(); |
||
33 | |||
34 | $flagState = WebRequest::postInt('flag'); |
||
35 | $commentId = WebRequest::postInt('comment'); |
||
36 | $updateVersion = WebRequest::postInt('updateversion'); |
||
37 | |||
38 | if ($flagState !== 0 && $flagState !== 1) { |
||
39 | throw new ApplicationLogicException('Flag status not valid'); |
||
40 | } |
||
41 | |||
42 | $database = $this->getDatabase(); |
||
43 | |||
44 | /** @var Comment|false $comment */ |
||
45 | $comment = Comment::getById($commentId, $database); |
||
46 | if ($comment === false) { |
||
|
|||
47 | throw new ApplicationLogicException('Unknown comment'); |
||
48 | } |
||
49 | |||
50 | $currentUser = User::getCurrent($database); |
||
51 | |||
52 | if ($comment->getFlagged() && !$this->barrierTest('unflag', $currentUser)) { |
||
53 | // user isn't allowed to unflag comments |
||
54 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
||
55 | } |
||
56 | |||
57 | /** @var Request $request */ |
||
58 | $request = Request::getById($comment->getRequest(), $database); |
||
59 | |||
60 | if ($comment->getFlagged() |
||
61 | && !$this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData') |
||
62 | && $request->getReserved() !== $currentUser->getId() |
||
63 | ) { |
||
64 | // can't unflag if you can't see it. |
||
65 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
||
66 | } |
||
67 | |||
68 | $comment->setFlagged($flagState == 1); |
||
69 | $comment->setUpdateVersion($updateVersion); |
||
70 | $comment->save(); |
||
71 | |||
72 | if ($flagState === 1) { |
||
73 | Logger::flaggedComment($database, $comment, $request->getDomain()); |
||
74 | } |
||
75 | else { |
||
76 | Logger::unflaggedComment($database, $comment, $request->getDomain()); |
||
77 | } |
||
78 | |||
79 | if (WebRequest::postString('return') == 'list') { |
||
80 | $this->redirect('flaggedComments'); |
||
81 | } |
||
82 | else { |
||
83 | $this->redirect('viewRequest', null, ['id' => $comment->getRequest()]); |
||
84 | } |
||
86 | } |