Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#494)
by Jérémiah
18:56
created

Configuration::securityQuerySection()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0007

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 34
ccs 22
cts 23
cp 0.9565
rs 9.552
c 0
b 0
f 0
cc 3
nc 1
nop 2
crap 3.0007
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\DependencyInjection;
6
7
use GraphQL\Validator\Rules\QueryComplexity;
8
use GraphQL\Validator\Rules\QueryDepth;
9
use Overblog\GraphQLBundle\Definition\Argument;
10
use Overblog\GraphQLBundle\Error\ErrorHandler;
11
use Overblog\GraphQLBundle\EventListener\ErrorLoggerListener;
12
use Overblog\GraphQLBundle\Resolver\Resolver;
13
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
14
use Symfony\Component\Config\Definition\Builder\EnumNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17
use Symfony\Component\Config\Definition\ConfigurationInterface;
18
19
class Configuration implements ConfigurationInterface
20
{
21
    public const NAME = 'overblog_graphql';
22
23
    /** bool */
24
    private $debug;
25
26
    /** null|string */
27
    private $cacheDir;
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param bool        $debug    Whether to use the debug mode
33
     * @param string|null $cacheDir
34
     */
35 28
    public function __construct(bool $debug, string $cacheDir = null)
36
    {
37 28
        $this->debug = (bool) $debug;
38 28
        $this->cacheDir = $cacheDir;
39 28
    }
40
41 28
    public function getConfigTreeBuilder()
42
    {
43 28
        $treeBuilder = new TreeBuilder(self::NAME);
44
45 28
        $rootNode = self::getRootNodeWithoutDeprecation($treeBuilder, self::NAME);
46
47
        $rootNode
48 28
            ->children()
49 28
                ->append($this->batchingMethodSection())
50 28
                ->append($this->definitionsSection())
51 28
                ->append($this->errorsHandlerSection())
52 28
                ->append($this->servicesSection())
53 28
                ->append($this->securitySection())
54 28
                ->append($this->doctrineSection())
55 28
            ->end();
56
57 28
        return $treeBuilder;
58
    }
59
60 28
    private function batchingMethodSection()
61
    {
62 28
        $builder = new TreeBuilder('batching_method', 'enum');
63
        /** @var EnumNodeDefinition $node */
64 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'batching_method', 'enum');
65
66
        $node
67 28
            ->values(['relay', 'apollo'])
68 28
            ->defaultValue('relay')
69 28
        ->end();
70
71 28
        return $node;
72
    }
73
74 28
    private function errorsHandlerSection()
75
    {
76 28
        $builder = new TreeBuilder('errors_handler');
77
        /** @var ArrayNodeDefinition $node */
78 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'errors_handler');
79
        $node
80 28
            ->treatFalseLike(['enabled' => false])
81 28
            ->treatTrueLike(['enabled' => true])
82 28
            ->treatNullLike(['enabled' => true])
83 28
            ->addDefaultsIfNotSet()
84 28
            ->children()
85 28
                ->booleanNode('enabled')->defaultTrue()->end()
86 28
                ->scalarNode('internal_error_message')->defaultValue(ErrorHandler::DEFAULT_ERROR_MESSAGE)->end()
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
                ->/** @scrutinizer ignore-call */ scalarNode('internal_error_message')->defaultValue(ErrorHandler::DEFAULT_ERROR_MESSAGE)->end()
Loading history...
87 28
                ->booleanNode('rethrow_internal_exceptions')->defaultFalse()->end()
88 28
                ->booleanNode('debug')->defaultValue($this->debug)->end()
89 28
                ->booleanNode('log')->defaultTrue()->end()
90 28
                ->scalarNode('logger_service')->defaultValue(ErrorLoggerListener::DEFAULT_LOGGER_SERVICE)->end()
91 28
                ->booleanNode('map_exceptions_to_parent')->defaultFalse()->end()
92 28
                ->arrayNode('exceptions')
93 28
                    ->addDefaultsIfNotSet()
