Completed
Push — master ( f7755f...e9eb75 )
by Tobias
06:49
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 28
    public function __construct($debug)
37
    {
38 28
        $this->debug = (bool) $debug;
39 28
    }
40
41
    /**
42
     * Generates the configuration tree builder.
43
     *
44
     * @return TreeBuilder The tree builder
45
     */
46 28
    public function getConfigTreeBuilder()
47
    {
48 28
        $treeBuilder = new TreeBuilder();
49 28
        $rootNode = $treeBuilder->root('bazinga_geocoder');
50
51
        $rootNode
52 28
            ->children()
53 28
            ->append($this->getProvidersNode())
54 28
            ->arrayNode('profiling')
55 28
                ->addDefaultsIfNotSet()
56 28
                ->treatFalseLike(['enabled' => false])
57 28
                ->treatTrueLike(['enabled' => true])
58 28
                ->treatNullLike(['enabled' => $this->debug])
59 28
                ->info('Extend the debug profiler with information about requests.')
60 28
                ->children()
61 28
                    ->booleanNode('enabled')
62 28
                        ->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
63 28
                        ->defaultValue($this->debug)
64 28
                    ->end()
65 28
                ->end()
66 28
            ->end()
67 28
            ->arrayNode('fake_ip')
68 28
                ->beforeNormalization()
69 28
                ->ifString()
70 28
                    ->then(function ($value) {
71
                        return ['ip' => $value];
72 28
                    })
73 28
                ->end()
74 28
                ->canBeEnabled()
75 28
                ->children()
76 28
                    ->scalarNode('ip')->defaultNull()->end()
77 28
                ->end()
78 28
            ->end();
79
80 28
        return $treeBuilder;
81
    }
82
83
    /**
84
     * @return ArrayNodeDefinition
85
     */
86 28
    private function getProvidersNode()
87
    {
88 28
        $treeBuilder = new TreeBuilder();
89 28
        $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 fixXmlConfig() 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 28
            ->requiresAtLeastOneElement()
93 28
            ->useAttributeAsKey('name')
94 28
            ->prototype('array')
95 28
            ->fixXmlConfig('plugin')
96 28
                ->children()
97 28
                    ->scalarNode('factory')->isRequired()->cannotBeEmpty()->end()
98 28
                    ->variableNode('options')->defaultValue([])->end()
99 28
                    ->scalarNode('cache')->defaultNull()->end()
100 28
                    ->scalarNode('cache_lifetime')->defaultNull()->end()
101 28
                    ->scalarNode('cache_precision')
102 28
                        ->defaultNull()
103 28
                        ->info('Precision of the coordinates to cache.')
104 28
                        ->end()
105 28
                    ->scalarNode('limit')->defaultNull()->end()
106 28
                    ->scalarNode('locale')->defaultNull()->end()
107 28
                    ->scalarNode('logger')->defaultNull()->end()
108 28
                    ->arrayNode('aliases')
109 28
                        ->prototype('scalar')->end()
110 28
                    ->end()
111 28
                    ->append($this->createClientPluginNode())
112 28
                ->end()
113 28
            ->end();
114
115 28
        return $node;
116
    }
117
118
    /**
119
     * Create plugin node of a client.
120
     *
121
     * @return ArrayNodeDefinition The plugin node
122
     */
123 28
    private function createClientPluginNode()
124
    {
125 28
        $builder = new TreeBuilder();
126 28
        $node = $builder->root('plugins');
127
128
        /** @var ArrayNodeDefinition $pluginList */
129
        $pluginList = $node
130 28
            ->info('A list of plugin service ids. The order is important.')
131 28
            ->prototype('array')
132
        ;
133
        $pluginList
134
            // support having just a service id in the list
135 28
            ->beforeNormalization()
136 28
                ->always(function ($plugin) {
137 2
                    if (is_string($plugin)) {
138
                        return [
139 2
                            'reference' => [
140
                                'enabled' => true,
141 2
                                'id' => $plugin,
142
                            ],
143
                        ];
144
                    }
145
146 1
                    return $plugin;
147 28
                })
148 28
            ->end()
149
        ;
150
151
        $pluginList
152 28
            ->children()
153 28
                ->arrayNode('reference')
154 28
                    ->canBeEnabled()
155 28
                    ->info('Reference to a plugin service')
156 28
                    ->children()
157 28
                        ->scalarNode('id')
158 28
                            ->info('Service id of a plugin')
159 28
                            ->isRequired()
160 28
                            ->cannotBeEmpty()
161 28
                        ->end()
162 28
                    ->end()
163 28
                ->end()
164 28
            ->end()
165 28
        ->end();
166
167 28
        return $node;
168
    }
169
}
170