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 (#638)
by Vincent
06:16
created

Configuration::errorsHandlerSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 33
rs 9.456
ccs 29
cts 29
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\NodeDefinition;
20
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
21
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
22
use Symfony\Component\Config\Definition\ConfigurationInterface;
23
24
class Configuration implements ConfigurationInterface
25
{
26
    public const NAME = 'overblog_graphql';
27
28
    /** bool */
29
    private $debug;
30
31
    /** null|string */
32
    private $cacheDir;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param bool $debug Whether to use the debug mode
38
     */
39 42
    public function __construct(bool $debug, string $cacheDir = null)
40
    {
41 42
        $this->debug = (bool) $debug;
42 42
        $this->cacheDir = $cacheDir;
43 42
    }
44
45 42
    public function getConfigTreeBuilder()
46
    {
47 42
        $treeBuilder = new TreeBuilder(self::NAME);
48
49 42
        $rootNode = self::getRootNodeWithoutDeprecation($treeBuilder, self::NAME);
50
51
        $rootNode
52 42
            ->children()
53 42
                ->append($this->batchingMethodSection())
54 42
                ->append($this->definitionsSection())
55 42
                ->append($this->errorsHandlerSection())
56 42
                ->append($this->servicesSection())
57 42
                ->append($this->securitySection())
58 42
                ->append($this->doctrineSection())
59 42
                ->append($this->profilerSection())
60 42
            ->end();
61
62 42
        return $treeBuilder;
63
    }
64
65 42
    private function batchingMethodSection()
66
    {
67 42
        $builder = new TreeBuilder('batching_method', 'enum');
68
        /** @var EnumNodeDefinition $node */
69 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'batching_method', 'enum');
70
71
        $node
72 42
            ->values(['relay', 'apollo'])
73 42
            ->defaultValue('relay')
74 42
        ->end();
75
76 42
        return $node;
77
    }
78
79 42
    private function errorsHandlerSection()
80
    {
81 42
        $builder = new TreeBuilder('errors_handler');
82
        /** @var ArrayNodeDefinition $node */
83 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'errors_handler');
84
        $node
85 42
            ->treatFalseLike(['enabled' => false])
86 42
            ->treatTrueLike(['enabled' => true])
87 42
            ->treatNullLike(['enabled' => true])
88 42
            ->addDefaultsIfNotSet()
89 42
            ->children()
90 42
                ->booleanNode('enabled')->defaultTrue()->end()
91 42
                ->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

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

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

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

204
            ->/** @scrutinizer ignore-call */ useAttributeAsKey('name')
Loading history...
205 42
            ->prototype('array')
206 42
                ->addDefaultsIfNotSet()
207 42
                ->children()
208 42
                    ->scalarNode('query')->defaultNull()->end()
209 42
                    ->scalarNode('mutation')->defaultNull()->end()
210 42
                    ->scalarNode('subscription')->defaultNull()->end()
211 42
                    ->arrayNode('resolver_maps')
212 42
                        ->defaultValue([])
213 42
                        ->prototype('scalar')->end()
214 42
                        ->setDeprecated('The "%path%.%node%" configuration is deprecated since version 0.13 and will be removed in 0.14. Add the "overblog_graphql.resolver_map" tag to the services instead.', '0.13')
215 42
                    ->end()
216 42
                    ->arrayNode('types')
217 42
                        ->defaultValue([])
218 42
                        ->prototype('scalar')->end()
219 42
                    ->end()
220 42
                ->end()
221 42
            ->end()
222 42
        ->end();
223
224 42
        return $node;
225
    }
226
227 42
    private function definitionsMappingsSection()
228
    {
229 42
        $builder = new TreeBuilder('mappings');
230 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
231
        $node
232 42
            ->children()
233 42
                ->arrayNode('auto_discover')
234 42
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
235 42
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
236 42
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
237 42
                    ->addDefaultsIfNotSet()
238 42
                    ->children()
239 42
                        ->booleanNode('bundles')->defaultFalse()->end()
240 42
                        ->booleanNode('root_dir')->defaultFalse()->end()
241 42
                    ->end()
242 42
                ->end()
243 42
                ->arrayNode('types')
244 42
                    ->prototype('array')
245 42
                        ->addDefaultsIfNotSet()
246 42
                        ->beforeNormalization()
247
                            ->ifTrue(function ($v) {
248 33
                                return isset($v['type']) && \is_string($v['type']);
249 42
                            })
250
                            ->then(function ($v) {
251 32
                                if ('yml' === $v['type']) {
252 7
                                    $v['types'] = ['yaml'];
253
                                } else {
254 25
                                    $v['types'] = [$v['type']];
255
                                }
256 32
                                unset($v['type']);
257
258 32
                                return $v;
259 42
                            })
260 42
                        ->end()
261 42
                        ->children()
262 42
                            ->arrayNode('types')
263 42
                                ->prototype('enum')->values(\array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
264 42
                            ->end()
265 42
                            ->scalarNode('dir')->defaultNull()->end()
266 42
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
267 42
                        ->end()
268 42
                    ->end()
269 42
                ->end()
270 42
            ->end()
271
        ;
272
273 42
        return $node;
274
    }
275
276 42
    private function doctrineSection()
277
    {
278 42
        $builder = new TreeBuilder('doctrine');
279
        /** @var ArrayNodeDefinition $node */
280 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'doctrine');
281
        $node
282 42
            ->addDefaultsIfNotSet()
283 42
            ->children()
284 42
                ->arrayNode('types_mapping')
285 42
                    ->defaultValue([])
286 42
                    ->prototype('scalar')->end()
287 42
                ->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

287
                ->/** @scrutinizer ignore-call */ end()
Loading history...
288 42
            ->end()
289
        ;
290
291 42
        return $node;
292
    }
