Completed
Push — master ( 556252...f1435e )
by Tobias
04:32
created

Configuration::getRootNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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