AdminController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 20
rs 9.9666
c 2
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\Controller;
13
14
use FSi\Bundle\AdminBundle\Message\FlashMessages;
15
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
16
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent;
17
use FSi\Bundle\AdminSecurityBundle\Security\User\ChangeablePasswordInterface;
18
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
use Symfony\Component\Routing\RouterInterface;
26
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
27
28
class AdminController
29
{
30
    /**
31
     * @var TokenStorageInterface
32
     */
33
    private $tokenStorage;
34
35
    /**
36
     * @var RouterInterface
37
     */
38
    private $router;
39
40
    /**
41
     * @var EventDispatcherInterface
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @var EngineInterface
47
     */
48
    private $templating;
49
50
    /**
51
     * @var FormFactoryInterface
52
     */
53
    private $formFactory;
54
55
    /**
56
     * @var string
57
     */
58
    private $changePasswordActionTemplate;
59
60
    /**
61
     * @var FlashMessages
62
     */
63
    private $flashMessages;
64
65
    /**
66
     * @var string
67
     */
68
    private $changePasswordFormType;
69
70
    /**
71
     * @var array
72
     */
73
    private $changePasswordFormValidationGroups;
74
75
    public function __construct(
76
        EngineInterface $templating,
77
        FormFactoryInterface $formFactory,
78
        TokenStorageInterface $tokenStorage,
79
        RouterInterface $router,
80
        EventDispatcherInterface $eventDispatcher,
81
        FlashMessages $flashMessages,
82
        string $changePasswordActionTemplate,
83
        string $changePasswordFormType,
84
        array $changePasswordFormValidationGroups
85
    ) {
86
        $this->templating = $templating;
87
        $this->formFactory = $formFactory;
88
        $this->tokenStorage = $tokenStorage;
89
        $this->router = $router;
90
        $this->eventDispatcher = $eventDispatcher;
91
        $this->flashMessages = $flashMessages;
92
        $this->changePasswordActionTemplate = $changePasswordActionTemplate;
93
        $this->changePasswordFormType = $changePasswordFormType;
94
        $this->changePasswordFormValidationGroups = $changePasswordFormValidationGroups;
95
    }
96
97
    public function changePasswordAction(Request $request): Response
98
    {
99
        $user = $this->tokenStorage->getToken()->getUser();
100
        if (!($user instanceof ChangeablePasswordInterface)) {
101
            throw new NotFoundHttpException();
102
        }
103
104
        $form = $this->formFactory->create(
105
            $this->changePasswordFormType,
106
            $user,
107
            ['validation_groups' => $this->changePasswordFormValidationGroups]
108
        );
109
110
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
111
112
            $this->eventDispatcher->dispatch(
113
                AdminSecurityEvents::CHANGE_PASSWORD,
0 ignored issues
show
Bug introduced by
FSi\Bundle\AdminSecurity...Events::CHANGE_PASSWORD of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
                /** @scrutinizer ignore-type */ AdminSecurityEvents::CHANGE_PASSWORD,
Loading history...
114
                new ChangePasswordEvent($user)
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with new FSi\Bundle\AdminSecu...ngePasswordEvent($user). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
115
                                    dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
115
            );
116
117
            $this->flashMessages->success(
118
                'admin.change_password_message.success',
119
                [],
120
                'FSiAdminSecurity'
121
            );
122
123
            return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
124
        }
125
126
        return $this->templating->renderResponse(
127
            $this->changePasswordActionTemplate,
128
            ['form' => $form->createView()]
129
        );
130
    }
131
}
132