Completed
Push — master ( 709dbb...f12a68 )
by Tobias
09:11
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 0
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
11
namespace Bazinga\GeocoderBundle\DependencyInjection;
12
13
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
17
/**
18
 * @author William Durand <[email protected]>
19
 */
20
class Configuration implements ConfigurationInterface
21
{
22
    /**
23
     * Whether to use the debug mode.
24
     *
25
     * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41
26
     *
27
     * @var bool
28
     */
29
    private $debug;
30
31
    /**
32
     * @param bool $debug
33
     */
34
    public function __construct($debug)
35
    {
36
        $this->debug = (bool) $debug;
37
    }
38
39
    /**
40
     * Generates the configuration tree builder.
41
     *
42
     * @return TreeBuilder The tree builder
43
     */
44
    public function getConfigTreeBuilder()
45
    {
46
        $treeBuilder = new TreeBuilder();
47
        $rootNode = $treeBuilder->root('bazinga_geocoder');
48
49
        $rootNode
50
            ->children()
51
            ->append($this->getProvidersNode())
52
            ->arrayNode('profiling')
53
                ->addDefaultsIfNotSet()
54
                ->treatFalseLike(['enabled' => false])
55
                ->treatTrueLike(['enabled' => true])
56
                ->treatNullLike(['enabled' => $this->debug])
57
                ->info('Extend the debug profiler with information about requests.')
58
                ->children()
59
                    ->booleanNode('enabled')
60
                        ->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
61
                        ->defaultValue($this->debug)
62
                    ->end()
63
                ->end()
64
            ->end()
65
            ->arrayNode('fake_ip')
66
                ->beforeNormalization()
67
                ->ifString()
68
                    ->then(function ($value) {
69
                        return ['ip' => $value];
70
                    })
71
                ->end()
72
                ->canBeEnabled()
73
                ->children()
74
                    ->scalarNode('ip')->defaultNull()->end()
75
                ->end()
76
            ->end();
77
78
        return $treeBuilder;
79
    }
80
81
    /**
82
     * @return ArrayNodeDefinition
83
     */
84
    private function getProvidersNode()
85
    {
86
        $treeBuilder = new TreeBuilder();
87
        $node = $treeBuilder->root('providers');
88
89
        $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...
90
            ->requiresAtLeastOneElement()
91
            ->useAttributeAsKey('name')
92
            ->prototype('array')
93
                ->children()
94
                    ->scalarNode('factory')->isRequired()->cannotBeEmpty()->end()
95
                    ->variableNode('options')->defaultValue([])->end()
96
                    ->scalarNode('cache')->defaultNull()->end()
97
                    ->scalarNode('cache_lifetime')->defaultNull()->end()
98
                    ->arrayNode('aliases')
99
                        ->prototype('scalar')->end()
100
                    ->end()
101
                ->end()
102
            ->end();
103
104
        return $node;
105
    }
106
}
107