94 28
                    ->children()
95 28
                        ->arrayNode('warnings')
96 28
                            ->treatNullLike([])
97 28
                            ->prototype('scalar')->end()
98 28
                        ->end()
99 28
                        ->arrayNode('errors')
100 28
                            ->treatNullLike([])
101 28
                            ->prototype('scalar')->end()
102 28
                    ->end()
103 28
                ->end()
104 28
            ->end();
105
106 28
        return $node;
107
    }
108
109 28
    private function definitionsSection()
110
    {
111 28
        $builder = new TreeBuilder('definitions');
112
        /** @var ArrayNodeDefinition $node */
113 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'definitions');
114
        $node
115 28
            ->addDefaultsIfNotSet()
116 28
            ->children()
117 28
                ->scalarNode('argument_class')->defaultValue(Argument::class)->end()
118 28
                ->variableNode('default_resolver')->defaultValue([Resolver::class, 'defaultResolveFn'])->end()
0 ignored issues
show
Bug introduced by
The method variableNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
                ->/** @scrutinizer ignore-call */ variableNode('default_resolver')->defaultValue([Resolver::class, 'defaultResolveFn'])->end()
Loading history...
119 28
                ->scalarNode('class_namespace')->defaultValue('Overblog\\GraphQLBundle\\__DEFINITIONS__')->end()
120 28
                ->scalarNode('cache_dir')->defaultNull()->end()
121 28
                ->scalarNode('cache_dir_permissions')->defaultNull()->end()
122 28
                ->booleanNode('use_classloader_listener')->defaultTrue()->end()
123 28
                ->booleanNode('auto_compile')->defaultTrue()->end()
124 28
                ->booleanNode('show_debug_info')->info('Show some performance stats in extensions')->defaultFalse()->end()
125 28
                ->booleanNode('config_validation')->defaultValue($this->debug)->end()
126 28
                ->append($this->definitionsSchemaSection())
127 28
                ->append($this->definitionsMappingsSection())
128 28
                ->arrayNode('builders')
129 28
                    ->children()
130 28
                        ->append($this->builderSection('field'))
131 28
                        ->append($this->builderSection('fields'))
132 28
                        ->append($this->builderSection('args'))
133 28
                    ->end()
134 28
                ->end()
135
136 28
            ->end()
137 28
        ->end();
138
139 28
        return $node;
140
    }
141
142 28
    private function servicesSection()
143
    {
144 28
        $builder = new TreeBuilder('services');
145
        /** @var ArrayNodeDefinition $node */
146 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'services');
147
        $node
148 28
            ->addDefaultsIfNotSet()
149 28
            ->children()
150 28
                ->scalarNode('executor')
151 28
                    ->defaultValue(self::NAME.'.executor.default')
152 28
                ->end()
153 28
                ->scalarNode('promise_adapter')
154 28
                    ->defaultValue(self::NAME.'.promise_adapter.default')
155 28
                ->end()
156 28
                ->scalarNode('expression_language')
157 28
                    ->defaultValue(self::NAME.'.expression_language.default')
158 28
                ->end()
159 28
                ->scalarNode('cache_expression_language_parser')->end()
160 28
            ->end()
161 28
        ->end();
162
163 28
        return $node;
164
    }
165
166 28
    private function securitySection()
167
    {
168 28
        $builder = new TreeBuilder('security');
169
        /** @var ArrayNodeDefinition $node */
170 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'security');
171
        $node
172 28
            ->addDefaultsIfNotSet()
173 28
            ->children()
174 28
                ->append($this->securityQuerySection('query_max_depth', QueryDepth::DISABLED))
