Passed
Pull Request — master (#125)
by MusikAnimal
03:53
created

RfXAnalysisController::indexAction()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 31
nc 8
nop 3
dl 0
loc 51
ccs 0
cts 31
cp 0
crap 90
rs 6.2727
c 1
b 0
f 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
 * This file contains the code that powers the RfX Analysis page of XTools.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Xtools\ProjectRepository;
13
use Xtools\Page;
14
use Xtools\PagesRepository;
15
use Xtools\UserRepository;
16
use Xtools\RFX;
17
18
/**
19
 * This controller handles the RfX Analysis tool.
20
 */
21
class RfXAnalysisController extends Controller
22
{
23
24
    /**
25
     * Get the tool's shortname.
26
     *
27
     * @return string
28
     * @codeCoverageIgnore
29
     */
30
    public function getToolShortname()
31
    {
32
        return 'rfx';
33
    }
34
35
    /**
36
     * Renders the index page for the RfX Tool
37
     *
38
     * @param Request $request Given by Symfony
39
     * @param string  $project Optional project.
40
     * @param string  $type    Optional RfX type
41
     *
42
     * @Route("/rfx",                  name="rfxAnalysis")
43
     * @Route("/rfx",                  name="rfx")
44
     * @Route("/rfx/",                 name="rfxSlash")
45
     * @Route("/rfx/index.php",        name="rfxAnalysisIndexPhp")
46
     * @Route("/rfx/{project}",        name="rfxAnalysisProject")
47
     * @Route("/rfx/{project}/{type}", name="rfxAnalysisProjectType")
48
     *
49
     * @return Response|RedirectResponse
0 ignored issues
show
Bug introduced by
The type AppBundle\Controller\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type AppBundle\Controller\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     */
51
    public function indexAction(Request $request, $project = null, $type = null)
0 ignored issues
show
Unused Code introduced by
The parameter $project is not used and could be removed. ( Ignorable by Annotation )

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

51
    public function indexAction(Request $request, /** @scrutinizer ignore-unused */ $project = null, $type = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

51
    public function indexAction(Request $request, $project = null, /** @scrutinizer ignore-unused */ $type = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        if ($request->get('projecttype')
54
            && (strpos($request->get('projecttype'), '|') !== false)
55
        ) {
56
            $projectType = explode('|', $request->get('projecttype'), 2);
57
            $projectQuery = $projectType[0];
58
            $typeQuery = $projectType[1];
59
        } else {
60
            $projectQuery = $request->get('project');
61
            $typeQuery = $request->get('type');
62
        }
63
64
        $username = $request->get('username');
65
66
        if ($projectQuery != '' && $typeQuery != '' && $username != '') {
67
            return $this->redirectToRoute(
68
                'rfxAnalysisResult',
69
                [
70
                    'project' => $projectQuery,
71
                    'type' => $typeQuery,
72
                    'username' => $username
73
                ]
74
            );
75
        } elseif ($projectQuery != '' && $typeQuery != '') {
76
            return $this->redirectToRoute(
77
                'rfxAnalysisProjectType',
78
                [
79
                    'project' => $projectQuery,
80
                    'type' => $typeQuery
81
                ]
82
            );
83
        }
84
85
        $rfx = $this->getParameter('rfx');
86
87
        $projectFields = [];
88
89
        foreach (array_keys($rfx) as $row) {
90
            $projectFields[$row] = $rfx[$row]['pages'];
91
        }
92
93
        // replace this example code with whatever you need
94
        return $this->render(
95
            'rfxAnalysis/index.html.twig',
96
            [
97
                'xtPageTitle' => 'tool-rfx',
98
                'xtSubtitle' => 'tool-rfx-desc',
99
                'xtPage' => 'rfx',
100
                'project' => $projectQuery,
101
                'available' => $projectFields,
102
            ]
103
        );
104
    }
105
106
    /**
107
     * Renders the output page for the RfX Tool
108
     *
109
     * @param string $project  Optional project.
110
     * @param string $type     Type of RfX we are processing.
111
     * @param string $username Username of the person we're analizing.
112
     *
113
     * @Route("/rfx/{project}/{type}/{username}", name="rfxAnalysisResult")
114
     *
115
     * @return Response|RedirectResponse
116
     * @codeCoverageIgnore
117
     */
118
    public function resultAction($project, $type, $username)
119
    {
120
        $projectData = ProjectRepository::getProject($project, $this->container);
121
122
        if (!$projectData->exists()) {
123
            $this->addFlash('notice', ['invalid-project', $project]);
0 ignored issues
show
Bug introduced by
array('invalid-project', $project) of type array<integer,string> is incompatible with the type string expected by parameter $message of Symfony\Bundle\Framework...\Controller::addFlash(). ( Ignorable by Annotation )

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

123
            $this->addFlash('notice', /** @scrutinizer ignore-type */ ['invalid-project', $project]);
Loading history...
124
            return $this->redirectToRoute('rfx');
125
        }
126
127
        $db = $projectData->getDatabaseName();
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
128
        $domain = $projectData->getDomain();
129
130
        if ($this->getParameter('rfx')[$domain] === null) {
131
            $this->addFlash('notice', ['invalid-project-cant-use', $project]);
132
            return $this->redirectToRoute('rfx');
133
        }
134
135
        // Construct the page name
136
        if (!isset($this->getParameter('rfx')[$domain]['pages'][$type])) {
137
            $pagename = '';
138
        } else {
139
            $pagename = $this->getParameter('rfx')[$domain]['pages'][$type];
140
        }
141
142
        $user = UserRepository::getUser($username, $this->container);
143
144
        $pagename .= '/'.$user->getUsername();
145
        $page = new Page($projectData, $pagename);
146
        $pageRepo = new PagesRepository();
147
        $pageRepo->setContainer($this->container);
148
        $page->setRepository($pageRepo);
149
150
        $text = $page->getWikitext();
151
152 View Code Duplication
        if (!isset($text)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
            $this->addFlash('notice', ['no-result', $pagename]);
154
            return $this->redirectToRoute(
155
                'rfxAnalysisProject',
156
                [
157
                    'project' => $projectData->getDatabaseName()
158
                ]
159
            );
160
        }
161
162
        $rfx = new RFX(
163
            $text,
164
            $this->getParameter('rfx')[$domain]['sections'],
165
            'User'
166
        );
167
        $support = $rfx->getSection('support');
168
        $oppose = $rfx->getSection('oppose');
169
        $neutral = $rfx->getSection('neutral');
170
        $dup = $rfx->getDuplicates();
171
172
        $total = count($support) + count($oppose) + count($neutral);
173
174 View Code Duplication
        if ($total === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
            $this->addFlash('notice', ['no-result', $pagename]);
176
            return $this->redirectToRoute(
177
                'rfxAnalysisProject',
178
                [
179
                    'project' => $projectData->getDatabaseName(),
180
                ]
181
            );
182
        }
183
184
        $end = $rfx->getEndDate();
185
186
        // replace this example code with whatever you need
187
        return $this->render(
188
            'rfxAnalysis/result.html.twig',
189
            [
190
                'xtTitle' => $user->getUsername(),
191
                'xtPage' => 'rfx',
192
                'project' => $projectData,
193
                'user' => $user,
194
                'page' => $page,
195
                'type' => $type,
196
                'support' => $support,
197
                'oppose' => $oppose,
198
                'neutral' => $neutral,
199
                'total' => $total,
200
                'duplicates' => $dup,
201
                'enddate' => $end,
202
            ]
203
        );
204
    }
205
}
206