Configuration::getConfigTreeBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @project Magento Bridge for Symfony 2.
5
 *
6
 * @author  Sébastien MALOT <[email protected]>
7
 * @license MIT
8
 * @url     <https://github.com/smalot/magento-bundle>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Smalot\MagentoBundle\DependencyInjection;
15
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * Class Configuration
21
 *
22
 * @package Smalot\MagentoBundle\DependencyInjection
23
 */
24
class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     */
29
    public function getConfigTreeBuilder()
30
    {
31
        $treeBuilder = new TreeBuilder('magento');
32
33
        $treeBuilder->getRootNode()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. 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...
34
          ->children()
35
            ->arrayNode('connections')->info('List all available connections')
36
                ->requiresAtLeastOneElement()
37
                ->useAttributeAsKey('default')
38
                ->prototype('array')
39
                  ->children()
40
                      ->scalarNode('url')->isRequired()->cannotBeEmpty()->example('http://domain.tld/magento/')->end()
41
                      ->scalarNode('api_user')->isRequired()->cannotBeEmpty()->example('username')->end()
42
                      ->scalarNode('api_key')->isRequired()->cannotBeEmpty()->example('0123456789AZ')->end()
43
                      ->booleanNode('logging')->defaultValue(false)->info('Enable logging system')->example('%kernel.debug%')->end()
44
                      ->scalarNode('logger')->defaultValue(null)->info('Refers to the logger service')->end()
45
                      ->scalarNode('dispatcher')->defaultValue(null)->info('Refers to the dispatcher service')->end()
46
                  ->end()
47
                ->end()
48
            ->end()
49
            ->scalarNode('default_connection')->defaultValue('default')->info('Refers to the default connection in the connection pool')->example('default')
50
            ->end()
51
          ->end()
52
        ;
53
54
        return $treeBuilder;
55
    }
56
}
57