Completed
Pull Request — master (#244)
by Łukasz
11:47
created

DataSourceBindParametersHandler::validateEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
11
12
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractHandler;
13
use FSi\Bundle\AdminBundle\Event\AdminEvent;
14
use FSi\Bundle\AdminBundle\Event\ListEvent;
15
use FSi\Bundle\AdminBundle\Event\ListEvents;
16
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class DataSourceBindParametersHandler extends AbstractHandler
20
{
21
    /**
22
     * @param AdminEvent $event
23
     * @param Request $request
24
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
25
     * @return null|\Symfony\Component\HttpFoundation\Response
26
     */
27
    public function handleRequest(AdminEvent $event, Request $request)
28
    {
29
        $this->validateEvent($event);
30
31
        if (!$event->hasResponse()) {
32
            $this->eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_PRE_BIND, $event);
33
            if (!$event->hasResponse()) {
34
                $event->getDataSource()->bindParameters($request);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FSi\Bundle\AdminBundle\Event\AdminEvent as the method getDataSource() does only exist in the following sub-classes of FSi\Bundle\AdminBundle\Event\AdminEvent: FSi\Bundle\AdminBundle\Event\ListEvent. 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...
35
                $this->eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_POST_BIND, $event);
36
                if (!$event->hasResponse()) {
37
                    return ;
38
                }
39
            }
40
        }
41
42
        return $event->getResponse();
43
    }
44
45
    /**
46
     * @param AdminEvent $event
47
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
48
     */
49
    protected function validateEvent(AdminEvent $event)
50
    {
51
        if (!$event instanceof ListEvent) {
52
            throw new RequestHandlerException(sprintf("%s require ListEvent", get_class($this)));
53
        }
54
    }
55
}
56