0 ignored issues
show
Bug introduced by
GraphQL\Validator\Rules\QueryDepth::DISABLED of type integer is incompatible with the type boolean expected by parameter $disabledValue of Overblog\GraphQLBundle\D...:securityQuerySection(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
                ->append($this->securityQuerySection('query_max_depth', /** @scrutinizer ignore-type */ QueryDepth::DISABLED))
Loading history...
175 28
                ->append($this->securityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
176 28
                ->booleanNode('enable_introspection')->defaultTrue()->end()
177 28
                ->booleanNode('handle_cors')->defaultFalse()->end()
0 ignored issues
show
Bug introduced by
The method booleanNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

177
                ->/** @scrutinizer ignore-call */ booleanNode('handle_cors')->defaultFalse()->end()
Loading history...
178 28
            ->end()
179 28
        ->end();
180
181 28
        return $node;
182
    }
183
184 28
    private function definitionsSchemaSection()
185
    {
186 28
        $builder = new TreeBuilder('schema');
187
        /** @var ArrayNodeDefinition $node */
188 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'schema');
189
        $node
190 28
            ->beforeNormalization()
191
                ->ifTrue(function ($v) {
192 20
                    return isset($v['query']) && \is_string($v['query']) || isset($v['mutation']) && \is_string($v['mutation']);
193 28
                })
194
                ->then(function ($v) {
195 19
                    return ['default' => $v];
196 28
                })
197 28
            ->end()
198 28
            ->useAttributeAsKey('name')
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
            ->/** @scrutinizer ignore-call */ useAttributeAsKey('name')
Loading history...
199 28
            ->prototype('array')
200 28
                ->addDefaultsIfNotSet()
201 28
                ->children()
202 28
                    ->scalarNode('query')->defaultNull()->end()
203 28
                    ->scalarNode('mutation')->defaultNull()->end()
204 28
                    ->scalarNode('subscription')->defaultNull()->end()
205 28
                    ->arrayNode('resolver_maps')
206 28
                        ->defaultValue([])
207 28
                        ->prototype('scalar')->end()
208 28
                    ->end()
209 28
                    ->arrayNode('types')
210 28
                        ->defaultValue([])
211 28
                        ->prototype('scalar')->end()
212 28
                    ->end()
213 28
                ->end()
214 28
            ->end()
215 28
        ->end();
216
217 28
        return $node;
218
    }
219
220 28
    private function definitionsMappingsSection()
