Completed
Push — master ( 3a9385...4f2256 )
by WEBEWEB
06:25
created

DataTablesManager::setIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 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\JQuery\DataTablesBundle\Manager;
13
14
use InvalidArgumentException;
15
use WBW\Bundle\CoreBundle\Manager\AbstractManager;
16
use WBW\Bundle\CoreBundle\Provider\ProviderInterface;
17
use WBW\Bundle\JQuery\DataTablesBundle\Exception\AlreadyRegisteredDataTablesProviderException;
18
use WBW\Bundle\JQuery\DataTablesBundle\Exception\UnregisteredDataTablesProviderException;
19
use WBW\Bundle\JQuery\DataTablesBundle\Provider\DataTablesProviderInterface;
20
21
/**
22
 * DataTables manager.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Bundle\JQuery\DataTablesBundle\Manager
26
 */
27
class DataTablesManager extends AbstractManager {
28
29
    /**
30
     * Service name.
31
     *
32
     * @var string
33
     */
34
    const SERVICE_NAME = "wbw.jquery.datatables.manager";
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function addProvider(ProviderInterface $provider) {
40
        if (true === $this->contains($provider)) {
41
            throw new AlreadyRegisteredDataTablesProviderException($provider->getName());
1 ignored issue
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 getName() does only exist in the following implementations of said interface: WBW\Bundle\BootstrapBund...pplicationThemeProvider, WBW\Bundle\BootstrapBundle\WBWBootstrapBundle, WBW\Bundle\CoreBundle\Color\AbstractColorProvider, 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\Te...Color\TestColorProvider, WBW\Bundle\CoreBundle\Th...pplicationThemeProvider, WBW\Bundle\CoreBundle\WBWCoreBundle, WBW\Bundle\JQuery\DataTa...tractDataTablesProvider, WBW\Bundle\JQuery\DataTa...WJQueryDataTablesBundle, WBW\Bundle\SyntaxHighlig...SyntaxHighlighterBundle.

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
        }
43
        return parent::addProvider($provider);
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function contains(ProviderInterface $provider) {
50
        if (false === ($provider instanceof DataTablesProviderInterface)) {
51
            throw new InvalidArgumentException("The provider must implements DataTablesProviderInterface");
52
        }
53
        foreach ($this->getProviders() as $current) {
54
            if ($provider->getName() === $current->getName()) {
1 ignored issue
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 getName() does only exist in the following implementations of said interface: WBW\Bundle\BootstrapBund...pplicationThemeProvider, WBW\Bundle\BootstrapBundle\WBWBootstrapBundle, WBW\Bundle\CoreBundle\Color\AbstractColorProvider, 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\Te...Color\TestColorProvider, WBW\Bundle\CoreBundle\Th...pplicationThemeProvider, WBW\Bundle\CoreBundle\WBWCoreBundle, WBW\Bundle\JQuery\DataTa...tractDataTablesProvider, WBW\Bundle\JQuery\DataTa...WJQueryDataTablesBundle, WBW\Bundle\SyntaxHighlig...SyntaxHighlighterBundle.

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...
55
                return true;
56
            }
57
        }
58
        return false;
59
    }
60
61
    /**
62
     * Get a provider.
63
     *
64
     * @param string $name The name.
65
     * @return DataTablesProviderInterface Returns the provider.
66
     * @throws UnregisteredDataTablesProviderException Throws an unregistered provider exception.
67
     */
68
    public function getProvider($name) {
69
        foreach ($this->getProviders() as $current) {
70
            if ($name === $current->getName()) {
1 ignored issue
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 getName() does only exist in the following implementations of said interface: WBW\Bundle\BootstrapBund...pplicationThemeProvider, WBW\Bundle\BootstrapBundle\WBWBootstrapBundle, WBW\Bundle\CoreBundle\Color\AbstractColorProvider, 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\Te...Color\TestColorProvider, WBW\Bundle\CoreBundle\Th...pplicationThemeProvider, WBW\Bundle\CoreBundle\WBWCoreBundle, WBW\Bundle\JQuery\DataTa...tractDataTablesProvider, WBW\Bundle\JQuery\DataTa...WJQueryDataTablesBundle, WBW\Bundle\SyntaxHighlig...SyntaxHighlighterBundle.

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...
71
                return $current;
72
            }
73
        }
74
        throw new UnregisteredDataTablesProviderException($name);
75
    }
76
}
77