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

Passed
Pull Request — master (#590)
by Daniel
22:44
created

Configuration::securitySection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 16
rs 9.8666
ccs 12
cts 12
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\DependencyInjection;
6
7
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
8
use GraphQL\Validator\Rules\QueryComplexity;
9
use GraphQL\Validator\Rules\QueryDepth;
10
use Overblog\GraphQLBundle\Definition\Argument;
11
use Overblog\GraphQLBundle\DependencyInjection\Compiler\ConfigParserPass;
12
use Overblog\GraphQLBundle\Error\ErrorHandler;
13
use Overblog\GraphQLBundle\EventListener\ErrorLoggerListener;
14
use Overblog\GraphQLBundle\Executor\Executor;
15
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage;
16
use Overblog\GraphQLBundle\Resolver\FieldResolver;
17
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
18
use Symfony\Component\Config\Definition\Builder\EnumNodeDefinition;
19
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
20
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
21
use Symfony\Component\Config\Definition\ConfigurationInterface;
22
23
class Configuration implements ConfigurationInterface
24
{
25
    public const NAME = 'overblog_graphql';
26
27
    /** bool */
28
    private $debug;
29
30
    /** null|string */
31
    private $cacheDir;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param bool        $debug    Whether to use the debug mode
37
     * @param string|null $cacheDir
38
     */
39 41
    public function __construct(bool $debug, string $cacheDir = null)
40
    {
41 41
        $this->debug = (bool) $debug;
42 41
        $this->cacheDir = $cacheDir;
43 41
    }
44
45 41
    public function getConfigTreeBuilder()
46
    {
47 41
        $treeBuilder = new TreeBuilder(self::NAME);
48
49 41
        $rootNode = self::getRootNodeWithoutDeprecation($treeBuilder, self::NAME);
50
51
        $rootNode
52 41
            ->children()
53 41
                ->append($this->batchingMethodSection())
54 41
                ->append($this->definitionsSection())
55 41
                ->append($this->errorsHandlerSection())
56 41
                ->append($this->servicesSection())
57 41
                ->append($this->securitySection())
58 41
                ->append($this->doctrineSection())
59 41
            ->end();
60
61 41
        return $treeBuilder;
62
    }
63
64 41
    private function batchingMethodSection()
65
    {
66 41
        $builder = new TreeBuilder('batching_method', 'enum');
67
        /** @var EnumNodeDefinition $node */
68 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'batching_method', 'enum');
69
70
        $node
71 41
            ->values(['relay', 'apollo'])
72 41
            ->defaultValue('relay')
73 41
        ->end();
74
75 41
        return $node;
76
    }
77
78 41
    private function errorsHandlerSection()
79
    {
80 41
        $builder = new TreeBuilder('errors_handler');
81
        /** @var ArrayNodeDefinition $node */
82 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'errors_handler');
83
        $node
84 41
            ->treatFalseLike(['enabled' => false])
85 41
            ->treatTrueLike(['enabled' => true])
86 41
            ->treatNullLike(['enabled' => true])
87 41
            ->addDefaultsIfNotSet()
88 41
            ->children()
89 41
                ->booleanNode('enabled')->defaultTrue()->end()
90 41
                ->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

90
                ->/** @scrutinizer ignore-call */ scalarNode('internal_error_message')->defaultValue(ErrorHandler::DEFAULT_ERROR_MESSAGE)->end()
Loading history...
91 41
                ->booleanNode('rethrow_internal_exceptions')->defaultFalse()->end()
92 41
                ->booleanNode('debug')->defaultValue($this->debug)->end()
93 41
                ->booleanNode('log')->defaultTrue()->end()
94 41
                ->scalarNode('logger_service')->defaultValue(ErrorLoggerListener::DEFAULT_LOGGER_SERVICE)->end()
95 41
                ->booleanNode('map_exceptions_to_parent')->defaultFalse()->end()
96 41
                ->arrayNode('exceptions')
97 41
                    ->addDefaultsIfNotSet()
98 41
                    ->children()
99 41
                        ->arrayNode('warnings')
100 41
                            ->treatNullLike([])
101 41
                            ->prototype('scalar')->end()
102 41
                        ->end()
103 41
                        ->arrayNode('errors')
104 41
                            ->treatNullLike([])
105 41
                            ->prototype('scalar')->end()
106 41
                    ->end()
107 41
                ->end()
108 41
            ->end();
109
110 41
        return $node;
111
    }
112
113 41
    private function definitionsSection()
114
    {
115 41
        $builder = new TreeBuilder('definitions');
116
        /** @var ArrayNodeDefinition $node */
117 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'definitions');
118
        $node
119 41
            ->addDefaultsIfNotSet()
120 41
            ->children()
121 41
                ->scalarNode('argument_class')->defaultValue(Argument::class)->end()
122 41
                ->scalarNode('use_experimental_executor')->defaultFalse()->end()
123 41
                ->scalarNode('default_field_resolver')->defaultValue(FieldResolver::class)->end()
124 41
                ->scalarNode('class_namespace')->defaultValue('Overblog\\GraphQLBundle\\__DEFINITIONS__')->end()
