DayController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 207
Duplicated Lines 10.14 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 15
Bugs 2 Features 2
Metric Value
wmc 17
lcom 1
cbo 6
dl 21
loc 207
c 15
b 2
f 2
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 0 4 1
A createAction() 0 4 1
A viewAction() 0 4 1
A deleteAction() 0 4 1
A crudListFormType() 0 4 1
A crudCreateFormType() 0 4 1
A crudEditRole() 0 4 1
A crudViewRole() 0 4 1
A crudDeleteRole() 0 4 1
A crudName() 0 4 1
A crudObjectClass() 0 4 1
A __construct() 21 21 1
A editAction() 0 4 1
A crudListPerPage() 0 4 1
A crudListFormDataEnrich() 0 6 1
A crudCreateFactory() 0 10 1
A crudEditFormType() 0 11 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 Dominikzogg\EnergyCalculator\Controller;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Dominikzogg\EnergyCalculator\Entity\Comestible;
7
use Dominikzogg\EnergyCalculator\Entity\Day;
8
use Dominikzogg\EnergyCalculator\Form\DayListType;
9
use Dominikzogg\EnergyCalculator\Form\DayType;
10
use Dominikzogg\EnergyCalculator\Repository\ComestibleRepository;
11
use Knp\Component\Pager\Paginator;
12
use Saxulum\Crud\Listing\ListingFactory;
13
use Saxulum\RouteController\Annotation\DI;
14
use Saxulum\RouteController\Annotation\Route;
15
use Symfony\Component\Form\FormFactory;
16
use Symfony\Component\Form\FormTypeInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
22
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
23
use Symfony\Component\Translation\TranslatorInterface;
24
25
/**
26
 * @Route("/{_locale}/day", asserts={"_locale"="([a-z]{2}|[a-z]{2}_[A-Z]{2})"})
27
 * @DI(serviceIds={
28
 *      "security.authorization_checker",
29
 *      "security.token_storage",
30
 *      "saxulum.crud.listing.factory",
31
 *      "doctrine",
32
 *      "form.factory",
33
 *      "knp_paginator",
34
 *      "url_generator",
35
 *      "twig",
36
 *      "translator"
37
 * })
38
 */
