Failed Conditions
Push — rbac ( be68b4...52c28b )
by Michael
03:11
created

PageEditComment::main()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 57
ccs 0
cts 42
cp 0
rs 8.0555
cc 9
nc 7
nop 0
crap 90

How to fix   Long Method   

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 Waca\DataObjects\Comment;
12
use Waca\DataObjects\Request;
13
use Waca\DataObjects\User;
14
use Waca\Exceptions\AccessDeniedException;
15
use Waca\Exceptions\ApplicationLogicException;
16
use Waca\Helpers\Logger;
17
use Waca\SessionAlert;
18
use Waca\Tasks\InternalPageBase;
19
use Waca\WebRequest;
20
21
class PageEditComment extends InternalPageBase
22
{
23
    /**
24
     * Main function for this page, when no specific actions are called.
25
     * @throws ApplicationLogicException
26
     */
0 ignored issues
show
Coding Style introduced by
Expected 2 @throws tag(s) in function comment; 1 found
Loading history...
27
    protected function main()
28
    {
29
        $commentId = WebRequest::getInt('id');
30
        if ($commentId === null) {
31
            throw new ApplicationLogicException('Comment ID not specified');
32
        }
33
34
        $database = $this->getDatabase();
35
36
        /** @var Comment $comment */
37
        $comment = Comment::getById($commentId, $database);
38
        if ($comment === false) {
0 ignored issues
show
introduced by
The condition $comment === false is always false.
Loading history...
39
            throw new ApplicationLogicException('Comment not found');
40
        }
41
42
        $currentUser = User::getCurrent($database);
43
        if ($comment->getUser() !== $currentUser->getId() && !$this->barrierTest('editOthers', $currentUser)) {
44
            throw new AccessDeniedException($this->getSecurityManager());
45
        }
46
47
        /** @var Request $request */
48
        $request = Request::getById($comment->getRequest(), $database);
49
50
        if ($request === false) {
0 ignored issues
show
introduced by
The condition $request === false is always false.
Loading history...
51
            throw new ApplicationLogicException('Request was not found.');
52
        }
53
54
        if (WebRequest::wasPosted()) {
55
            $this->validateCSRFToken();
56
            $newComment = WebRequest::postString('newcomment');
57
            $visibility = WebRequest::postString('visibility');
58
59
            if ($visibility !== 'user' && $visibility !== 'admin') {
60
                throw new ApplicationLogicException('Comment visibility is not valid');
61
            }
62
63
            // optimisticly lock from the load of the edit comment form
64
            $updateVersion = WebRequest::postInt('updateversion');
65
            $comment->setUpdateVersion($updateVersion);
66
67
            $comment->setComment($newComment);
68
            $comment->setVisibility($visibility);
69
70
            $comment->save();
71
72
            Logger::editComment($database, $comment, $request);
73
            $this->getNotificationHelper()->commentEdited($comment, $request);
74
            SessionAlert::success("Comment has been saved successfully");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Comment has been saved successfully does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
75
76
            $this->redirect('viewRequest', null, array('id' => $comment->getRequest()));
77
        }
78
        else {
79
            $this->assignCSRFToken();
80
            $this->assign('comment', $comment);
81
            $this->assign('request', $request);
82
            $this->assign('user', User::getById($comment->getUser(), $database));
83
            $this->setTemplate('edit-comment.tpl');
84
        }
85
    }
0 ignored issues
show
Coding Style introduced by
Expected //end main()
Loading history...
86
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
87