Completed
Push — master ( e3e8c6...010637 )
by MusikAnimal
12s
created

SimpleEditCounterController::resultAction()   D

Complexity

Conditions 13
Paths 154

Size

Total Lines 106
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 4.6605
c 0
b 0
f 0
cc 13
eloc 56
nc 154
nop 1

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
 * This file contains only the SimpleEditCounterController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use Doctrine\DBAL\Connection;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Xtools\Project;
14
use Xtools\ProjectRepository;
15
use Xtools\User;
16
17
/**
18
 * This controller handles the Simple Edit Counter tool.
19
 */
20
class SimpleEditCounterController extends XtoolsController
21
{
22
23
    /**
24
     * Get the tool's shortname.
25
     * @return string
26
     * @codeCoverageIgnore
27
     */
28
    public function getToolShortname()
29
    {
30
        return 'sc';
31
    }
32
33
    /**
34
     * The Simple Edit Counter search form.
35
     * @Route("/sc", name="sc")
36
     * @Route("/sc", name="SimpleEditCounter")
37
     * @Route("/sc/", name="SimpleEditCounterSlash")
38
     * @Route("/sc/index.php", name="SimpleEditCounterIndexPhp")
39
     * @Route("/sc/{project}", name="SimpleEditCounterProject")
40
     * @param Request $request The HTTP request.
41
     * @return Response
42
     */
43
    public function indexAction(Request $request)
44
    {
45
        $params = $this->parseQueryParams($request);
46
47
        // Redirect if project and username are given.
48
        if (isset($params['project']) && isset($params['username'])) {
49
            return $this->redirectToRoute('SimpleEditCounterResult', $params);
50
        }
51
52
        // Convert the given project (or default project) into a Project instance.
53
        $params['project'] = $this->getProjectFromQuery($params);
54
55
        // Show the form.
56
        return $this->render('simpleEditCounter/index.html.twig', [
57
            'xtPageTitle' => 'tool-sc',
58
            'xtSubtitle' => 'tool-sc-desc',
59
            'xtPage' => 'sc',
60
            'project' => $params['project'],
61
        ]);
62
    }
63
64
    /**
65
     * Display the
66
     * @Route("/sc/{project}/{username}", name="SimpleEditCounterResult")
67
     * @param Request $request The HTTP request.
68
     * @return Response
69
     * @codeCoverageIgnore
70
     */
71
    public function resultAction(Request $request)
72
    {
73
        $ret = $this->validateProjectAndUser($request);
74
        if ($ret instanceof RedirectResponse) {
0 ignored issues
show
Bug introduced by
The class AppBundle\Controller\RedirectResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
75
            return $ret;
76
        } else {
77
            list($project, $user) = $ret;
78
        }
79
80
        $userTable = $project->getTableName('user');
81
        $archiveTable = $project->getTableName('archive');
82
        $revisionTable = $project->getTableName('revision');
83
        $userGroupsTable = $project->getTableName('user_groups');
84
85
        /** @var Connection $conn */
86
        $conn = $this->get('doctrine')->getManager('replicas')->getConnection();
87
88
        // Prepare the query and execute
89
        $resultQuery = $conn->prepare("
90
            SELECT 'id' AS source, user_id as value
91
                FROM $userTable
92
                WHERE user_name = :username
93
            UNION
94
            SELECT 'arch' AS source, COUNT(*) AS value
95
                FROM $archiveTable
96
                WHERE ar_user_text = :username
97
            UNION
98
            SELECT 'rev' AS source, COUNT(*) AS value
99
                FROM $revisionTable
100
                WHERE rev_user_text = :username
101
            UNION
102
            SELECT 'groups' AS source, ug_group AS value
103
                FROM $userGroupsTable
104
                JOIN $userTable ON user_id = ug_user
105
                WHERE user_name = :username
106
        ");
107
108
        $username = $user->getUsername();
109
        $resultQuery->bindParam('username', $username);
110
        $resultQuery->execute();
111
112
        // Fetch the result data
113
        $results = $resultQuery->fetchAll();
114
115
        // Initialize the variables - just so we don't get variable undefined errors if there is a problem
116
        $id = '';
117
        $arch = '';
118
        $rev = '';
119
        $groups = '';
120
121
        // Iterate over the results, putting them in the right variables
122
        foreach ($results as $row) {
123
            if ($row['source'] == 'id') {
124
                $id = $row['value'];
125
            }
126
            if ($row['source'] == 'arch') {
127
                $arch = $row['value'];
128
            }
129
            if ($row['source'] == 'rev') {
130
                $rev = $row['value'];
131
            }
132
            if ($row['source'] == 'groups') {
133
                $groups .= $row['value']. ', ';
134
            }
135
        }
136
137
        // Unknown user - If the user is created the $results variable will have 3 entries.
138
        // This is a workaround to detect non-existent IPs.
139
        if (count($results) < 3 && $arch == 0 && $rev == 0) {
140
            $this->addFlash('notice', [ 'no-result', $username]);
0 ignored issues
show
Documentation introduced by
array('no-result', $username) is of type array<integer,*,{"0":"string","1":"*"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
142
            return $this->redirectToRoute('SimpleEditCounterProject', [ 'project' => $project->getDomain() ]);
143
        }
144
145
        // Remove the last comma and space
146
        if (strlen($groups) > 2) {
147
            $groups = substr($groups, 0, -2);
148
        }
149
150
        // If the user isn't in any groups, show a message.
151
        if (strlen($groups) == 0) {
152
            $groups = '---';
153
        }
154
155
        $globalGroups = '';
156
157
        if (boolval($this->getParameter('app.single_wiki'))) {
158
            // Retrieving the global groups, using the ApiHelper class
159
            $api = $this->get('app.api_helper');
160
            $globalGroups = $api->globalGroups($project->getUrl(), $username);
161
        }
162
163
        // Assign the values and display the template
164
        return $this->render('simpleEditCounter/result.html.twig', [
165
            'xtPage' => 'sc',
166
            'xtTitle' => $username,
167
            'user' => $user,
168
            'project' => $project,
169
            'id' => $id,
170
            'arch' => $arch,
171
            'rev' => $rev + $arch,
172
            'live' => $rev,
173
            'groups' => $groups,
174
            'globalGroups' => $globalGroups,
175
        ]);
176
    }
177
}
178