Completed
Push — master ( 8c9a02...d7d88b )
by Tobias
11:28
created

Configuration   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.9%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 143
ccs 77
cts 82
cp 0.939
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getConfigTreeBuilder() 0 36 1
B getProvidersNode() 0 26 1
B createClientPluginNode() 0 46 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Bazinga\GeocoderBundle\DependencyInjection;
14
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
/**
20
 * @author William Durand <[email protected]>
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * Whether to use the debug mode.
26
     *
27
     * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41
28
     *
29
     * @var bool
30
     */
31
    private $debug;
32
33
    /**
34
     * @param bool $debug
35
     */
36 25
    public function __construct($debug)
37
    {
38 25
        $this->debug = (bool) $debug;
39 25
    }
40
41
    /**
42
     * Generates the configuration tree builder.
43
     *
44
     * @return TreeBuilder The tree builder
45
     */
46 25
    public function getConfigTreeBuilder()
47
    {
48 25
        $treeBuilder = new TreeBuilder();
49 25
        $rootNode = $treeBuilder->root('bazinga_geocoder');
50
51
        $rootNode
52 25
            ->children()
53 25
            ->append($this->getProvidersNode())
54 25
            ->arrayNode('profiling')
55 25
                ->addDefaultsIfNotSet()
56 25
                ->treatFalseLike(['enabled' => false])
57 25
                ->treatTrueLike(['enabled' => true])
58 25
                ->treatNullLike(['enabled' => $this->debug])
59 25
                ->info('Extend the debug profiler with information about requests.')
60 25
                ->children()
61 25
                    ->booleanNode('enabled')
62 25
                        ->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
63 25
                        ->defaultValue($this->debug)
64 25
                    ->end()
65 25
                ->end()
66 25
            ->end()
67 25
            ->arrayNode('fake_ip')
68 25
                ->beforeNormalization()
69 25
                ->ifString()
70
                    ->then(function ($value) {
71
                        return ['ip' => $value];
72 25
                    })
73 25
                ->end()
74 25
                ->canBeEnabled()
75 25
                ->children()
76 25
                    ->scalarNode('ip')->defaultNull()->end()
77 25
                ->end()
78 25
            ->end();
79
80 25
        return $treeBuilder;
81
    }
82
83
    /**
84
     * @return ArrayNodeDefinition
85
     */
86 25
    private function getProvidersNode()
87
    {
88 25
        $treeBuilder = new TreeBuilder();
89 25
        $node = $treeBuilder->root('providers');
90
91
        $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...
92 25
            ->requiresAtLeastOneElement()
93 25
            ->useAttributeAsKey('name')
94 25
            ->prototype('array')
95 25
                ->children()
96 25
                    ->scalarNode('factory')->isRequired()->cannotBeEmpty()->end()
97 25
                    ->variableNode('options')->defaultValue([])->end()
98 25
                    ->scalarNode('cache')->defaultNull()->end()
99 25
                    ->scalarNode('cache_lifetime')->defaultNull()->end()
100 25
                    ->scalarNode('limit')->defaultNull()->end()
101 25
                    ->scalarNode('locale')->defaultNull()->end()
102 25
                    ->scalarNode('logger')->defaultNull()->end()
103 25
                    ->arrayNode('aliases')
104 25
                        ->prototype('scalar')->end()
105 25
                    ->end()
106 25
                    ->append($this->createClientPluginNode())
107 25
                ->end()
108 25
            ->end();
109
110 25
        return $node;
111
    }
112
113
    /**
114
     * Create plugin node of a client.
115
     *
116
     * @return ArrayNodeDefinition The plugin node
117
     */
118 25
    private function createClientPluginNode()
119
    {
120 25
        $builder = new TreeBuilder();
121 25
        $node = $builder->root('plugins');
122
123
        /** @var ArrayNodeDefinition $pluginList */
124
        $pluginList = $node
125 25
            ->info('A list of plugin service ids. The order is important.')
126 25
            ->prototype('array')
127
        ;
128
        $pluginList
129
            // support having just a service id in the list
130 25
            ->beforeNormalization()
131 25
                ->always(function ($plugin) {
132
                    if (is_string($plugin)) {
133
                        return [
134
                            'reference' => [
135
                                'enabled' => true,
136
                                'id' => $plugin,
137
                            ],
138
                        ];
139
                    }
140
141
                    return $plugin;
142 25
                })
143 25
            ->end()
144
        ;
145
146
        $pluginList
147 25
            ->children()
148 25
                ->arrayNode('reference')
149 25
                    ->canBeEnabled()
150 25
                    ->info('Reference to a plugin service')
151 25
                    ->children()
152 25
                        ->scalarNode('id')
153 25
                            ->info('Service id of a plugin')
154 25
                            ->isRequired()
155 25
                            ->cannotBeEmpty()
156 25
                        ->end()
157 25
                    ->end()
158 25
                ->end()
159 25
            ->end()
160 25
        ->end();
161
162 25
        return $node;
163
    }
164
}
165