Completed
Pull Request — master (#45)
by Laurent
04:03
created

FamilyLogController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 162
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 10
Bugs 1 Features 2
Metric Value
wmc 12
c 10
b 1
f 2
lcom 1
cbo 5
dl 162
loc 162
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A showAction() 9 9 1
A newAction() 10 10 1
A editAction() 17 17 1
A updateAction() 23 23 2
A deleteAction() 11 11 2
A indexAction() 9 9 1
B createAction() 26 26 4

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
 * FamilyLogController controller des familles logistiques.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version   since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller\Settings\Divers;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\FamilyLog;
23
use AppBundle\Form\Type\FamilyLogType;
24
25
/**
26
 * FamilyLog controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/settings/divers/familylog")
31
 */
32 View Code Duplication
class FamilyLogController extends AbstractController
1 ignored issue
show
Duplication introduced by
This class 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...
33
{
34
    /**
35
     * Lists all FamilyLog entities.
36
     *
37
     * @Route("/", name="admin_familylog")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $entities = $em->getRepository('AppBundle:FamilyLog')->childrenHierarchy();
45
        
46
        return array(
47
            'entities'  => $entities,
48
        );
49
    }
50
51
    /**
52
     * Finds and displays a FamilyLog entity.
53
     *
54
     * @Route("/{slug}/show", name="admin_familylog_show")
55
     * @Method("GET")
56
     * @Template()
57
     */
58
    public function showAction(FamilyLog $familylog)
59
    {
60
        $deleteForm = $this->createDeleteForm($familylog->getId(), 'admin_familylog_delete');
61
62
        return array(
63
            'familylog' => $familylog,
64
            'delete_form' => $deleteForm->createView(),
65
        );
66
    }
67
68
    /**
69
     * Displays a form to create a new FamilyLog entity.
70
     *
71
     * @Route("/new", name="admin_familylog_new")
72
     * @Method("GET")
73
     * @Template()
74
     */
75
    public function newAction()
76
    {
77
        $familylog = new FamilyLog();
78
        $form = $this->createForm(new FamilyLogType(), $familylog);
79
80
        return array(
81
            'familylog' => $familylog,
82
            'form'   => $form->createView(),
83
        );
84
    }
85
86
    /**
87
     * Creates a new FamilyLog entity.
88
     *
89
     * @Route("/create", name="admin_familylog_create")
90
     * @Method("POST")
91
     * @Template("AppBundle:FamilyLog:new.html.twig")
92
     */
93
    public function createAction(Request $request)
94
    {
95
        $familylog = new FamilyLog();
96
        $form = $this->createForm(new FamilyLogType(), $familylog);
97
        if ($form->handleRequest($request)->isValid()) {
98
            $em = $this->getDoctrine()->getManager();
99
            $em->persist($familylog);
100
            $em->flush();
101
102
            if ($form->get('save')->isSubmitted()) {
103
                $url = $this->redirect($this->generateUrl(
104
                    'admin_familylog_show',
105
                    array('slug' => $familylog->getSlug())
106
                ));
107
            } elseif ($form->get('addmore')->isSubmitted()) {
108
                $this->addFlash('info', 'gestock.settings.add_ok');
109
                $url = $this->redirectToRoute('admin_familylog_new');
110
            }
111
            return $url;
0 ignored issues
show
Bug introduced by
The variable $url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
112
        }
113
114
        return array(
115
            'familylog' => $familylog,
116
            'form'   => $form->createView(),
117
        );
118
    }
119
120
    /**
121
     * Displays a form to edit an existing FamilyLog entity.
122
     *
123
     * @Route("/{slug}/edit", name="admin_familylog_edit")
124
     * @Method("GET")
125
     * @Template()
126
     */
127
    public function editAction(FamilyLog $familylog)
128
    {
129
        $editForm = $this->createForm(new FamilyLogType(), $familylog, array(
130
            'action' => $this->generateUrl(
131
                'admin_familylog_update',
132
                array('slug' => $familylog->getSlug())
133
            ),
134
            'method' => 'PUT',
135
        ));
136
        $deleteForm = $this->createDeleteForm($familylog->getId(), 'admin_familylog_delete');
137
138
        return array(
139
            'familylog' => $familylog,
140
            'edit_form'   => $editForm->createView(),
141
            'delete_form' => $deleteForm->createView(),
142
        );
143
    }
144
145
    /**
146
     * Edits an existing FamilyLog entity.
147
     *
148
     * @Route("/{slug}/update", name="admin_familylog_update")
149
     * @Method("PUT")
150
     * @Template("AppBundle:FamilyLog:edit.html.twig")
151
     */
152
    public function updateAction(FamilyLog $famlog, Request $request)
153
    {
154
        $editForm = $this->createForm(new FamilyLogType(), $famlog, array(
155
            'action' => $this->generateUrl(
156
                'admin_familylog_update',
157
                array('slug' => $famlog->getSlug())
158
            ),
159
            'method' => 'PUT',
160
        ));
161
        if ($editForm->handleRequest($request)->isValid()) {
162
            $this->getDoctrine()->getManager()->flush();
163
            $this->addFlash('info', 'gestock.settings.edit_ok');
164
165
            return $this->redirectToRoute('admin_familylog_edit', array('slug' => $famlog->getSlug()));
166
        }
167
        $deleteForm = $this->createDeleteForm($famlog->getId(), 'admin_familylog_delete');
168
169
        return array(
170
            'familylog' => $famlog,
171
            'edit_form'   => $editForm->createView(),
172
            'delete_form' => $deleteForm->createView(),
173
        );
174
    }
175
176
    /**
177
     * Deletes a FamilyLog entity.
178
     *
179
     * @Route("/{id}/delete", name="admin_familylog_delete", requirements={"id"="\d+"})
180
     * @Method("DELETE")
181
     */
182
    public function deleteAction(FamilyLog $familylog, Request $request)
183
    {
184
        $form = $this->createDeleteForm($familylog->getId(), 'admin_familylog_delete');
185
        if ($form->handleRequest($request)->isValid()) {
186
            $em = $this->getDoctrine()->getManager();
187
            $em->remove($familylog);
188
            $em->flush();
189
        }
190
191
        return $this->redirectToRoute('admin_familylog');
192
    }
193
}
194