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 — 0.13 (#745)
by Timur
12:56
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
use Symfony\Component\HttpKernel\Kernel;
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
     * @param string|null $cacheDir
39
     */
40 42
    public function __construct(bool $debug, string $cacheDir = null)
41
    {
42 42
        $this->debug = (bool) $debug;
43 42
        $this->cacheDir = $cacheDir;
44 42
    }
45
46 42
    public function getConfigTreeBuilder()
47
    {
48 42
        $treeBuilder = new TreeBuilder(self::NAME);
49
50 42
        $rootNode = self::getRootNodeWithoutDeprecation($treeBuilder, self::NAME);
51
52
        $rootNode
53 42
            ->children()
54 42
                ->append($this->batchingMethodSection())
55 42
                ->append($this->definitionsSection())
56 42
                ->append($this->errorsHandlerSection())
57 42
                ->append($this->servicesSection())
58 42
                ->append($this->securitySection())
59 42
                ->append($this->doctrineSection())
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(...$this->getDeprecationArgs())
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
    /**
228
     * BC layer for symfony/config <5.1 .
229
     */
230 42
    private function getDeprecationArgs()
231
    {
232 42
        $msg = 'The "%path%.%node%" configuration is deprecated since version 0.13 and will be removed in 1.0 Add the "overblog_graphql.resolver_map" tag to the services instead.';
233
234 42
        if (Kernel::VERSION_ID < 50100) {
235 42
            return [$msg];
236
        }
237
238
        return ['overblog/graphql-bundle', '0.13', $msg];
239
    }
240
241 42
    private function definitionsMappingsSection()
242
    {
243 42
        $builder = new TreeBuilder('mappings');
244 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
245
        $node
246 42
            ->children()
247 42
                ->arrayNode('auto_discover')
248 42
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
249 42
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
250 42
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
251 42
                    ->addDefaultsIfNotSet()
252 42
                    ->children()
253 42
                        ->booleanNode('bundles')->defaultFalse()->end()
254 42
                        ->booleanNode('root_dir')->defaultFalse()->end()
255 42
                    ->end()
256 42
                ->end()
257 42
                ->arrayNode('types')
258 42
                    ->prototype('array')
259 42
                        ->addDefaultsIfNotSet()
260 42
                        ->beforeNormalization()
261
                            ->ifTrue(function ($v) {
262 33
                                return isset($v['type']) && \is_string($v['type']);
263 42
                            })
264
                            ->then(function ($v) {
265 32
                                if ('yml' === $v['type']) {
266 7
                                    $v['types'] = ['yaml'];
267
                                } else {
268 25
                                    $v['types'] = [$v['type']];
269
                                }
270 32
                                unset($v['type']);
271
272 32
                                return $v;
273 42
                            })
274 42
                        ->end()
275 42
                        ->children()
276 42
                            ->arrayNode('types')
277 42
                                ->prototype('enum')->values(\array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
278 42
                            ->end()
279 42
                            ->scalarNode('dir')->defaultNull()->end()
280 42
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
281 42
                        ->end()
282 42
                    ->end()
283 42
                ->end()
284 42
            ->end()
285
        ;
286
287 42
        return $node;
288
    }
289
290 42
    private function doctrineSection()
291
    {
292 42
        $builder = new TreeBuilder('doctrine');
293
        /** @var ArrayNodeDefinition $node */
294 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'doctrine');
295
        $node
296 42
            ->addDefaultsIfNotSet()
297 42
            ->children()
298 42
                ->arrayNode('types_mapping')
299 42
                    ->defaultValue([])
300 42
                    ->prototype('scalar')->end()
301 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

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

401
        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...
402
    }
403
}
404