Completed
Pull Request — dev (#24)
by
unknown
08:52
created

SurveyController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 45.24 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 38
loc 84
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A surveysAction() 0 10 1
A surveyAction() 0 9 1
A surveyCreateAction() 19 19 3
A surveyDeleteAction() 19 19 3

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
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\SurveyAnswer;
6
use AppBundle\Entity\Survey;
7
use AppBundle\Entity\SurveyType;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\Request;
13
14
class SurveyController extends Controller
15
{
16
    /**
17
     * @param Request $request
18
     * @Route("/surveys", name="surveys")
19
     * @Template("@App/surveys.html.twig")
20
     */
21
    public function surveysAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
22
    {
23
        $em = $this->getDoctrine()->getManager();
24
        $surveys = $em->getRepository(Survey::class)->findSurveyByStatus('submited');
25
        $surveyTypes = $em->getRepository(SurveyType::class)->findAll();
26
27
        return [
28
            'surveys' => $surveys, 'survey_types' => $surveyTypes,
29
        ];
30
    }
31
32
    /**
33
     * @param Survey $survey
34
     * @Route("/survey/{id}", name="survey_get")
35
     * @Template("@App/survey.html.twig")
36
     * @ParamConverter("survey", class="AppBundle:Survey")
37
     */
38
    public function surveyAction(Survey $survey)
39
    {
40
        $em = $this->getDoctrine()->getManager();
41
        $fields = $em->getRepository(SurveyAnswer::class)->findAnswersBySurvey($survey);
42
43
        return [
44
            'survey' => $survey, 'fields' => $fields,
45
        ];
46
    }
47
48
    /**
49
     * @param Request $request, SurveyType $surveyType
0 ignored issues
show
Documentation introduced by
There is no parameter named $request,. Did you maybe mean $request?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
50
     * @Route("/survey/create/{survey_type}", name="survey_create")
51
     * @ParamConverter("surveyType", options={"mapping": {"survey_type": "name"}})
52
     */
53 View Code Duplication
    public function surveyCreateAction(Request $request, SurveyType $surveyType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
    {
55
        $survey = new Survey();
56
        $survey->setType($surveyType);
57
        $form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey);
58
        $form->handleRequest($request);
59
60
        if ($form->isSubmitted() && $form->isValid()) {
61
            $em = $this->getDoctrine()->getManager();
62
            $em->persist($survey);
63
            $em->flush();
64
65
            return $this->redirectToRoute('homepage');
66
        }
67
68
        return $this->render('@App/surveyform.html.twig', array(
69
            'form' => $form->createView(), 'type' => $surveyType,
70
        ));
71
    }
72
73
    /**
74
     * @param Request $request, Survey $survey
0 ignored issues
show
Documentation introduced by
There is no parameter named $request,. Did you maybe mean $request?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
75
     * @Route("/survey/delete/{id}", name="survey_delete")
76
     * @ParamConverter("survey", class="AppBundle:Survey")
77
     */
78 View Code Duplication
    public function surveyDeleteAction(Request $request, Survey $survey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
    {
80
        $surveyType = $survey->getType();
81
82
        $form = $this->createForm(\AppBundle\Form\SurveyType::class, $survey);
83
        $form->handleRequest($request);
84
85
        if ($form->isSubmitted() && $form->isValid()) {
86
            $em = $this->getDoctrine()->getManager();
87
            $em->remove($survey);
88
            $em->flush();
89
90
            return $this->redirectToRoute('homepage');
91
        }
92
93
        return $this->render('@App/surveyform.html.twig', array(
94
            'form' => $form->createView(), 'type' => $surveyType,
95
        ));
96
    }
97
}
98