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 — 0.13 (#715)
by Timur
11:48
created

Configuration::doctrineSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
rs 9.9
ccs 11
cts 11
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
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 [
239
            'overblog/graphql-bundle',
240
            '0.13',
241
            $msg
242
        ];
243
    }
244
245 42
    private function definitionsMappingsSection()
246
    {
247 42
        $builder = new TreeBuilder('mappings');
248 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
249
        $node
250 42
            ->children()
251 42
                ->arrayNode('auto_discover')
252 42
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
253 42
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
254 42
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
255 42
                    ->addDefaultsIfNotSet()
256 42
                    ->children()
257 42
                        ->booleanNode('bundles')->defaultFalse()->end()
258 42
                        ->booleanNode('root_dir')->defaultFalse()->end()
259 42
                    ->end()
260 42
                ->end()
261 42
                ->arrayNode('types')
262 42
                    ->prototype('array')
263 42
                        ->addDefaultsIfNotSet()
264 42
                        ->beforeNormalization()
265
                            ->ifTrue(function ($v) {
266 33
                                return isset($v['type']) && \is_string($v['type']);
267 42
                            })
268
                            ->then(function ($v) {
269 32
                                if ('yml' === $v['type']) {
270 7
                                    $v['types'] = ['yaml'];
271
                                } else {
272 25
                                    $v['types'] = [$v['type']];
273
                                }
274 32
                                unset($v['type']);
275
276 32
                                return $v;
277 42
                            })
278 42
                        ->end()
279 42
                        ->children()
280 42
                            ->arrayNode('types')
281 42
                                ->prototype('enum')->values(\array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
282 42
                            ->end()
283 42
                            ->scalarNode('dir')->defaultNull()->end()
284 42
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
285 42
                        ->end()
286 42
                    ->end()
287 42
                ->end()
288 42
            ->end()
289
        ;
290
291 42
        return $node;
292
    }
293
294 42
    private function doctrineSection()
295
    {
296 42
        $builder = new TreeBuilder('doctrine');
297
        /** @var ArrayNodeDefinition $node */
298 42
        $node = self::getRootNodeWithoutDeprecation($builder, 'doctrine');
299
        $node
300 42
            ->addDefaultsIfNotSet()
301 42
            ->children()
302 42
                ->arrayNode('types_mapping')
303 42
                    ->defaultValue([])
304 42
                    ->prototype('scalar')->end()
305 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

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

405
        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...
406
    }
407
}
408