| Total Complexity | 40 |
| Total Lines | 155 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PageEditComment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageEditComment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class PageEditComment extends InternalPageBase |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Main function for this page, when no specific actions are called. |
||
| 26 | * @throws ApplicationLogicException |
||
| 27 | * @throws Exception |
||
| 28 | */ |
||
| 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 | |||
| 46 | $this->checkCommentAccess($comment, $currentUser); |
||
| 47 | |||
| 48 | /** @var Request|false $request */ |
||
| 49 | $request = Request::getById($comment->getRequest(), $database); |
||
| 50 | |||
| 51 | if ($request === false) { |
||
| 52 | throw new ApplicationLogicException('Request was not found.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | $canUnflag = $this->barrierTest('unflag', $currentUser, PageFlagComment::class); |
||
| 56 | |||
| 57 | if (WebRequest::wasPosted()) { |
||
| 58 | $this->validateCSRFToken(); |
||
| 59 | $newComment = WebRequest::postString('newcomment'); |
||
| 60 | $visibility = WebRequest::postString('visibility'); |
||
| 61 | $doUnflag = WebRequest::postBoolean('unflag'); |
||
| 62 | |||
| 63 | if ($newComment === null || $newComment === '') { |
||
| 64 | throw new ApplicationLogicException('Comment cannot be empty!'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $commentDataUnchanged = $newComment === $comment->getComment() |
||
| 68 | && ($comment->getVisibility() === 'requester' || $comment->getVisibility() === $visibility); |
||
| 69 | $flagStatusUnchanged = (!$canUnflag || $comment->getFlagged() && !$doUnflag); |
||
| 70 | |||
| 71 | if ($commentDataUnchanged && $flagStatusUnchanged) { |
||
| 72 | // No change was made; redirect back. |
||
| 73 | $this->redirectBack($comment->getRequest()); |
||
| 74 | |||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | // optimistically lock from the load of the edit comment form |
||
| 79 | $updateVersion = WebRequest::postInt('updateversion'); |
||
| 80 | |||
| 81 | // comment data has changed, update the object |
||
| 82 | if (!$commentDataUnchanged) { |
||
| 83 | $this->updateCommentData($comment, $visibility, $newComment); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($doUnflag && $canUnflag) { |
||
| 87 | $comment->setFlagged(false); |
||
| 88 | } |
||
| 89 | |||
| 90 | $comment->setUpdateVersion($updateVersion); |
||
| 91 | $comment->save(); |
||
| 92 | |||
| 93 | if (!$commentDataUnchanged) { |
||
| 94 | Logger::editComment($database, $comment, $request); |
||
| 95 | $this->getNotificationHelper()->commentEdited($comment, $request); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($doUnflag && $canUnflag) { |
||
| 99 | Logger::unflaggedComment($database, $comment, $request->getDomain()); |
||
| 100 | } |
||
| 101 | |||
| 102 | SessionAlert::success('Comment has been saved successfully'); |
||
| 103 | $this->redirectBack($comment->getRequest()); |
||
| 104 | } |
||
| 105 | else { |
||
| 106 | $this->assignCSRFToken(); |
||
| 107 | $this->assign('comment', $comment); |
||
| 108 | $this->assign('request', $request); |
||
| 109 | $this->assign('user', User::getById($comment->getUser(), $database)); |
||
| 110 | $this->assign('canUnflag', $canUnflag); |
||
| 111 | $this->setTemplate('edit-comment.tpl'); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @throws AccessDeniedException |
||
| 117 | */ |
||
| 118 | private function checkCommentAccess(Comment $comment, User $currentUser): void |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @throws ApplicationLogicException |
||
| 153 | */ |
||
| 154 | private function updateCommentData(Comment $comment, ?string $visibility, string $newComment): void |
||
| 155 | { |
||
| 156 | if ($comment->getVisibility() !== 'requester') { |
||
| 157 | if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') { |
||
| 158 | throw new ApplicationLogicException('Comment visibility is not valid'); |
||
| 159 | } |
||
| 160 | |||
| 161 | $comment->setVisibility($visibility); |
||
| 162 | } |
||
| 163 | |||
| 164 | $comment->setComment($newComment); |
||
| 165 | $comment->touchEdited(); |
||
| 166 | } |
||
| 167 | |||
| 168 | private function redirectBack(int $requestId): void |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 |