39
class DayController extends AbstractCRUDController
40
{
41
    /**
42
     * @var TranslatorInterface
43
     */
44
    protected $translator;
45
46
    /**
47
     * @param AuthorizationCheckerInterface $authorizationChecker
48
     * @param TokenStorageInterface $tokenStorage
49
     * @param ListingFactory $listingFactory
50
     * @param ManagerRegistry          $doctrine
51
     * @param FormFactory              $formFactory
52
     * @param Paginator                $paginator
53
     * @param UrlGeneratorInterface    $urlGenerator
54
     * @param \Twig_Environment        $twig
55
     * @param TranslatorInterface      $translator
56
     */
57 View Code Duplication
    public function __construct(
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...
58
        AuthorizationCheckerInterface $authorizationChecker,
59
        TokenStorageInterface $tokenStorage,
60
        ListingFactory $listingFactory,
61
        ManagerRegistry $doctrine,
62
        FormFactory $formFactory,
63
        Paginator $paginator,
64
        UrlGeneratorInterface $urlGenerator,
65
        \Twig_Environment $twig,
66
        TranslatorInterface $translator
67
    ) {
68
        $this->authorizationChecker = $authorizationChecker;
69
        $this->tokenStorage = $tokenStorage;
70
        $this->listingFactory = $listingFactory;
71
        $this->doctrine = $doctrine;
72
        $this->formFactory = $formFactory;
73
        $this->paginator = $paginator;
74
        $this->urlGenerator = $urlGenerator;
75
        $this->twig = $twig;
76
        $this->translator = $translator;
77
    }
78
79
    /**
80
     * @Route("/", bind="day_list", method="GET")
81
     * @param  Request  $request
82
     * @return Response
83
     */
84
    public function listAction(Request $request)
85
    {
86
        return parent::crudListObjects($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudListObjects() instead of listAction()). Are you sure this is correct? If so, you might want to change this to $this->crudListObjects().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
87
    }
88
89
    /**
90
     * @Route("/create", bind="day_create")
91
     * @param  Request                   $request
92
     * @return Response|RedirectResponse
93
     */
94
    public function createAction(Request $request)
95
    {
96
        return parent::crudCreateObject($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudCreateObject() instead of createAction()). Are you sure this is correct? If so, you might want to change this to $this->crudCreateObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
97
    }
98
99
    /**
100
     * @Route("/edit/{id}", bind="day_edit", asserts={"id"="[0-9a-f]{1,24}"})
101
     * @param  Request                   $request
102
     * @param $id
103
     * @return Response|RedirectResponse
104
     */
105
    public function editAction(Request $request, $id)
106
    {
107
        return parent::crudEditObject($request, $id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudEditObject() instead of editAction()). Are you sure this is correct? If so, you might want to change this to $this->crudEditObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
108
    }
109
110
    /**
111
     * @Route("/view/{id}", bind="day_view", asserts={"id"="[0-9a-f]{1,24}"}, method="GET")
112
     * @param  Request  $request
113
     * @param $id
114
     * @return Response
115
     */
116
    public function viewAction(Request $request, $id)
117
    {
118
        return parent::crudViewObject($request, $id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudViewObject() instead of viewAction()). Are you sure this is correct? If so, you might want to change this to $this->crudViewObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
119
    }
120
121
    /**
122
     * @Route("/delete/{id}", bind="day_delete", asserts={"id"="[0-9a-f]{1,24}"}, method="GET")
123
     * @param  Request          $request
124
     * @param $id
125
     * @return RedirectResponse
126
     */
127
    public function deleteAction(Request $request, $id)
128
    {
129
        return parent::crudDeleteObject($request, $id);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (crudDeleteObject() instead of deleteAction()). Are you sure this is correct? If so, you might want to change this to $this->crudDeleteObject().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
130
    }
131
132
    /**
133
     * @return int
134
     */
135
    protected function crudListPerPage()
136
    {
137
        return 7;
138
    }
139
140
    /**
141
     * @param Request $request
142
     *
143
     * @return FormTypeInterface
144
     */
145
    protected function crudListFormType(Request $request)
146
    {
147
        return new DayListType();
148
    }
149
150
    /**
151
     * @param  Request $request
152
     * @param  array   $formData
153
     * @return array
154
     */
155
    protected function crudListFormDataEnrich(Request $request, array $formData)
156
    {
157
        return array_replace_recursive($formData, array(
158
            'user' => $this->getUser()->getId(),
159
        ));
160
    }
161
162
    /**
163
     * @param  Request $request
164
     * @return Day
165
     */
166
    protected function crudCreateFactory(Request $request)
167
    {
168
        $objectClass = $this->crudObjectClass();
169
170
        /** @var Day $object */
171
        $object = new $objectClass();
172
        $object->setUser($this->getUser());
173
174
        return $object;
175
    }
176
177
    /**
178
     * @param Request $request
179
     * @param object $object
180
     *
181
     * @return FormTypeInterface
182
     */
183
    protected function crudCreateFormType(Request $request, $object)
184
    {
185
        return $this->crudEditFormType($request, $object);
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    protected function crudEditRole()
192
    {
193
        return strtoupper('edit');
194
    }
195
196
    /**
197
     * @param Request $request
198
     * @param object $object
199
     *
200
     * @return FormTypeInterface
201
     */
202
    protected function crudEditFormType(Request $request, $object)
203
    {
204
        /** @var ComestibleRepository $repo */
205
        $repo = $this->crudRepositoryForClass(Comestible::class);
206
207
        return new DayType(
208
            $this->getUser(),
209
            $this->translator,
210
            $repo->searchComestibleOfUserQb($this->getUser())
211
        );
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    protected function crudViewRole()
218
    {
219
        return strtoupper('view');
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    protected function crudDeleteRole()
226
    {
227
        return strtoupper('delete');
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    protected function crudName()
234
    {
235
        return 'day';
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    protected function crudObjectClass()
242
    {
243
        return Day::class;
244
    }
245
}
246