Completed
Push — master ( a814e0...392de1 )
by Tobias
24:09
created

Configuration   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 94
rs 10
c 4
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getConfigTreeBuilder() 0 42 1
A getProvidersNode() 0 22 1
1
<?php
2
3
/**
4
 * This file is part of the BazingaGeocoderBundle package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
namespace Bazinga\Bundle\GeocoderBundle\DependencyInjection;
11
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
/**
17
 * @author William Durand <[email protected]>
18
 */
19
class Configuration implements ConfigurationInterface
20
{
21
    /**
22
     * Whether to use the debug mode.
23
     *
24
     * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41
25
     *
26
     * @var bool
27
     */
28
    private $debug;
29
30
    /**
31
     * @param bool $debug
32
     */
33
    public function __construct($debug)
34
    {
35
        $this->debug = (bool) $debug;
36
    }
37
38
    /**
39
     * Generates the configuration tree builder.
40
     *
41
     * @return TreeBuilder The tree builder
42
     */
43
    public function getConfigTreeBuilder()
44
    {
45
        $treeBuilder = new TreeBuilder();
46
        $rootNode = $treeBuilder->root('bazinga_geocoder');
47
48
        $rootNode
49
            ->children()
50
            ->append($this->getProvidersNode())
51
            ->arrayNode('profiling')
52
                ->addDefaultsIfNotSet()
53
                ->treatFalseLike(['enabled' => false])
54
                ->treatTrueLike(['enabled' => true])
55
                ->treatNullLike(['enabled' => $this->debug])
56
                ->info('Extend the debug profiler with information about requests.')
57
                ->children()
58
                    ->booleanNode('enabled')
59
                        ->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
60
                        ->defaultValue($this->debug)
61
                    ->end()
62
                ->end()
63
            ->end()
64
            ->scalarNode('default_provider')->defaultNull()->end()
65
            ->arrayNode('fake_ip')
66
                ->beforeNormalization()
67
                ->ifString()
68
                    ->then(function ($value) { return array('ip' => $value); })
69
                ->end()
70
                ->treatFalseLike(array('enabled' => false))
71
                ->treatTrueLike(array('enabled' => true))
72
                ->treatNullLike(array('enabled' => true))
73
                ->children()
74
                    ->booleanNode('enabled')
75
                        ->defaultTrue()
76
                    ->end()
77
                    ->scalarNode('ip')->defaultNull()->end()
78
                    ->scalarNode('priority')->defaultValue(0)->end()
79
                ->end()
80
            ->end()
81
        ;
82
83
        return $treeBuilder;
84
    }
85
86
    /**
87
     * @return ArrayNodeDefinition
88
     */
89
    private function getProvidersNode()
90
    {
91
        $treeBuilder = new TreeBuilder();
92
        $node = $treeBuilder->root('providers');
93
94
        $node
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...
95
            ->requiresAtLeastOneElement()
96
            ->useAttributeAsKey('name')
97
            ->prototype('array')
98
                ->children()
99
                    ->scalarNode('factory')->isRequired()->cannotBeEmpty()->end()
100
                    ->variableNode('options')->defaultValue([])->end()
101
                    ->scalarNode('cache')->defaultNull()->end()
102
                    ->scalarNode('cache_lifetime')->defaultNull()->end()
103
                    ->arrayNode('aliases')
104
                        ->prototype('scalar')->end()
105
                    ->end()
106
                ->end()
107
            ->end();
108
109
        return $node;
110
    }
111
112
}
113