293
294 42
    private function profilerSection()
295
    {
296 42
        $builder = new TreeBuilder('profiler');
297
        /** @var ArrayNodeDefinition $node */
298 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'profiler');
299
        $node
300 42
            ->addDefaultsIfNotSet()
301 42
            ->children()
302 42
                ->scalarNode('query_match')->defaultNull()->end()
303 42
            ->end()
304
        ;
305
306 42
        return $node;
307
    }
308
309
    /**
310
     * @param string $name
311
     *
312
     * @return ArrayNodeDefinition
313
     */
314 42
    private function builderSection($name)
315
    {
316 42
        $builder = new TreeBuilder($name);
317
        /** @var ArrayNodeDefinition $node */
318 42
        $node = self::getRootNodeWithoutDeprecation($builder, $name);
319 42
        $node->beforeNormalization()
320
            ->ifTrue(function ($v) {
321 5
                return \is_array($v) && !empty($v);
322 42
            })
323
            ->then(function ($v) {
324 5
                foreach ($v as $key => &$config) {
325 5
                    if (\is_string($config)) {
326
                        $config = [
327 5
                            'alias' => $key,
328 5
                            'class' => $config,
329
                        ];
330
                    }
331
                }
332
333 5
                return $v;
334 42
            })
335 42
        ->end();
336
337 42
        $node->prototype('array')
338 42
            ->children()
339 42
                ->scalarNode('alias')->isRequired()->end()
340 42
                ->scalarNode('class')->isRequired()->end()
341 42
            ->end()
342 42
        ->end()
343
        ;
344
345 42
        return $node;
346
    }
347
348
    /**
349
     * @param string $name
350
     * @param bool   $disabledValue
351
     *
352
     * @return ScalarNodeDefinition
353
     */
354 42
    private function securityQuerySection($name, $disabledValue)
355
    {
356 42
        $builder = new TreeBuilder($name, 'scalar');
357
        /** @var ScalarNodeDefinition $node */
358 42
        $node = self::getRootNodeWithoutDeprecation($builder, $name, 'scalar');
359 42
        $node->beforeNormalization()
360
                ->ifTrue(function ($v) {
361 36
                    return \is_string($v) && \is_numeric($v);
362 42
                })
363
                ->then(function ($v) {
364 2
                    return (int) $v;
365 42
                })
366 42
            ->end();
367
368
        $node
369 42
            ->info('Disabled if equal to false.')
370 42
            ->beforeNormalization()
371
                ->ifTrue(function ($v) {
372 36
                    return false === $v;
373 42
                })
374
                ->then(function () use ($disabledValue) {
375 36
                    return $disabledValue;
376 42
                })
377 42
            ->end()
378 42
            ->defaultValue($disabledValue)
379 42
            ->validate()
380
                ->ifTrue(function ($v) {
381 36
                    return \is_int($v) && $v < 0;
382 42
                })
383 42
                ->thenInvalid(\sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
384 42
            ->end()
385
        ;
386
387 42
        return $node;
388
    }
389
390
    /**
391
     * @return ArrayNodeDefinition|NodeDefinition
392
     *
393
     * @internal
394
     */
395 48
    public static function getRootNodeWithoutDeprecation(TreeBuilder $builder, string $name, string $type = 'array')
396
    {
397
        // BC layer for symfony/config 4.1 and older
398 48
        return \method_exists($builder, 'getRootNode') ? $builder->getRootNode() : $builder->root($name, $type);
0 ignored issues
show
Bug introduced by
The method root() does not exist on Symfony\Component\Config...ion\Builder\TreeBuilder. ( Ignorable by Annotation )

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

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

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...
399
    }
400
}
401