221
    {
222 28
        $builder = new TreeBuilder('mappings');
223 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
224
        $node
225 28
            ->children()
226 28
                ->arrayNode('auto_discover')
227 28
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
228 28
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
229 28
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
230 28
                    ->addDefaultsIfNotSet()
231 28
                    ->children()
232 28
                        ->booleanNode('bundles')->defaultFalse()->end()
233 28
                        ->booleanNode('root_dir')->defaultFalse()->end()
234 28
                    ->end()
235 28
                ->end()
236 28
                ->arrayNode('types')
237 28
                    ->prototype('array')
238 28
                        ->addDefaultsIfNotSet()
239 28
                        ->beforeNormalization()
240
                            ->ifTrue(function ($v) {
241 20
                                return isset($v['type']) && \is_string($v['type']);
242 28
                            })
243
                            ->then(function ($v) {
244 19
                                if ('yml' === $v['type']) {
245 5
                                    $v['types'] = ['yaml'];
246
                                } else {
247 14
                                    $v['types'] = [$v['type']];
248
                                }
249 19
                                unset($v['type']);
250
251 19
                                return $v;
252 28
                            })
253 28
                        ->end()
254 28
                        ->children()
255 28
                            ->arrayNode('types')
256 28
                                ->prototype('enum')->values(\array_keys(OverblogGraphQLTypesExtension::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
257 28
                            ->end()
258 28
                            ->scalarNode('dir')->defaultNull()->end()
259 28
                            ->scalarNode('suffix')->defaultValue(OverblogGraphQLTypesExtension::DEFAULT_TYPES_SUFFIX)->end()
260 28
                        ->end()
261 28
                    ->end()
262 28
                ->end()
263 28
            ->end()
264
        ;
265
266 28
        return $node;
267
    }
268
269 28
    private function doctrineSection()
270
    {
271 28
        $builder = new TreeBuilder('doctrine');
272
        /** @var ArrayNodeDefinition $node */
273 28
        $node = self::getRootNodeWithoutDeprecation($builder, 'doctrine');
274
        $node
275 28
            ->addDefaultsIfNotSet()
276 28
            ->children()
277 28
                ->arrayNode('types_mapping')
278 28
                    ->defaultValue([])
279 28
                    ->prototype('scalar')->end()
280 28
                ->end()
0 ignored issues
show
Bug introduced by
The method end() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Config...ion\Builder\TreeBuilder. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

280
                ->/** @scrutinizer ignore-call */ end()
Loading history...
281 28
            ->end()
282
        ;
283
284 28
        return $node;
285
    }
286
287
    /**
288
     * @param string $name
289
     *
290
     * @return ArrayNodeDefinition
291
     */
292 28
    private function builderSection($name)
293
    {
294 28
        $builder = new TreeBuilder($name);
295
        /** @var ArrayNodeDefinition $node */
296 28
        $node = self::getRootNodeWithoutDeprecation($builder, $name);
297 28
        $node->beforeNormalization()
298
            ->ifTrue(function ($v) {
299 5
                return \is_array($v) && !empty($v);
300 28
            })
301
            ->then(function ($v) {
302 5
                foreach ($v as $key => &$config) {
303 5
                    if (\is_string($config)) {
304
                        $config = [
305 5
                            'alias' => $key,
306 5
                            'class' => $config,
307
                        ];
308
                    }
309
                }
310
311 5
                return $v;
312 28
            })
313 28
        ->end();
314
315 28
        $node->prototype('array')
316 28
            ->children()
317 28
                ->scalarNode('alias')->isRequired()->end()
318 28
                ->scalarNode('class')->isRequired()->end()
319 28
            ->end()
320 28
        ->end()
321
        ;
322
323 28
        return $node;
324
    }
325
326
    /**
327
     * @param string $name
328
     * @param bool   $disabledValue
329
     *
330
     * @return ScalarNodeDefinition
331
     */
332 28
    private function securityQuerySection($name, $disabledValue)
333
    {
334 28
        $builder = new TreeBuilder($name, 'scalar');
335
        /** @var ScalarNodeDefinition $node */
336 28
        $node = self::getRootNodeWithoutDeprecation($builder, $name, 'scalar');
337 28
        $node->beforeNormalization()
338
                ->ifTrue(function ($v) {
339 22
                    return \is_string($v) && \is_numeric($v);
340 28
                })
341
                ->then(function ($v) {
342
                    return (int) $v;
343 28
                })
344 28
            ->end();
345
346
        $node
347 28
            ->info('Disabled if equal to false.')
348 28
            ->beforeNormalization()
349
                ->ifTrue(function ($v) {
350 22
                    return false === $v;
351 28
                })
352
                ->then(function () use ($disabledValue) {
353 22
                    return $disabledValue;
354 28
                })
355 28
            ->end()
356 28
            ->defaultValue($disabledValue)
357 28
            ->validate()
358
                ->ifTrue(function ($v) {
359 22
                    return \is_int($v) && $v < 0;
360 28
                })
361 28
                ->thenInvalid(\sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
362 28
            ->end()
363
        ;
364
365 28
        return $node;
366
    }
367
368
    /**
369
     * @internal
370
     *
371
     * @param TreeBuilder $builder
372
     * @param string|null $name
373
     * @param string      $type
374
     *
375
     * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
376
     */
377 33
    public static function getRootNodeWithoutDeprecation(TreeBuilder $builder, string $name, string $type = 'array')
378
    {
379
        // BC layer for symfony/config 4.1 and older
380 33
        return \method_exists($builder, 'getRootNode') ? $builder->getRootNode() : $builder->root($name, $type);
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

380
        return \method_exists($builder, 'getRootNode') ? $builder->getRootNode() : /** @scrutinizer ignore-deprecated */ $builder->root($name, $type);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
381
    }
382
}
383