ModuleController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 63.64 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 90%

Importance

Changes 8
Bugs 0 Features 7
Metric Value
wmc 9
c 8
b 0
f 7
lcom 1
cbo 9
dl 77
loc 121
ccs 54
cts 60
cp 0.9
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createModuleAction() 19 19 2
A editModuleAction() 19 19 2
A removeModuleAction() 0 18 2
B showModuleAction() 25 25 2
A createFormDelete() 14 14 1

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\Admin;
4
5
use AppBundle\Entity\Module;
6
use AppBundle\Form\ModuleType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
class ModuleController extends Controller
15
{
16
    /**
17
     * @Route("/admin/module/new", name="create_module")
18
     * @Template("@App/admin/module/createModule.html.twig")
19
     */
20 1 View Code Duplication
    public function createModuleAction(Request $request)
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...
21
    {
22 1
        $em = $this->getDoctrine()->getManager();
23
24 1
        $module = new Module();
25 1
        $form = $this->createForm(ModuleType::class, $module);
26
27 1
        $form->handleRequest($request);
28
29 1
        if ($form->isValid()) {
30
            $em->persist($module);
31
            $em->flush();
32
33
            return $this->redirectToRoute('create_question', array('idModule' => $module->getId()));
34
35
        }
36
37 1
        return ['form' => $form->createView()];
38
    }
39
40
    /**
41
     * @Route("/admin/module/edit/{id}", name="edit_module")
42
     * @Template("@App/admin/module/editModule.html.twig")
43
     */
44 1 View Code Duplication
    public function editModuleAction(Request $request, $id)
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...
45
    {
46 1
        $em = $this->getDoctrine()->getManager();
47
48 1
        $module = $em->getRepository('AppBundle:Module')
49 1
            ->find($id);
50 1
        $form = $this->createForm(ModuleType::class, $module);
51
52 1
        $form->handleRequest($request);
53
54 1
        if ($form->isValid()) {
55
            $em->flush();
56
57
            return $this->redirectToRoute('show_module');
58
        }
59
60 1
        return ['module' => $module,
61 1
                'form' => $form->createView()];
62
    }
63
64
    /**
65
     * @Route("/admin/module/remove/{id}", name="remove_module")
66
     * @Method("DELETE")
67
     */
68 1
    public function removeModuleAction($id)
69
    {
70 1
        $em = $this->getDoctrine()->getManager();
71
72 1
        $module = $em->getRepository('AppBundle:Module')
73 1
            ->find($id);
74 1
        $users = $em->getRepository('AppBundle:User')
75 1
            ->findChoiceModules($module->getId());
76 1
        foreach ($users as $item) {
77
                $item->removeChosenModule($module->getId());
78 1
        }
79
80 1
        $em->remove($module);
81 1
        $em->flush();
82
83 1
        return $this->redirectToRoute('show_module');
84
85
    }
86
87
    /**
88
     * @Route("/admin/module", name="show_module")
89
     * @Template("@App/admin/module/showmodule.html.twig")
90
     */
91 1 View Code Duplication
    public function showModuleAction()
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...
92
    {
93 1
        $em = $this->getDoctrine()->getManager();
94
95 1
        $sql = $em->getRepository('AppBundle:Module')
0 ignored issues
show
Bug introduced by
The method findAllModules() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findAll()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
96 1
            ->findAllModules();
97 1
        $paginator = $this->get('knp_paginator');
98 1
        $module = $paginator->paginate(
99 1
            $sql,
100 1
            $this->get('request')->query->get('page', 1),
101 1
            $this->container->getParameter('knp_paginator.page_range')
102 1
        );
103
104 1
        $form_delete = [];
105
106 1
        foreach ($module as $item) {
107 1
            $form_delete[$item->getId()] = $this->createFormDelete($item->getId())->createView();
108 1
        }
109
110 1
        return [
111 1
            'modules' => $module,
112
            'form_remove' => $form_delete
113 1
        ];
114
115
    }
116
117
    /**
118
     * @return \Symfony\Component\Form\Form
119
     */
120 1 View Code Duplication
    private function createFormDelete($id)
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...
121
    {
122 1
        return $this->createFormBuilder()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Form\FormConfigBuilder as the method add() does only exist in the following sub-classes of Symfony\Component\Form\FormConfigBuilder: Symfony\Component\Form\FormBuilder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
123 1
            ->setAction($this->generateUrl('remove_module', ['id' => $id]))
124 1
            ->setMethod('DELETE')
125 1
            ->add('submit', SubmitType::class, [
126 1
                'label' => ' ',
127
                'attr' => [
128 1
                    'class' => 'glyphicon glyphicon-remove btn-link',
129
                    'onclick' => 'return confirm("Are you sure?")'
130 1
                ]
131 1
            ])
132 1
            ->getForm();
133
    }
134
}
135