Completed
Pull Request — master (#311)
by Łukasz
06:21
created

FormValidRequestHandler::getRedirectResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * (c) FSi sp. z o.o. <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
13
14
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractFormValidRequestHandler;
15
use FSi\Bundle\AdminBundle\Event\AdminEvent;
16
use FSi\Bundle\AdminBundle\Event\FormEvent;
17
use FSi\Bundle\AdminBundle\Event\FormEvents;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
21
class FormValidRequestHandler extends AbstractFormValidRequestHandler
22
{
23
    protected function action(FormEvent $event, Request $request): void
24
    {
25
        $event->getElement()->save($event->getForm()->getData());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FSi\Bundle\AdminBundle\Admin\Element as the method save() does only exist in the following implementations of said interface: FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement, FSi\Bundle\AdminBundle\A...UD\DependentFormElement, FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement, FSi\Bundle\AdminBundle\A...CRUD\GenericFormElement, FSi\Bundle\AdminBundle\A...\GenericResourceElement, FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement, FSi\Bundle\AdminBundle\D...in\DependentCRUDElement, FSi\Bundle\AdminBundle\D...in\DependentFormElement, FSi\Bundle\AdminBundle\Doctrine\Admin\FormElement, FSi\Bundle\AdminBundle\D...e\Admin\ResourceElement.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
26
    }
27
28
    protected function getPreSaveEventName(): string
29
    {
30
        return FormEvents::FORM_DATA_PRE_SAVE;
31
    }
32
33
    protected function getPostSaveEventName(): string
34
    {
35
        return FormEvents::FORM_DATA_POST_SAVE;
36
    }
37
38
    public function handleRequest(AdminEvent $event, Request $request): ?Response
39
    {
40
        $response = parent::handleRequest($event, $request);
41
        if ($response) {
42
            return $response;
43
        }
44
45
        $this->eventDispatcher->dispatch(FormEvents::FORM_RESPONSE_PRE_RENDER, $event);
46
        if ($event->hasResponse()) {
47
            return $event->getResponse();
48
        }
49
50
        return null;
51
    }
52
}
53