Completed
Push — tests-organisation ( e0cc56...d1374b )
by Kamil
20:01
created

RequestListener::onKernelRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ThemeBundle\Tests\Functional\TestBundle\Listener;
13
14
use Sylius\Bundle\ThemeBundle\Context\SettableThemeContext;
15
use Sylius\Bundle\ThemeBundle\Context\ThemeContextInterface;
16
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
17
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18
use Symfony\Component\HttpKernel\HttpKernelInterface;
19
20
/**
21
 * @author Kamil Kokot <[email protected]>
22
 */
23
final class RequestListener
24
{
25
    /**
26
     * @var ThemeRepositoryInterface
27
     */
28
    private $themeRepository;
29
30
    /**
31
     * @var ThemeContextInterface
32
     */
33
    private $themeContext;
34
35
    /**
36
     * @param ThemeRepositoryInterface $themeRepository
37
     * @param SettableThemeContext $themeContext
38
     */
39
    public function __construct(ThemeRepositoryInterface $themeRepository, SettableThemeContext $themeContext)
40
    {
41
        $this->themeRepository = $themeRepository;
42
        $this->themeContext = $themeContext;
43
    }
44
45
    /**
46
     * @param GetResponseEvent $event
47
     */
48
    public function onKernelRequest(GetResponseEvent $event)
49
    {
50
        if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
51
            // don't do anything if it's not the master request
52
            return;
53
        }
54
55
        $this->themeContext->setTheme($this->themeRepository->findOneByName('sylius/first-test-theme'));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Bundle\ThemeBundl...t\ThemeContextInterface as the method setTheme() does only exist in the following implementations of said interface: Sylius\Bundle\ThemeBundl...xt\SettableThemeContext.

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...
56
    }
57
}
58