125 41
                ->scalarNode('cache_dir')->defaultNull()->end()
126 41
                ->scalarNode('cache_dir_permissions')->defaultNull()->end()
127 41
                ->booleanNode('use_classloader_listener')->defaultTrue()->end()
128 41
                ->scalarNode('auto_compile')->defaultTrue()->end()
129 41
                ->booleanNode('show_debug_info')->info('Show some performance stats in extensions')->defaultFalse()->end()
130 41
                ->booleanNode('config_validation')->defaultValue($this->debug)->end()
131 41
                ->append($this->definitionsSchemaSection())
132 41
                ->append($this->definitionsMappingsSection())
133 41
                ->arrayNode('builders')
134 41
                    ->children()
135 41
                        ->append($this->builderSection('field'))
136 41
                        ->append($this->builderSection('fields'))
137 41
                        ->append($this->builderSection('args'))
138 41
                    ->end()
139 41
                ->end()
140
141 41
            ->end()
142 41
        ->end();
143
144 41
        return $node;
145
    }
146
147 41
    private function servicesSection()
148
    {
149 41
        $builder = new TreeBuilder('services');
150
        /** @var ArrayNodeDefinition $node */
151 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'services');
152
        $node
153 41
            ->addDefaultsIfNotSet()
154 41
            ->children()
155 41
                ->scalarNode('executor')
156 41
                    ->defaultValue(Executor::class)
157 41
                ->end()
158 41
                ->scalarNode('promise_adapter')
159 41
                    ->defaultValue(SyncPromiseAdapter::class)
160 41
                ->end()
161 41
                ->scalarNode('expression_language')
162 41
                    ->defaultValue(ExpressionLanguage::class)
163 41
                ->end()
164 41
                ->scalarNode('cache_expression_language_parser')->end()
165 41
            ->end()
166 41
        ->end();
167
168 41
        return $node;
169
    }
170
171 41
    private function securitySection()
172
    {
173 41
        $builder = new TreeBuilder('security');
174
        /** @var ArrayNodeDefinition $node */
175 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'security');
176
        $node
177 41
            ->addDefaultsIfNotSet()
178 41
            ->children()
179 41
                ->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

179
                ->append($this->securityQuerySection('query_max_depth', /** @scrutinizer ignore-type */ QueryDepth::DISABLED))
Loading history...
180 41
                ->append($this->securityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
181 41
                ->booleanNode('enable_introspection')->defaultTrue()->end()
182 41
                ->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

182
                ->/** @scrutinizer ignore-call */ booleanNode('handle_cors')->defaultFalse()->end()
Loading history...
183 41
            ->end()
184 41
        ->end();
185
186 41
        return $node;
187
    }
188
189 41
    private function definitionsSchemaSection()
190
    {
191 41
        $builder = new TreeBuilder('schema');
192
        /** @var ArrayNodeDefinition $node */
193 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'schema');
194
        $node
195 41
            ->beforeNormalization()
196
                ->ifTrue(function ($v) {
197 33
                    return isset($v['query']) && \is_string($v['query']) || isset($v['mutation']) && \is_string($v['mutation']);
198 41
                })
199
                ->then(function ($v) {
200 31
                    return ['default' => $v];
201 41
                })
202 41
            ->end()
203 41
            ->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

203
            ->/** @scrutinizer ignore-call */ useAttributeAsKey('name')
Loading history...
204 41
            ->prototype('array')
205 41
                ->addDefaultsIfNotSet()
206 41
                ->children()
207 41
                    ->scalarNode('query')->defaultNull()->end()
208 41
                    ->scalarNode('mutation')->defaultNull()->end()
209 41
                    ->scalarNode('subscription')->defaultNull()->end()
210 41
                    ->arrayNode('resolver_maps')
211 41
                        ->defaultValue([])
212 41
                        ->prototype('scalar')->end()
213 41
                    ->end()
214 41
                    ->arrayNode('types')
215 41
                        ->defaultValue([])
216 41
                        ->prototype('scalar')->end()
217 41
                    ->end()
218 41
                ->end()
219 41
            ->end()
220 41
        ->end();
221
222 41
        return $node;
223
    }
224
225 41
    private function definitionsMappingsSection()
