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