Passed
Push — search ( bdb480...5e7f6e )
by Simon
17:14 queued 07:17
created

PageListFlaggedComments::main()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 55
rs 8.0555
cc 9
nc 7
nop 0

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\PdoDatabase;
15
use Waca\Security\RoleConfiguration;
16
use Waca\Tasks\InternalPageBase;
17
18
class PageListFlaggedComments extends InternalPageBase
19
{
20
    /**
21
     * @inheritDoc
22
     */
23
    protected function main()
24
    {
25
        $this->setHtmlTitle('Flagged comments');
26
        $this->setTemplate('flagged-comments.tpl');
27
28
        $database = $this->getDatabase();
29
        $this->assignCSRFToken();
30
31
        /** @var Comment[] $commentObjects */
32
        $commentObjects = Comment::getFlaggedComments($database);
33
        $comments = [];
34
35
        $currentUser = User::getCurrent($database);
36
37
        $seeRestrictedComments = $this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData');
38
        $seeCheckuserComments = $this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData');
39
40
        foreach ($commentObjects as $object) {
41
            $data = [
42
                'id'            => $object->getId(),
43
                'visibility'    => $object->getVisibility(),
44
                'updateversion' => $object->getUpdateVersion(),
45
                'hidden'        => false
46
            ];
47
48
            if ($object->getVisibility() == 'requester' || $object->getVisibility() == 'user') {
49
                $this->copyCommentData($object, $data, $database);
50
            }
51
            elseif ($object->getVisibility() == 'admin') {
52
                if ($seeRestrictedComments) {
53
                    $this->copyCommentData($object, $data, $database);
54
                }
55
                else {
56
                    $data['hidden'] = true;
57
                }
58
            }
59
            elseif ($object->getVisibility() == 'checkuser') {
60
                if ($seeCheckuserComments) {
61
                    $this->copyCommentData($object, $data, $database);
62
                }
63
                else {
64
                    $data['hidden'] = true;
65
                }
66
            }
67
68
            $comments[] = $data;
69
        }
70
71
        $this->assign('comments', $comments);
72
        $this->assign('seeRestrictedComments', $seeRestrictedComments);
73
        $this->assign('seeCheckuserComments', $seeCheckuserComments);
74
75
        $this->assign('editOthersComments', $this->barrierTest('editOthers', $currentUser, PageEditComment::class));
76
        $this->assign('editComments', $this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageEditComment::class));
77
        $this->assign('canUnflag', $this->barrierTest('unflag', $currentUser, PageFlagComment::class) && $this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageFlagComment::class));
78
    }
79
80
    protected function copyCommentData(Comment $object, array &$data, PdoDatabase $database): void
81
    {
82
        $data['comment'] = $object->getComment();
83
        $data['time'] = $object->getTime();
84
        $data['requestid'] = $object->getRequest();
85
        $data['request'] = Request::getById($object->getRequest(), $database)->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Waca\DataObject. It seems like you code against a sub-type of Waca\DataObject such as Waca\DataObjects\RequestForm or Waca\DataObjects\Ban or Waca\DataObjects\EmailTemplate or Waca\DataObjects\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $data['request'] = Request::getById($object->getRequest(), $database)->/** @scrutinizer ignore-call */ getName();
Loading history...
86
        $data['userid'] = $object->getUser();
87
        $data['user'] = User::getById($object->getUser(), $database)->getUsername();
88
    }
89
}