226
    {
227 41
        $builder = new TreeBuilder('mappings');
228 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
229
        $node
230 41
            ->children()
231 41
                ->arrayNode('auto_discover')
232 41
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
233 41
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
234 41
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
235 41
                    ->addDefaultsIfNotSet()
236 41
                    ->children()
237 41
                        ->booleanNode('bundles')->defaultFalse()->end()
238 41
                        ->booleanNode('root_dir')->defaultFalse()->end()
239 41
                    ->end()
240 41
                ->end()
241 41
                ->scalarNode('types_callback')
242 41
                    ->defaultNull()
243 41
                    ->validate()
244
                        ->ifTrue(function ($callback) {
245
                            if (!empty($callback)) {
246
                                return !\is_subclass_of((string) $callback, TypesConfigurationCallbackInterface::class);
247
                            }
248
249
                            return false;
250 41
                        })
251 41
                        ->thenInvalid(
252 41
                            \sprintf(
253 41
                                'Types callback should be an instance of %s.',
254 41
                                TypesConfigurationCallbackInterface::class
255
                            )
256
                        )
257 41
                    ->end()
258 41
                ->end()
259 41
                ->arrayNode('types')
260 41
                    ->prototype('array')
261 41
                        ->addDefaultsIfNotSet()
262 41
                        ->beforeNormalization()
263
                            ->ifTrue(function ($v) {
264 32
                                return isset($v['type']) && \is_string($v['type']);
265 41
                            })
266
                            ->then(function ($v) {
267 31
                                if ('yml' === $v['type']) {
268 7
                                    $v['types'] = ['yaml'];
269
                                } else {
270 24
                                    $v['types'] = [$v['type']];
271
                                }
272 31
                                unset($v['type']);
273
274 31
                                return $v;
275 41
                            })
276 41
                        ->end()
277 41
                        ->children()
278 41
                            ->arrayNode('types')
279 41
                                ->prototype('enum')->values(\array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
280 41
                            ->end()
281 41
                            ->scalarNode('dir')->defaultNull()->end()
282 41
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
283 41
                        ->end()
284 41
                    ->end()
285 41
                ->end()
286 41
            ->end()
287
        ;
288
289 41
        return $node;
290
    }
291
292 41
    private function doctrineSection()
293
    {
294 41
        $builder = new TreeBuilder('doctrine');
295
        /** @var ArrayNodeDefinition $node */
296 41
        $node = self::getRootNodeWithoutDeprecation($builder, 'doctrine');
297
        $node
298 41
            ->addDefaultsIfNotSet()
299 41
            ->children()
300 41
                ->arrayNode('types_mapping')
301 41
                    ->defaultValue([])
302 41
                    ->prototype('scalar')->end()
303 41
                ->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

303
                ->/** @scrutinizer ignore-call */ end()
Loading history...
304 41
            ->end()
305
        ;
306
307 41
        return $node;
308
    }
309
310
    /**
311
     * @param string $name
312
     *
313
     * @return ArrayNodeDefinition
314
     */
315 41
    private function builderSection($name)
316
    {
317 41
        $builder = new TreeBuilder($name);
318
        /** @var ArrayNodeDefinition $node */
319 41
        $node = self::getRootNodeWithoutDeprecation($builder, $name);
320 41
        $node->beforeNormalization()
321
            ->ifTrue(function ($v) {
322 5
                return \is_array($v) && !empty($v);
323 41
            })
324
            ->then(function ($v) {
325 5
                foreach ($v as $key => &$config) {
326 5
                    if (\is_string($config)) {
327
                        $config = [
328 5
                            'alias' => $key,
329 5
                            'class' => $config,
330
                        ];
331
                    }
332
                }
333
334 5
                return $v;
335 41
            })
336 41
        ->end();
337
338 41
        $node->prototype('array')
339 41
            ->children()
340 41
                ->scalarNode('alias')->isRequired()->end()
341 41
                ->scalarNode('class')->isRequired()->end()
342 41
            ->end()
343 41
        ->end()
344
        ;
345
346 41
        return $node;
347
    }
348
349
    /**
350
     * @param string $name
351
     * @param bool   $disabledValue
352
     *
353
     * @return ScalarNodeDefinition
354
     */
355 41
    private function securityQuerySection($name, $disabledValue)
356
    {
357 41
        $builder = new TreeBuilder($name, 'scalar');
358
        /** @var ScalarNodeDefinition $node */
359 41
        $node = self::getRootNodeWithoutDeprecation($builder, $name, 'scalar');
360 41
        $node->beforeNormalization()
361
                ->ifTrue(function ($v) {
362 35
                    return \is_string($v) && \is_numeric($v);
363 41
                })
364
                ->then(function ($v) {
365 2
                    return (int) $v;
366 41
                })
367 41
            ->end();
368
369
        $node
370 41
            ->info('Disabled if equal to false.')
371 41
            ->beforeNormalization()
372
                ->ifTrue(function ($v) {
373 35
                    return false === $v;
374 41
                })
375
                ->then(function () use ($disabledValue) {
376 35
                    return $disabledValue;
377 41
                })
378 41
            ->end()
379 41
            ->defaultValue($disabledValue)
380 41
            ->validate()
381
                ->ifTrue(function ($v) {
382 35
                    return \is_int($v) && $v < 0;
383 41
                })
384 41
                ->thenInvalid(\sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
385 41
            ->end()
386
        ;
387
388 41
        return $node;
389
    }
390
391
    /**
392
     * @internal
393
     *
394
     * @param TreeBuilder $builder
395
     * @param string|null $name
396
     * @param string      $type
397
     *
398
     * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
399
     */
400 48
    public static function getRootNodeWithoutDeprecation(TreeBuilder $builder, string $name, string $type = 'array')
401
    {
402
        // BC layer for symfony/config 4.1 and older
403 48
        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

403
        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...
404
    }
405
}
406