| Conditions | 19 |
| Paths | 158 |
| Total Lines | 83 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 30 | protected function main() |
||
| 31 | { |
||
| 32 | $commentId = WebRequest::getInt('id'); |
||
| 33 | if ($commentId === null) { |
||
| 34 | throw new ApplicationLogicException('Comment ID not specified'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $database = $this->getDatabase(); |
||
| 38 | |||
| 39 | /** @var Comment|false $comment */ |
||
| 40 | $comment = Comment::getById($commentId, $database); |
||
| 41 | if ($comment === false) { |
||
|
|
|||
| 42 | throw new ApplicationLogicException('Comment not found'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $currentUser = User::getCurrent($database); |
||
| 46 | |||
| 47 | $this->checkCommentAccess($comment, $currentUser); |
||
| 48 | |||
| 49 | /** @var Request|false $request */ |
||
| 50 | $request = Request::getById($comment->getRequest(), $database); |
||
| 51 | |||
| 52 | if ($request === false) { |
||
| 53 | throw new ApplicationLogicException('Request was not found.'); |
||
| 54 | } |
||
| 55 | |||
| 56 | $canUnflag = $this->barrierTest('unflag', $currentUser, PageFlagComment::class); |
||
| 57 | |||
| 58 | if (WebRequest::wasPosted()) { |
||
| 59 | $this->validateCSRFToken(); |
||
| 60 | $newComment = WebRequest::postString('newcomment'); |
||
| 61 | $visibility = WebRequest::postString('visibility'); |
||
| 62 | $doUnflag = WebRequest::postBoolean('unflag'); |
||
| 63 | |||
| 64 | if ($newComment === null || $newComment === '') { |
||
| 65 | throw new ApplicationLogicException('Comment cannot be empty!'); |
||
| 66 | } |
||
| 67 | |||
| 68 | $commentDataUnchanged = $newComment === $comment->getComment() |
||
| 69 | && ($comment->getVisibility() === 'requester' || $comment->getVisibility() === $visibility); |
||
| 70 | $flagStatusUnchanged = (!$canUnflag || $comment->getFlagged() && !$doUnflag); |
||
| 71 | |||
| 72 | if ($commentDataUnchanged && $flagStatusUnchanged) { |
||
| 73 | // No change was made; redirect back. |
||
| 74 | $this->redirectBack($comment->getRequest()); |
||
| 75 | |||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | // optimistically lock from the load of the edit comment form |
||
| 80 | $updateVersion = WebRequest::postInt('updateversion'); |
||
| 81 | |||
| 82 | // comment data has changed, update the object |
||
| 83 | if (!$commentDataUnchanged) { |
||
| 84 | $this->updateCommentData($comment, $visibility, $newComment); |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($doUnflag && $canUnflag) { |
||
| 88 | $comment->setFlagged(false); |
||
| 89 | } |
||
| 90 | |||
| 91 | $comment->setUpdateVersion($updateVersion); |
||
| 92 | $comment->save(); |
||
| 93 | |||
| 94 | if (!$commentDataUnchanged) { |
||
| 95 | Logger::editComment($database, $comment, $request); |
||
| 96 | $this->getNotificationHelper()->commentEdited($comment, $request); |
||
| 97 | } |
||
| 98 | |||
| 99 | if ($doUnflag && $canUnflag) { |
||
| 100 | Logger::unflaggedComment($database, $comment, $request->getDomain()); |
||
| 101 | } |
||
| 102 | |||
| 103 | SessionAlert::success('Comment has been saved successfully'); |
||
| 104 | $this->redirectBack($comment->getRequest()); |
||
| 105 | } |
||
| 106 | else { |
||
| 107 | $this->assignCSRFToken(); |
||
| 108 | $this->assign('comment', $comment); |
||
| 109 | $this->assign('request', $request); |
||
| 110 | $this->assign('user', User::getById($comment->getUser(), $database)); |
||
| 111 | $this->assign('canUnflag', $canUnflag); |
||
| 112 | $this->setTemplate('edit-comment.tpl'); |
||
| 113 | } |
||
| 181 |