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 (#436)
by Jérémiah
22:48
created

Configuration::batchingMethodSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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

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

170
                ->append($this->securityQuerySection('query_max_depth', /** @scrutinizer ignore-type */ QueryDepth::DISABLED))
Loading history...
171 33
                ->append($this->securityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
172 33
                ->booleanNode('enable_introspection')->defaultTrue()->end()
173 33
                ->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

173
                ->/** @scrutinizer ignore-call */ booleanNode('handle_cors')->defaultFalse()->end()
Loading history...
174 33
            ->end()
175
        ->end();
176 33
177
        return $node;
178
    }
179 33
180
    private function definitionsSchemaSection()
181 33
    {
182
        $builder = new TreeBuilder('schema');
183 33
        /** @var ArrayNodeDefinition $node */
184
        $node = self::getRootNodeWithoutDeprecation($builder, 'schema');
185 33
        $node
186
            ->beforeNormalization()
187 29
                ->ifTrue(function ($v) {
188 33
                    return isset($v['query']) && \is_string($v['query']) || isset($v['mutation']) && \is_string($v['mutation']);
189
                })
190 28
                ->then(function ($v) {
191 33
                    return ['default' => $v];
192 33
                })
193 33
            ->end()
194 33
            ->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

194
            ->/** @scrutinizer ignore-call */ useAttributeAsKey('name')
Loading history...
195 33
            ->prototype('array')
196 33
                ->addDefaultsIfNotSet()
197 33
                ->children()
198 33
                    ->scalarNode('query')->defaultNull()->end()
199 33
                    ->scalarNode('mutation')->defaultNull()->end()
200 33
                    ->scalarNode('subscription')->defaultNull()->end()
201 33
                    ->arrayNode('resolver_maps')
202 33
                        ->defaultValue([])
203 33
                        ->prototype('scalar')->end()
204 33
                    ->end()
205 33
                    ->arrayNode('types')
206 33
                        ->defaultValue([])
207 33
                        ->prototype('scalar')->end()
208 33
                    ->end()
209 33
                ->end()
210 33
            ->end()
211
        ->end();
212 33
213
        return $node;
214
    }
215 33
216
    private function definitionsMappingsSection()
217 33
    {
218 33
        $builder = new TreeBuilder('mappings');
219
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
220 33
        $node
221 33
            ->children()
222 33
                ->arrayNode('auto_discover')
223 33
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
224 33
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
225 33
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
226 33
                    ->addDefaultsIfNotSet()
227 33
                    ->children()
228 33
                        ->booleanNode('bundles')->defaultTrue()->end()
229 33
                        ->booleanNode('root_dir')->defaultTrue()->end()
230 33
                    ->end()
231 33
                ->end()
232 33
                ->arrayNode('types')
233 33
                    ->prototype('array')
234 33
                        ->addDefaultsIfNotSet()
235
                        ->beforeNormalization()
236 29
                            ->ifTrue(function ($v) {
237 33
                                return isset($v['type']) && \is_string($v['type']);
238
                            })
239 28
                            ->then(function ($v) {
240 7
                                if ('yml' === $v['type']) {
241
                                    $v['types'] = ['yaml'];
242 21
                                } else {
243
                                    $v['types'] = [$v['type']];
244 28
                                }
245
                                unset($v['type']);
246 28
247 33
                                return $v;
248 33
                            })
249 33
                        ->end()
250 33
                        ->children()
251 33
                            ->arrayNode('types')
252 33
                                ->prototype('enum')->values(\array_keys(OverblogGraphQLTypesExtension::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
253 33
                            ->end()
254 33
                            ->scalarNode('dir')->defaultNull()->end()
255 33
                            ->scalarNode('suffix')->defaultValue(OverblogGraphQLTypesExtension::DEFAULT_TYPES_SUFFIX)->end()
256 33
                        ->end()
257 33
                    ->end()
258 33
                ->end()
259
            ->end()
260
        ;
261 33
262
        return $node;
263
    }
264
265
    /**
266
     * @param string $name
267
     *
268
     * @return ArrayNodeDefinition
269 33
     */
270
    private function builderSection($name)
271 33
    {
272
        $builder = new TreeBuilder($name);
273 33
        /** @var ArrayNodeDefinition $node */
274 33
        $node = self::getRootNodeWithoutDeprecation($builder, $name);
275
        $node->beforeNormalization()
276 1
            ->ifTrue(function ($v) {
277 33
                return \is_array($v) && !empty($v);
278
            })
279 1
            ->then(function ($v) {
280 1
                foreach ($v as $key => &$config) {
281
                    if (\is_string($config)) {
282 1
                        $config = [
283 1
                            'alias' => $key,
284
                            'class' => $config,
285
                        ];
286
                    }
287
                }
288 1
289 33
                return $v;
290 33
            })
291
        ->end();
292 33
293 33
        $node->prototype('array')
294 33
            ->children()
295 33
                ->scalarNode('alias')->isRequired()->end()
296 33
                ->scalarNode('class')->isRequired()->end()
297 33
            ->end()
298
        ->end()
299
        ;
300 33
301
        return $node;
302
    }
303
304
    /**
305
     * @param string $name
306
     * @param bool   $disabledValue
307
     *
308
     * @return ScalarNodeDefinition
309 33
     */
310
    private function securityQuerySection($name, $disabledValue)
311 33
    {
312
        $builder = new TreeBuilder($name, 'scalar');
313 33
        /** @var ScalarNodeDefinition $node */
314 33
        $node = self::getRootNodeWithoutDeprecation($builder, $name, 'scalar');
315
        $node->beforeNormalization()
316 31
                ->ifTrue(function ($v) {
317 33
                    return \is_string($v) && \is_numeric($v);
318
                })
319 4
                ->then(function ($v) {
320 33
                    return (int) $v;
321 33
                })
322
            ->end();
323
324 33
        $node
325 33
            ->info('Disabled if equal to false.')
326
            ->beforeNormalization()
327 31
                ->ifTrue(function ($v) {
328 33
                    return false === $v;
329
                })
330 31
                ->then(function () use ($disabledValue) {
331 33
                    return $disabledValue;
332 33
                })
333 33
            ->end()
334 33
            ->defaultFalse()
335
            ->validate()
336 31
                ->ifTrue(function ($v) {
337 33
                    return \is_int($v) && $v < 0;
338 33
                })
339 33
                ->thenInvalid(\sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
340
            ->end()
341
        ;
342 33
343
        return $node;
344
    }
345
346
    private function routesSection()
347
    {
348
        $builder = new TreeBuilder('routes');
349
        /** @var ScalarNodeDefinition $node */
350
        $node = self::getRootNodeWithoutDeprecation($builder, 'routes');
351
352
        $node
353
            ->addDefaultsIfNotSet()
0 ignored issues
show
Bug introduced by
The method addDefaultsIfNotSet() does not exist on Symfony\Component\Config...er\ScalarNodeDefinition. ( Ignorable by Annotation )

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

353
            ->/** @scrutinizer ignore-call */ 
354
              addDefaultsIfNotSet()

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...
354 38
            ->children()
355
                ->scalarNode('endpoint_prefix')
356
                    ->defaultValue('')
357 38
                    ->isRequired()
358
                    ->info('Use to change GraphQL default route prefix.')
359
                ->end()
360
                ->scalarNode('multiple_endpoint_prefix')
361
                    ->defaultValue('graphql')
362
                    ->isRequired()
363
                    ->info('Use to change multiple GraphQL default route prefix.')
364
                ->end()
365
            ->end();
366
367
        return $node;
368
    }
369
370
    /**
371
     * @internal
372
     *
373
     * @param TreeBuilder $builder
374
     * @param string|null $name
375
     * @param string      $type
376
     *
377
     * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
378
     */
379
    public static function getRootNodeWithoutDeprecation(TreeBuilder $builder, string $name, string $type = 'array')
380
    {
381
        // BC layer for symfony/config 4.1 and older
382
        return \method_exists($builder, 'getRootNode') ? $builder->getRootNode() : $builder->root($name, $type);
383
    }
384
}
385