Failed Conditions
Pull Request — bugsquish (#574)
by Simon
05:46 queued 03:15
created

PageEditComment::main()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 76
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 76
ccs 0
cts 54
cp 0
rs 5.9166
cc 15
nc 15
nop 0
crap 240

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Exception;
12
use Waca\DataObjects\Comment;
13
use Waca\DataObjects\Request;
14
use Waca\DataObjects\User;
15
use Waca\Exceptions\AccessDeniedException;
16
use Waca\Exceptions\ApplicationLogicException;
17
use Waca\Helpers\Logger;
18
use Waca\SessionAlert;
19
use Waca\Tasks\InternalPageBase;
20
use Waca\WebRequest;
21
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) {
0 ignored issues
show
introduced by
The condition $comment === false is always false.
Loading history...
41
            throw new ApplicationLogicException('Comment not found');
42
        }
43
44
        $currentUser = User::getCurrent($database);
45
46
        if ($comment->getUser() !== $currentUser->getId()) {
47
            if (!$this->barrierTest('editOthers', $currentUser)) {
48
                throw new AccessDeniedException($this->getSecurityManager());
49
            }
50
51
            if ($comment->getVisibility() === 'checkuser'
52
                && !$this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData')) {
53
54
                throw new AccessDeniedException($this->getSecurityManager());
55
            }
56
57
            if ($comment->getVisibility() === 'admin'
58
                && !$this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData')) {
59
60
                throw new AccessDeniedException($this->getSecurityManager());
61
            }
62
        }
63
64
        /** @var Request|false $request */
65
        $request = Request::getById($comment->getRequest(), $database);
66
67
        if ($request === false) {
0 ignored issues
show
introduced by
The condition $request === false is always false.
Loading history...
68
            throw new ApplicationLogicException('Request was not found.');
69
        }
70
71
        if (WebRequest::wasPosted()) {
72
            $this->validateCSRFToken();
73
            $newComment = WebRequest::postString('newcomment');
74
75
            if ($comment->getVisibility() !== 'requester') {
76
                $visibility = WebRequest::postString('visibility');
77
78
                if ($visibility !== 'user' && $visibility !== 'admin' && $visibility !== 'checkuser') {
79
                    throw new ApplicationLogicException('Comment visibility is not valid');
80
                }
81
82
                $comment->setVisibility($visibility);
83
            }
84
85
            // optimistically lock from the load of the edit comment form
86
            $updateVersion = WebRequest::postInt('updateversion');
87
            $comment->setUpdateVersion($updateVersion);
88
89
            $comment->setComment($newComment);
90
91
            $comment->save();
92
93
            Logger::editComment($database, $comment, $request);
94
            $this->getNotificationHelper()->commentEdited($comment, $request);
95
            SessionAlert::success("Comment has been saved successfully");
96
97
            $this->redirect('viewRequest', null, array('id' => $comment->getRequest()));
98
        }
99
        else {
100
            $this->assignCSRFToken();
101
            $this->assign('comment', $comment);
102
            $this->assign('request', $request);
103
            $this->assign('user', User::getById($comment->getUser(), $database));
104
            $this->setTemplate('edit-comment.tpl');
105
        }
106
    }
107
}
108