| Conditions | 26 |
| Paths | 18 |
| Total Lines | 93 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 | $commentId = WebRequest::getInt('id'); |
||
| 32 | if ($commentId === null) { |
||
| 33 | throw new ApplicationLogicException('Comment ID not specified'); |
||
| 34 | } |
||
| 35 | |||
| 36 | $database = $this->getDatabase(); |
||
| 37 | |||
| 38 | /** @var Comment|false $comment */ |
||
| 39 | $comment = Comment::getById($commentId, $database); |
||
| 40 | if ($comment === false) { |
||
|
|
|||
| 41 | throw new ApplicationLogicException('Comment not found'); |
||
| 42 | } |
||
| 43 | |||
| 44 | $currentUser = User::getCurrent($database); |
||
| 45 | if ($comment->getUser() !== $currentUser->getId() && !$this->barrierTest('editOthers', $currentUser)) { |
||
| 46 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($comment->getVisibility() === 'admin' |
||
| 50 | && !$this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData') |
||
| 51 | && $comment->getUser() !== $currentUser->getId()) { |
||
| 52 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($comment->getVisibility() === 'checkuser' |
||
| 56 | && !$this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData') |
||
| 57 | && $comment->getUser() !== $currentUser->getId()) { |
||
| 58 | throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager()); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** @var Request|false $request */ |
||
| 62 | $request = Request::getById($comment->getRequest(), $database); |
||
| 63 | |||
| 64 | if ($request === false) { |
||
| 65 | throw new ApplicationLogicException('Request was not found.'); |
||
| 66 | } |
||
| 67 | |||
| 68 | $canUnflag = $this->barrierTest('unflag', $currentUser, PageFlagComment::class); |
||
| 69 | |||
| 70 | if (WebRequest::wasPosted()) { |
||
| 71 | $this->validateCSRFToken(); |
||
| 72 | $newComment = WebRequest::postString('newcomment'); |
||
| 73 | $visibility = WebRequest::postString('visibility'); |
||
| 74 | |||
| 75 | if ($newComment === null || $newComment === "") { |
||
| 76 | throw new ApplicationLogicException("Comment cannot be empty!"); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($newComment === $comment->getComment() && ($comment->getVisibility() === 'requester' || $comment->getVisibility() === $visibility)) { |
||
| 80 | // Only save and log if the comment changed |
||
| 81 | $this->redirect('viewRequest', null, array('id' => $comment->getRequest())); |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($comment->getVisibility() !== 'requester') { |
||
| 86 | if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') { |
||
| 87 | throw new ApplicationLogicException('Comment visibility is not valid'); |
||
| 88 | } |
||
| 89 | |||
| 90 | $comment->setVisibility($visibility); |
||
| 91 | } |
||
| 92 | |||
| 93 | // optimistically lock from the load of the edit comment form |
||
| 94 | $updateVersion = WebRequest::postInt('updateversion'); |
||
| 95 | $comment->setUpdateVersion($updateVersion); |
||
| 96 | |||
| 97 | $comment->setComment($newComment); |
||
| 98 | |||
| 99 | if (WebRequest::postBoolean('unflag') && $canUnflag) { |
||
| 100 | $comment->setFlagged(false); |
||
| 101 | } |
||
| 102 | |||
| 103 | $comment->touchEdited(); |
||
| 104 | $comment->save(); |
||
| 105 | |||
| 106 | Logger::editComment($database, $comment, $request); |
||
| 107 | if (WebRequest::postBoolean('unflag') && $canUnflag) { |
||
| 108 | Logger::unflaggedComment($database, $comment); |
||
| 109 | } |
||
| 110 | $this->getNotificationHelper()->commentEdited($comment, $request); |
||
| 111 | SessionAlert::success("Comment has been saved successfully"); |
||
| 112 | |||
| 113 | $this->redirect('viewRequest', null, array('id' => $comment->getRequest())); |
||
| 114 | } |
||
| 115 | else { |
||
| 116 | $this->assignCSRFToken(); |
||
| 117 | $this->assign('comment', $comment); |
||
| 118 | $this->assign('request', $request); |
||
| 119 | $this->assign('user', User::getById($comment->getUser(), $database)); |
||
| 120 | $this->assign('canUnflag', $canUnflag); |
||
| 121 | $this->setTemplate('edit-comment.tpl'); |
||
| 122 | } |
||
| 125 |