Completed
Push — master ( 4f9913...493cb6 )
by WEBEWEB
02:18
created

QuoteManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addProvider() 0 6 2
A contains() 0 12 4
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
        return parent::addProvider($provider);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function contains(ProviderInterface $provider) {
48
        if (false === ($provider instanceof QuoteProviderInterface)) {
49
            throw new InvalidArgumentException("The provider must implements QuoteProviderInterface");
50
        }
51
        foreach ($this->getProviders() as $current) {
52
            if ($provider->getDomain() !== $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...
53
                continue;
54
            }
55
            return true;
56
        }
57
        return false;
58
    }
59
}
60