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 (#695)
by Timur
07:48
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
            ->end();
61
62 42
        return $treeBuilder;
63
    }
64
65 42
    private function batchingMethodSection(): EnumNodeDefinition
66
    {
67 42
        $builder = new TreeBuilder('batching_method', 'enum');
68
69
        /** @var EnumNodeDefinition $node */
70 42
        $node = $builder->getRootNode();
71
72
        $node
73 42
            ->values(['relay', 'apollo'])
74 42
            ->defaultValue('relay')
75 42
        ->end();
76
77 42
        return $node;
78
    }
79
80 42
    private function errorsHandlerSection(): ArrayNodeDefinition
81
    {
82 42
        $builder = new TreeBuilder('errors_handler');
83
84
        /** @var ArrayNodeDefinition $node */
85 42
        $node = $builder->getRootNode();
86
87
        // @phpstan-ignore-next-line
88
        $node
89 42
            ->treatFalseLike(['enabled' => false])
90 42
            ->treatTrueLike(['enabled' => true])
91 42
            ->treatNullLike(['enabled' => true])
92 42
            ->addDefaultsIfNotSet()
93 42
            ->children()
94 42
                ->booleanNode('enabled')->defaultTrue()->end()
95 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

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

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

215
            ->/** @scrutinizer ignore-call */ useAttributeAsKey('name')
Loading history...
216 42
            ->prototype('array')
217 42
                ->addDefaultsIfNotSet()
218 42
                ->children()
219 42
                    ->scalarNode('query')->defaultNull()->end()
220 42
                    ->scalarNode('mutation')->defaultNull()->end()
221 42
                    ->scalarNode('subscription')->defaultNull()->end()
222 42
                    ->arrayNode('resolver_maps')
223 42
                        ->defaultValue([])
224 42
                        ->prototype('scalar')->end()
225 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.')
226 42
                    ->end()
227 42
                    ->arrayNode('types')
228 42
                        ->defaultValue([])
229 42
                        ->prototype('scalar')->end()
230 42
                    ->end()
231 42
                ->end()
232 42
            ->end()
233 42
        ->end();
234
235 42
        return $node;
236
    }
237
238 42
    private function definitionsMappingsSection(): ArrayNodeDefinition
239
    {
240 42
        $builder = new TreeBuilder('mappings');
241
242
        /** @var ArrayNodeDefinition $node */
243 42
        $node = $builder->getRootNode();
244
245
        // @phpstan-ignore-next-line
246
        $node
247 42
            ->children()
248 42
                ->arrayNode('auto_discover')
249 42
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
250 42
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
251 42
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
252 42
                    ->addDefaultsIfNotSet()
253 42
                    ->children()
254 42
                        ->booleanNode('bundles')->defaultFalse()->end()
255 42
                        ->booleanNode('root_dir')->defaultFalse()->end()
256 42
                    ->end()
257 42
                ->end()
258 42
                ->arrayNode('types')
259 42
                    ->prototype('array')
260 42
                        ->addDefaultsIfNotSet()
261 42
                        ->beforeNormalization()
262
                            ->ifTrue(function ($v) {
263 33
                                return isset($v['type']) && is_string($v['type']);
264 42
                            })
265
                            ->then(function ($v) {
266 32
                                if ('yml' === $v['type']) {
267 7
                                    $v['types'] = ['yaml'];
268
                                } else {
269 25
                                    $v['types'] = [$v['type']];
270
                                }
271 32
                                unset($v['type']);
272
273 32
                                return $v;
274 42
                            })
275 42
                        ->end()
276 42
                        ->children()
277 42
                            ->arrayNode('types')
278 42
                                ->prototype('enum')->values(array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
279 42
                            ->end()
280 42
                            ->scalarNode('dir')->defaultNull()->end()
281 42
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
282 42
                        ->end()
283 42
                    ->end()
284 42
                ->end()
285 42
            ->end()
286
        ;
287
288 42
        return $node;
289
    }
290
291 42
    private function doctrineSection(): ArrayNodeDefinition
292
    {
293 42
        $builder = new TreeBuilder('doctrine');
294
295
        /** @var ArrayNodeDefinition $node */
296 42
        $node = $builder->getRootNode();
297
298
        // @phpstan-ignore-next-line
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 42
    private function builderSection(string $name): ArrayNodeDefinition
313
    {
314 42
        $builder = new TreeBuilder($name);
315
316
        /** @var ArrayNodeDefinition $node */
317 42
        $node = $builder->getRootNode();
318
319 42
        $node->beforeNormalization()
320 42
            ->ifTrue(fn ($v) => is_array($v) && !empty($v))
321
            ->then(function ($v) {
322 5
                foreach ($v as $key => &$config) {
323 5
                    if (is_string($config)) {
324
                        $config = [
325 5
                            'alias' => $key,
326 5
                            'class' => $config,
327
                        ];
328
                    }
329
                }
330
331 5
                return $v;
332 42
            })
333 42
        ->end();
334
335
        // @phpstan-ignore-next-line
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 mixed $disabledValue
349
     */
350 42
    private function securityQuerySection(string $name, $disabledValue): ScalarNodeDefinition
351
    {
352 42
        $builder = new TreeBuilder($name, 'scalar');
353
354
        /** @var ScalarNodeDefinition $node */
355 42
        $node = $builder->getRootNode();
356
357 42
        $node->beforeNormalization()
358 42
                ->ifTrue(fn ($v) => is_string($v) && is_numeric($v))
359 42
                ->then(fn ($v) => (int) $v)
360 42
            ->end();
361
362
        $node
363 42
            ->info('Disabled if equal to false.')
364 42
            ->beforeNormalization()
365 42
                ->ifTrue(fn ($v) => false === $v)
366 42
                ->then(fn () => $disabledValue)
367 42
            ->end()
368 42
            ->defaultValue($disabledValue)
369 42
            ->validate()
370 42
                ->ifTrue(fn ($v) => is_int($v) && $v < 0)
371 42
                ->thenInvalid(sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
372 42
            ->end()
373
        ;
374
375 42
        return $node;
376
    }
377
}
378