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 (#695)
by Timur
07:20
created

Configuration::getRootNodeWithoutDeprecation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
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 function array_keys;
23
use function is_array;
24
use function is_int;
25
use function is_numeric;
26
use function is_string;
27
use function sprintf;
28
29
class Configuration implements ConfigurationInterface
30
{
31
    public const NAME = 'overblog_graphql';
32
33
    private bool $debug;
34
    private ?string $cacheDir;
35
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(): TreeBuilder
46
    {
47 42
        $treeBuilder = new TreeBuilder(self::NAME);
48
49 42
        $rootNode = $treeBuilder->getRootNode();
50
51
        // @phpstan-ignore-next-line
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
                ->append($this->profilerSection())
61 42
            ->end();
62
63 42
        return $treeBuilder;
64
    }
65
66 42
    private function batchingMethodSection(): EnumNodeDefinition
67
    {
68 42
        $builder = new TreeBuilder('batching_method', 'enum');
69
70
        /** @var EnumNodeDefinition $node */
71 42
        $node = $builder->getRootNode();
72
73
        $node
74 42
            ->values(['relay', 'apollo'])
75 42
            ->defaultValue('relay')
76 42
        ->end();
77
78 42
        return $node;
79
    }
80
81 42
    private function errorsHandlerSection(): ArrayNodeDefinition
82
    {
83 42
        $builder = new TreeBuilder('errors_handler');
84
85
        /** @var ArrayNodeDefinition $node */
86 42
        $node = $builder->getRootNode();
87
88
        // @phpstan-ignore-next-line
89
        $node
90 42
            ->treatFalseLike(['enabled' => false])
91 42
            ->treatTrueLike(['enabled' => true])
92 42
            ->treatNullLike(['enabled' => true])
93 42
            ->addDefaultsIfNotSet()
94 42
            ->children()
95 42
                ->booleanNode('enabled')->defaultTrue()->end()
96 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

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

196
                ->/** @scrutinizer ignore-call */ booleanNode('handle_cors')->defaultFalse()->end()
Loading history...
197 42
            ->end()
198 42
        ->end();
199
200 42
        return $node;
201
    }
202
203 42
    private function definitionsSchemaSection(): ArrayNodeDefinition
204
    {
205 42
        $builder = new TreeBuilder('schema');
206
207
        /** @var ArrayNodeDefinition $node */
208 42
        $node = $builder->getRootNode();
209
210
        // @phpstan-ignore-next-line
211
        $node
212 42
            ->beforeNormalization()
213 42
                ->ifTrue(fn ($v) => isset($v['query']) && is_string($v['query']) || isset($v['mutation']) && is_string($v['mutation']))
214 42
                ->then(fn ($v) => ['default' => $v])
215 42
            ->end()
216 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

216
            ->/** @scrutinizer ignore-call */ useAttributeAsKey('name')
Loading history...
217 42
            ->prototype('array')
218 42
                ->addDefaultsIfNotSet()
219 42
                ->children()
220 42
                    ->scalarNode('query')->defaultNull()->end()
221 42
                    ->scalarNode('mutation')->defaultNull()->end()
222 42
                    ->scalarNode('subscription')->defaultNull()->end()
223 42
                    ->arrayNode('resolver_maps')
224 42
                        ->defaultValue([])
225 42
                        ->prototype('scalar')->end()
226 42
                        ->setDeprecated('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.')
227 42
                    ->end()
228 42
                    ->arrayNode('types')
229 42
                        ->defaultValue([])
230 42
                        ->prototype('scalar')->end()
231 42
                    ->end()
232 42
                ->end()
233 42
            ->end()
234 42
        ->end();
235
236 42
        return $node;
237
    }
238
239 42
    private function definitionsMappingsSection(): ArrayNodeDefinition
240
    {
241 42
        $builder = new TreeBuilder('mappings');
242
243
        /** @var ArrayNodeDefinition $node */
244 42
        $node = $builder->getRootNode();
245
246
        // @phpstan-ignore-next-line
247
        $node
248 42
            ->children()
249 42
                ->arrayNode('auto_discover')
250 42
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
251 42
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
252 42
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
253 42
                    ->addDefaultsIfNotSet()
254 42
                    ->children()
255 42
                        ->booleanNode('bundles')->defaultFalse()->end()
256 42
                        ->booleanNode('root_dir')->defaultFalse()->end()
257 42
                    ->end()
258 42
                ->end()
259 42
                ->arrayNode('types')
260 42
                    ->prototype('array')
261 42
                        ->addDefaultsIfNotSet()
262 42
                        ->beforeNormalization()
263
                            ->ifTrue(function ($v) {
264 33
                                return isset($v['type']) && is_string($v['type']);
265 42
                            })
266
                            ->then(function ($v) {
267 32
                                if ('yml' === $v['type']) {
268 7
                                    $v['types'] = ['yaml'];
269
                                } else {
270 25
                                    $v['types'] = [$v['type']];
271
                                }
272 32
                                unset($v['type']);
273
274 32
                                return $v;
275 42
                            })
276 42
                        ->end()
277 42
                        ->children()
278 42
                            ->arrayNode('types')
279 42
                                ->prototype('enum')->values(array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
280 42
                            ->end()
281 42
                            ->scalarNode('dir')->defaultNull()->end()
282 42
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
283 42
                        ->end()
284 42
                    ->end()
285 42
                ->end()
286 42
            ->end()
287
        ;
288
289 42
        return $node;
290
    }
291
292 42
    private function doctrineSection(): ArrayNodeDefinition
293
    {
294 42
        $builder = new TreeBuilder('doctrine');
295
296
        /** @var ArrayNodeDefinition $node */
297 42
        $node = $builder->getRootNode();
298
299
        // @phpstan-ignore-next-line
300
        $node
301 42
            ->addDefaultsIfNotSet()
302 42
            ->children()
303 42
                ->arrayNode('types_mapping')
304 42
                    ->defaultValue([])
305 42
                    ->prototype('scalar')->end()
306 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

306
                ->/** @scrutinizer ignore-call */ end()
Loading history...
307 42
            ->end()
308
        ;
309
310 42
        return $node;
311
    }
312
313 42
    private function profilerSection(): ArrayNodeDefinition
314
    {
315 42
        $builder = new TreeBuilder('profiler');
316
317
        /** @var ArrayNodeDefinition $node */
318 42
        $node = $builder->getRootNode();
319
320
        // @phpstan-ignore-next-line
321
        $node
322 42
            ->addDefaultsIfNotSet()
323 42
            ->children()
324 42
                ->scalarNode('query_match')->defaultNull()->end()
325 42
            ->end()
326
        ;
327
328 42
        return $node;
329
    }
330
331 42
    private function builderSection(string $name): ArrayNodeDefinition
332
    {
333 42
        $builder = new TreeBuilder($name);
334
335
        /** @var ArrayNodeDefinition $node */
336 42
        $node = $builder->getRootNode();
337
338 42
        $node->beforeNormalization()
339 42
            ->ifTrue(fn ($v) => is_array($v) && !empty($v))
340
            ->then(function ($v) {
341 5
                foreach ($v as $key => &$config) {
342 5
                    if (is_string($config)) {
343
                        $config = [
344 5
                            'alias' => $key,
345 5
                            'class' => $config,
346
                        ];
347
                    }
348
                }
349
350 5
                return $v;
351 42
            })
352 42
        ->end();
353
354
        // @phpstan-ignore-next-line
355 42
        $node->prototype('array')
356 42
            ->children()
357 42
                ->scalarNode('alias')->isRequired()->end()
358 42
                ->scalarNode('class')->isRequired()->end()
359 42
            ->end()
360 42
        ->end()
361
        ;
362
363 42
        return $node;
364
    }
365
366
    /**
367
     * @param mixed $disabledValue
368
     */
369 42
    private function securityQuerySection(string $name, $disabledValue): ScalarNodeDefinition
370
    {
371 42
        $builder = new TreeBuilder($name, 'scalar');
372
373
        /** @var ScalarNodeDefinition $node */
374 42
        $node = $builder->getRootNode();
375
376 42
        $node->beforeNormalization()
377 42
                ->ifTrue(fn ($v) => is_string($v) && is_numeric($v))
378 42
                ->then(fn ($v) => (int) $v)
379 42
            ->end();
380
381
        $node
382 42
            ->info('Disabled if equal to false.')
383 42
            ->beforeNormalization()
384 42
                ->ifTrue(fn ($v) => false === $v)
385 42
                ->then(fn () => $disabledValue)
386 42
            ->end()
387 42
            ->defaultValue($disabledValue)
388 42
            ->validate()
389 42
                ->ifTrue(fn ($v) => is_int($v) && $v < 0)
390 42
                ->thenInvalid(sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
391 42
            ->end()
392
        ;
393
394 42
        return $node;
395
    }
396
}
397