Configuration   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 260
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 257 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\CacheBundle\DependencyInjection;
15
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
class Configuration implements ConfigurationInterface
20
{
21
    public function getConfigTreeBuilder(): TreeBuilder
22
    {
23
        $treeBuilder = new TreeBuilder('sonata_cache');
24
25
        // Keep compatibility with symfony/config < 4.2
26
        if (!method_exists($treeBuilder, 'getRootNode')) {
27
            $node = $treeBuilder->root('sonata_cache')->children();
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        } else {
29
            $node = $treeBuilder->getRootNode()->children();
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...
30
        }
31
32
        $node
33
            ->arrayNode('cache_invalidation')
34
                ->addDefaultsIfNotSet()
35
                ->children()
36
                    ->enumNode('orm_listener')
37
                        ->values([true, false, 'auto'])
38
                        ->defaultValue('auto')
39
                    ->end()
40
                    ->enumNode('phpcr_odm_listener')
41
                        ->values([true, false, 'auto'])
42
                        ->defaultValue('auto')
43
                    ->end()
44
                    ->scalarNode('service')->defaultValue('sonata.cache.invalidation.simple')->end()
45
                    ->scalarNode('recorder')->defaultValue('sonata.cache.recorder')->end()
46
                    ->arrayNode('classes')
47
                        ->useAttributeAsKey('id')
48
                        ->prototype('scalar')->end()
49
                    ->end()
50
                ->end()
51
            ->end()
52
53
            ->scalarNode('default_cache')->defaultValue(false)->end()
54
            ->scalarNode('default_counter')->defaultValue(false)->end()
55
56
            ->arrayNode('caches')
57
                ->children()
58
                    ->arrayNode('esi')
59
                        ->children()
60
                            ->scalarNode('token')->defaultValue(hash('sha256', uniqid((string) mt_rand(), true)))->end()
61
                            ->scalarNode('version')->defaultValue(2)->end()
62
                            ->arrayNode('servers')
63
                                ->prototype('scalar')->end()
64
                            ->end()
65
                        ->end()
66
                    ->end()
67
68
                    ->arrayNode('ssi')
69
                        ->children()
70
                            ->scalarNode('token')->defaultValue(hash('sha256', uniqid((string) mt_rand(), true)))->end()
71
                        ->end()
72
                    ->end()
73
74
                    ->arrayNode('mongo')
75
                        ->children()
76
                            ->scalarNode('database')->isRequired()->end()
77
                            ->scalarNode('collection')->isRequired()->end()
78
                            ->arrayNode('servers')
79
                                ->prototype('array')
80
                                    ->children()
81
                                        ->integerNode('port')->defaultValue(27017)->end()
82
                                        ->scalarNode('host')->isRequired()->end()
83
                                        ->scalarNode('user')->defaultValue(null)->end()
84
                                        ->scalarNode('password')->defaultValue(null)->end()
85
                                    ->end()
86
                                ->end()
87
                            ->end()
88
                        ->end()
89
                    ->end()
90
91
                    ->arrayNode('memcached')
92
                        ->children()
93
                            ->scalarNode('prefix')->isRequired()->end()
94
                            ->arrayNode('servers')
95
                                ->prototype('array')
96
                                    ->children()
97
                                        ->integerNode('port')->defaultValue(11211)->end()
98
                                        ->scalarNode('host')->defaultValue('localhost')->end()
99
                                        ->integerNode('weight')->defaultValue(0)->end()
100
                                    ->end()
101
                                ->end()
102
                            ->end()
103
                        ->end()
104
                    ->end()
105
106
                    ->arrayNode('predis')
107
                        ->children()
108
                            ->arrayNode('servers')
109
                                ->prototype('array')
110
                                    ->children()
111
                                        ->integerNode('port')->defaultValue(6379)->end()
112
                                        ->scalarNode('host')->defaultValue('localhost')->end()
113
                                        ->integerNode('database')->isRequired()->end()
114
                                        ->scalarNode('password')->defaultValue(null)->end()
115
                                    ->end()
116
                                ->end()
117
                            ->end()
118
                        ->end()
119
                    ->end()
120
121
                    ->arrayNode('apc')
122
                        ->children()
123
                            ->scalarNode('token')->isRequired()->end()
124
                            ->scalarNode('prefix')->isRequired()->end()
125
                            ->arrayNode('servers')
126
                                ->prototype('array')
127
                                    ->children()
128
                                        ->scalarNode('domain')->isRequired()->end()
129
                                        ->scalarNode('ip')->isRequired()->end()
130
                                        ->integerNode('port')->defaultValue(80)->end()
131
                                        ->scalarNode('basic')->defaultValue(false)->end()
132
                                    ->end()
133
                                ->end()
134
                            ->end()
135
                            ->arrayNode('timeout')
136
                                ->addDefaultsIfNotSet()
137
                                ->children()
138
                                    ->arrayNode('RCV')
139
                                        ->prototype('scalar')->end()
140
                                    ->end()
141
                                    ->arrayNode('SND')
142
                                        ->prototype('scalar')->end()
143
                                    ->end()
144
                                ->end()
145
                            ->end()
146
                        ->end()
147
                    ->end()
148
149
                    ->arrayNode('symfony')
150
                        ->children()
151
                            ->scalarNode('token')->isRequired()->end()
152
                            ->variableNode('types')
153
                                ->defaultValue(['all', 'annotations', 'doctrine', 'sonata', 'translations', 'twig'])
154
                                ->validate()
155
                                    ->ifString()
156
                                    ->thenInvalid('Symfony cache types must be an array [value1, value2, ...]')
157
                                ->end()
158
                                ->validate()
159
                                    ->ifNull()
160
                                    ->thenInvalid('Symfony cache types cannot be null')
161
                                ->end()
162
                            ->end()
163
                            ->booleanNode('php_cache_enabled')->defaultValue(false)->end()
164
                            ->arrayNode('servers')
165
                                ->prototype('array')
166
                                    ->children()
167
                                        ->scalarNode('domain')->isRequired()->end()
168
                                        ->scalarNode('ip')->isRequired()->end()
169
                                        ->integerNode('port')->defaultValue(80)->end()
170
                                        ->scalarNode('basic')->defaultValue(false)->end()
171
                                    ->end()
172
                                ->end()
173
                            ->end()
174
                            ->arrayNode('timeout')
175
                                ->addDefaultsIfNotSet()
176
                                ->children()
177
                                    ->arrayNode('RCV')
178
                                        ->addDefaultsIfNotSet()
179
                                        ->children()
180
                                            ->integerNode('sec')
181
                                                ->info('Timeout value specifying the amount of seconds for input operations')
182
                                                ->defaultValue(2)
183
                                            ->end()
184
                                            ->integerNode('usec')
185
                                                ->info('Timeout value specifying the amount of microseconds for input operations')
186
                                                ->defaultValue(0)
187
                                            ->end()
188
                                        ->end()
189
                                    ->end()
190
                                    ->arrayNode('SND')
191
                                        ->addDefaultsIfNotSet()
192
                                        ->children()
193
                                            ->integerNode('sec')
194
                                                ->info(
195
                                                    <<<'INFO'
196
Timeout value specifying the amount of seconds that an output function
197
blocks because flow control prevents data from being sent
198
INFO
199
                                                )
200
                                                ->defaultValue(2)
201
                                            ->end()
202
                                            ->integerNode('usec')
203
                                                ->info(
204
                                                    <<<'INFO'
205
Timeout value specifying the amount of microseconds that an output function
206
blocks because flow control prevents data from being sent
207
INFO
208
                                                )
209
                                                ->defaultValue(0)
210
                                            ->end()
211
                                        ->end()
212
                                    ->end()
213
                                ->end()
214
                            ->end()
215
                        ->end()
216
                    ->end()
217
                ->end()
218
            ->end()
219
            ->arrayNode('counters')
220
                ->children()
221
                    ->arrayNode('mongo')
222
                        ->children()
223
                            ->scalarNode('database')->isRequired()->end()
224
                            ->scalarNode('collection')->isRequired()->end()
225
                            ->arrayNode('servers')
226
                                ->prototype('array')
227
                                    ->children()
228
                                        ->integerNode('port')->defaultValue(27017)->end()
229
                                        ->scalarNode('host')->isRequired()->end()
230
                                        ->scalarNode('user')->defaultValue(null)->end()
231
                                        ->scalarNode('password')->defaultValue(null)->end()
232
                                    ->end()
233
                                ->end()
234
                            ->end()
235
                        ->end()
236
                    ->end()
237
238
                    ->arrayNode('memcached')
239
                        ->children()
240
                            ->scalarNode('prefix')->isRequired()->end()
241
                            ->arrayNode('servers')
242
                                ->prototype('array')
243
                                    ->children()
244
                                        ->integerNode('port')->defaultValue(11211)->end()
245
                                        ->scalarNode('host')->defaultValue('localhost')->end()
246
                                        ->integerNode('weight')->defaultValue(0)->end()
247
                                    ->end()
248
                                ->end()
249
                            ->end()
250
                        ->end()
251
                    ->end()
252
253
                    ->arrayNode('predis')
254
                        ->children()
255
                            ->arrayNode('servers')
256
                                ->prototype('array')
257
                                    ->children()
258
                                        ->integerNode('port')->defaultValue(6379)->end()
259
                                        ->scalarNode('host')->defaultValue('localhost')->end()
260
                                        ->integerNode('database')->isRequired()->end()
261
                                    ->end()
262
                                ->end()
263
                            ->end()
264
                        ->end()
265
                    ->end()
266
267
                    ->arrayNode('apc')
268
                        ->children()
269
                            ->scalarNode('prefix')->isRequired()->end()
270
                        ->end()
271
                    ->end()
272
                ->end()
273
            ->end()
274
        ;
275
276
        return $treeBuilder;
277
    }
278
}
279