Completed
Push — master ( 5c8e7a...62162d )
by MusikAnimal
22s
created

RfXAnalysisController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 182
Duplicated Lines 9.89 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 9
dl 18
loc 182
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getToolShortname() 0 4 1
B indexAction() 0 54 9
B resultAction() 18 87 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
     */
29
    public function getToolShortname()
30
    {
31
        return 'rfx';
32
    }
33
34
    /**
35
     * Renders the index page for the RfX Tool
36
     *
37
     * @param Request $request Given by Symfony
38
     * @param string  $project Optional project.
39
     * @param string  $type    Optional RfX type
40
     *
41
     * @Route("/rfx",                  name="rfxAnalysis")
42
     * @Route("/rfx",                  name="rfx")
43
     * @Route("/rfx/index.php",        name="rfxAnalysisIndexPhp")
44
     * @Route("/rfx/{project}",        name="rfxAnalysisProject")
45
     * @Route("/rfx/{project}/{type}", name="rfxAnalysisProjectType")
46
     *
47
     * @return Response|RedirectResponse
48
     */
49
    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.

This check looks from 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.

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

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
125
        $domain = $projectData->getDomain();
126
127
        if ($this->getParameter('rfx')[$domain] === null) {
128
            $this->addFlash('notice', ['invalid-project-cant-use', $project]);
0 ignored issues
show
Documentation introduced by
array('invalid-project-cant-use', $project) is of type array<integer,string,{"0":"string","1":"string"}>, 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...
129
            return $this->redirectToRoute('rfx');
130
        }
131
132
        // Construct the page name
133
        if (!isset($this->getParameter('rfx')[$domain]['pages'][$type])) {
134
            $pagename = '';
135
        } else {
136
            $pagename = $this->getParameter('rfx')[$domain]['pages'][$type];
137
        }
138
139
        $user = UserRepository::getUser($username, $this->container);
1 ignored issue
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
140
141
        $pagename .= '/'.$user->getUsername();
142
        $page = new Page($projectData, $pagename);
143
        $pageRepo = new PagesRepository();
144
        $pageRepo->setContainer($this->container);
1 ignored issue
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
145
        $page->setRepository($pageRepo);
146
147
        $text = $page->getWikitext();
148
149 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...
150
            $this->addFlash('notice', ['no-result', $pagename]);
0 ignored issues
show
Documentation introduced by
array('no-result', $pagename) is of type array<integer,string,{"0":"string","1":"string"}>, 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...
151
            return $this->redirectToRoute(
152
                'rfxAnalysisProject',
153
                [
154
                    'project' => $projectData->getDatabaseName()
155
                ]
156
            );
157
        }
158
159
        $rfx = new RFX(
160
            $text,
161
            $this->getParameter('rfx')[$domain]['sections'],
162
            'User'
163
        );
164
        $support = $rfx->getSection('support');
165
        $oppose = $rfx->getSection('oppose');
166
        $neutral = $rfx->getSection('neutral');
167
        $dup = $rfx->getDuplicates();
168
169
        $total = count($support) + count($oppose) + count($neutral);
170
171 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...
172
            $this->addFlash('notice', ['no-result', $pagename]);
0 ignored issues
show
Documentation introduced by
array('no-result', $pagename) is of type array<integer,string,{"0":"string","1":"string"}>, 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...
173
            return $this->redirectToRoute(
174
                'rfxAnalysisProject',
175
                [
176
                    'project' => $projectData->getDatabaseName(),
177
                ]
178
            );
179
        }
180
181
        $end = $rfx->getEndDate();
182
183
        // replace this example code with whatever you need
184
        return $this->render(
185
            'rfxAnalysis/result.html.twig',
186
            [
187
                'xtTitle' => $user->getUsername(),
188
                'xtPage' => 'rfx',
189
                'project' => $projectData,
190
                'user' => $user,
191
                'page' => $page,
192
                'type' => $type,
193
                'support' => $support,
194
                'oppose' => $oppose,
195
                'neutral' => $neutral,
196
                'total' => $total,
197
                'duplicates' => $dup,
198
                'enddate' => $end,
199
            ]
200
        );
201
    }
202
}
203