Completed
Push — master ( 493cb6...75e203 )
by WEBEWEB
02:04
created

QuoteManager::getQuoteProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
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 WBW\Bundle\CoreBundle\Manager;
13
14
use InvalidArgumentException;
15
use WBW\Bundle\CoreBundle\Exception\AlreadyRegisteredProviderException;
16
use WBW\Bundle\CoreBundle\Provider\ProviderInterface;
17
use WBW\Bundle\CoreBundle\Provider\QuoteProviderInterface;
18
19
/**
20
 * Quote manager.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\CoreBundle\Tests\Manager
24
 */
25
class QuoteManager extends AbstractManager {
26
27
    /**
28
     * Service name.
29
     *
30
     * @var string
31
     */
32
    const SERVICE_NAME = "webeweb.core.manager.quote";
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function addProvider(ProviderInterface $provider) {
38
        if (true === $this->contains($provider)) {
39
            throw new AlreadyRegisteredProviderException($provider);
40
        }
41
        $provider->init();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface WBW\Bundle\CoreBundle\Provider\ProviderInterface as the method init() does only exist in the following implementations of said interface: WBW\Bundle\CoreBundle\Quote\AbstractQuoteProvider, WBW\Bundle\CoreBundle\Quote\QuoteProvider.

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...
42
        return parent::addProvider($provider);
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function contains(ProviderInterface $provider) {
49
        if (false === ($provider instanceof QuoteProviderInterface)) {
50
            throw new InvalidArgumentException("The provider must implements QuoteProviderInterface");
51
        }
52
        foreach ($this->getProviders() as $current) {
53
            if ($provider->getDomain() !== $current->getDomain()) {
54
                continue;
55
            }
56
            return true;
57
        }
58
        return false;
59
    }
60
61
    /**
62
     * Get a quote provider.
63
     *
64
     * @param string $domain The domain.
65
     * @return ProviderInterface|null Returns the quote provider.
66
     */
67
    public function getQuoteProvider($domain) {
68
        foreach ($this->getProviders() as $current) {
69
            if ($domain === $current->getDomain()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface WBW\Bundle\CoreBundle\Provider\ProviderInterface as the method getDomain() does only exist in the following implementations of said interface: WBW\Bundle\CoreBundle\Color\AbstractColorProvider, WBW\Bundle\CoreBundle\Color\AmberColorProvider, WBW\Bundle\CoreBundle\Color\BlueColorProvider, WBW\Bundle\CoreBundle\Color\BlueGreyColorProvider, WBW\Bundle\CoreBundle\Color\BrownColorProvider, WBW\Bundle\CoreBundle\Color\CyanColorProvider, WBW\Bundle\CoreBundle\Co...DeepOrangeColorProvider, WBW\Bundle\CoreBundle\Co...DeepPurpleColorProvider, WBW\Bundle\CoreBundle\Color\GreenColorProvider, WBW\Bundle\CoreBundle\Color\GreyColorProvider, WBW\Bundle\CoreBundle\Color\IndigoColorProvider, WBW\Bundle\CoreBundle\Color\LightBlueColorProvider, WBW\Bundle\CoreBundle\Co...LightGreenColorProvider, WBW\Bundle\CoreBundle\Color\LimeColorProvider, WBW\Bundle\CoreBundle\Co...ette\AmberColorProvider, WBW\Bundle\CoreBundle\Co...ette\BlackColorProvider, WBW\Bundle\CoreBundle\Co...lette\BlueColorProvider, WBW\Bundle\CoreBundle\Co...e\BlueGreyColorProvider, WBW\Bundle\CoreBundle\Co...ette\BrownColorProvider, WBW\Bundle\CoreBundle\Co...lette\CyanColorProvider, WBW\Bundle\CoreBundle\Co...DeepOrangeColorProvider, WBW\Bundle\CoreBundle\Co...DeepPurpleColorProvider, WBW\Bundle\CoreBundle\Co...ette\GreenColorProvider, WBW\Bundle\CoreBundle\Co...lette\GreyColorProvider, WBW\Bundle\CoreBundle\Co...tte\IndigoColorProvider, WBW\Bundle\CoreBundle\Co...\LightBlueColorProvider, WBW\Bundle\CoreBundle\Co...LightGreenColorProvider, WBW\Bundle\CoreBundle\Co...lette\LimeColorProvider, WBW\Bundle\CoreBundle\Co...tte\OrangeColorProvider, WBW\Bundle\CoreBundle\Co...lette\PinkColorProvider, WBW\Bundle\CoreBundle\Co...tte\PurpleColorProvider, WBW\Bundle\CoreBundle\Co...alette\RedColorProvider, WBW\Bundle\CoreBundle\Co...lette\TealColorProvider, WBW\Bundle\CoreBundle\Co...ette\WhiteColorProvider, WBW\Bundle\CoreBundle\Co...tte\YellowColorProvider, WBW\Bundle\CoreBundle\Color\OrangeColorProvider, WBW\Bundle\CoreBundle\Color\PinkColorProvider, WBW\Bundle\CoreBundle\Color\PurpleColorProvider, WBW\Bundle\CoreBundle\Color\RedColorProvider, WBW\Bundle\CoreBundle\Color\TealColorProvider, WBW\Bundle\CoreBundle\Color\YellowColorProvider, WBW\Bundle\CoreBundle\Quote\AbstractQuoteProvider, WBW\Bundle\CoreBundle\Quote\QuoteProvider.

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...
70
                return $current;
71
            }
72
        }
73
        return null;
74
    }
75
}
76