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
25:41
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 42
        $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
            ->end();
62 42
63
        return $treeBuilder;
64
    }
65 42
66
    private function batchingMethodSection(): EnumNodeDefinition
67 42
    {
68
        $builder = new TreeBuilder('batching_method', 'enum');
69 42
70
        /** @var EnumNodeDefinition $node */
71
        $node = $builder->getRootNode();
72 42
73 42
        $node
74 42
            ->values(['relay', 'apollo'])
75
            ->defaultValue('relay')
76 42
        ->end();
77
78
        return $node;
79 42
    }
80
81 42
    private function errorsHandlerSection(): ArrayNodeDefinition
82
    {
83 42
        $builder = new TreeBuilder('errors_handler');
84
85 42
        /** @var ArrayNodeDefinition $node */
86 42
        $node = $builder->getRootNode();
87 42
88 42
        // @phpstan-ignore-next-line
89 42
        $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
                            ->treatNullLike([])
111 42
                            ->prototype('scalar')->end()
112
                    ->end()
113
                ->end()
114 42
            ->end();
115
116 42
        return $node;
117
    }
118 42
119
    private function definitionsSection(): ArrayNodeDefinition
120 42
    {
121 42
        $builder = new TreeBuilder('definitions');
122 42
        /** @var ArrayNodeDefinition $node */
123 42
        $node = $builder->getRootNode();
124 42
125 42
        // @phpstan-ignore-next-line
126 42
        $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
                ->arrayNode('builders')
142 42
                    ->children()
143 42
                        ->append($this->builderSection('field'))
144
                        ->append($this->builderSection('fields'))
145 42
                        ->append($this->builderSection('args'))
146
                    ->end()
147
                ->end()
148 42
149
            ->end()
150 42
        ->end();
151
152 42
        return $node;
153
    }
154 42
155 42
    private function servicesSection(): ArrayNodeDefinition
156 42
    {
157 42
        $builder = new TreeBuilder('services');
158 42
159 42
        /** @var ArrayNodeDefinition $node */
160 42
        $node = $builder->getRootNode();
161 42
162 42
        // @phpstan-ignore-next-line
163 42
        $node
164 42
            ->addDefaultsIfNotSet()
165 42
            ->children()
166 42
                ->scalarNode('executor')
167 42
                    ->defaultValue(Executor::class)
168
                ->end()
169 42
                ->scalarNode('promise_adapter')
170
                    ->defaultValue(SyncPromiseAdapter::class)
171
                ->end()
172 42
                ->scalarNode('expression_language')
173
                    ->defaultValue(ExpressionLanguage::class)
174 42
                ->end()
175
                ->scalarNode('cache_expression_language_parser')->end()
176 42
            ->end()
177
        ->end();
178 42
179 42
        return $node;
180 42
    }
181 42
182 42
    private function securitySection(): ArrayNodeDefinition
183 42
    {
184 42
        $builder = new TreeBuilder('security');
185 42
186
        /** @var ArrayNodeDefinition $node */
187 42
        $node = $builder->getRootNode();
188
189
        // @phpstan-ignore-next-line
190 42
        $node
191
            ->addDefaultsIfNotSet()
192 42
            ->children()
193
                ->append($this->securityQuerySection('query_max_depth', QueryDepth::DISABLED))
194 42
                ->append($this->securityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
195
                ->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
            ->end()
198 34
        ->end();
199 42
200
        return $node;
201 32
    }
202 42
203 42
    private function definitionsSchemaSection(): ArrayNodeDefinition
204 42
    {
205 42
        $builder = new TreeBuilder('schema');
206 42
207 42
        /** @var ArrayNodeDefinition $node */
208 42
        $node = $builder->getRootNode();
209 42
210 42
        // @phpstan-ignore-next-line
211 42
        $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
                    ->arrayNode('resolver_maps')
224 42
                        ->defaultValue([])
225
                        ->prototype('scalar')->end()
226
                        ->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.', '0.13')
227 42
                    ->end()
228
                    ->arrayNode('types')
229 42
                        ->defaultValue([])
230 42
                        ->prototype('scalar')->end()
231
                    ->end()
232 42
                ->end()
233 42
            ->end()
234 42
        ->end();
235 42
236 42
        return $node;
237 42
    }
238 42
239 42
    private function definitionsMappingsSection(): ArrayNodeDefinition
240 42
    {
241 42
        $builder = new TreeBuilder('mappings');
242 42
243 42
        /** @var ArrayNodeDefinition $node */
244 42
        $node = $builder->getRootNode();
245 42
246 42
        // @phpstan-ignore-next-line
247
        $node
248 33
            ->children()
249 42
                ->arrayNode('auto_discover')
250
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
251 32
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
252 7
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
253
                    ->addDefaultsIfNotSet()
254 25
                    ->children()
255
                        ->booleanNode('bundles')->defaultFalse()->end()
256 32
                        ->booleanNode('root_dir')->defaultFalse()->end()
257
                    ->end()
258 32
                ->end()
259 42
                ->arrayNode('types')
260 42
                    ->prototype('array')
261 42
                        ->addDefaultsIfNotSet()
262 42
                        ->beforeNormalization()
263 42
                            ->ifTrue(function ($v) {
264 42
                                return isset($v['type']) && is_string($v['type']);
265 42
                            })
266 42
                            ->then(function ($v) {
267 42
                                if ('yml' === $v['type']) {
268 42
                                    $v['types'] = ['yaml'];
269 42
                                } else {
270 42
                                    $v['types'] = [$v['type']];
271
                                }
272
                                unset($v['type']);
273 42
274
                                return $v;
275
                            })
276 42
                        ->end()
277
                        ->children()
278 42
                            ->arrayNode('types')
279
                                ->prototype('enum')->values(array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
280 42
                            ->end()
281
                            ->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 42
        ;
288 42
289
        return $node;
290
    }
291 42
292
    private function doctrineSection(): ArrayNodeDefinition
293
    {
294 42
        $builder = new TreeBuilder('doctrine');
295
296 42
        /** @var ArrayNodeDefinition $node */
297
        $node = $builder->getRootNode();
298 42
299
        // @phpstan-ignore-next-line
300 42
        $node
301 42
            ->addDefaultsIfNotSet()
302 42
            ->children()
303 42
                ->arrayNode('types_mapping')
304
                    ->defaultValue([])
305
                    ->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
            ->end()
308
        ;
309
310
        return $node;
311
    }
312
313
    private function profilerSection(): ArrayNodeDefinition
314 42
    {
315
        $builder = new TreeBuilder('profiler');
316 42
317
        /** @var ArrayNodeDefinition $node */
318 42
        $node = $builder->getRootNode();
319 42
320
        // @phpstan-ignore-next-line
321 5
        $node
322 42
            ->addDefaultsIfNotSet()
323
            ->children()
324 5
                ->scalarNode('query_match')->defaultNull()->end()
325 5
            ->end()
326
        ;
327 5
328 5
        return $node;
329
    }
330
331
    private function builderSection(string $name): ArrayNodeDefinition
332
    {
333 5
        $builder = new TreeBuilder($name);
334 42
335 42
        /** @var ArrayNodeDefinition $node */
336
        $node = $builder->getRootNode();
337 42
338 42
        $node->beforeNormalization()
339 42
            ->ifTrue(fn ($v) => is_array($v) && !empty($v))
340 42
            ->then(function ($v) {
341 42
                foreach ($v as $key => &$config) {
342 42
                    if (is_string($config)) {
343
                        $config = [
344
                            'alias' => $key,
345 42
                            'class' => $config,
346
                        ];
347
                    }
348
                }
349
350
                return $v;
351
            })
352
        ->end();
353
354 42
        // @phpstan-ignore-next-line
355
        $node->prototype('array')
356 42
            ->children()
357
                ->scalarNode('alias')->isRequired()->end()
358 42
                ->scalarNode('class')->isRequired()->end()
359 42
            ->end()
360
        ->end()
361 36
        ;
362 42
363
        return $node;
364 2
    }
365 42
366 42
    /**
367
     * @param mixed $disabledValue
368
     */
369 42
    private function securityQuerySection(string $name, $disabledValue): ScalarNodeDefinition
370 42
    {
371
        $builder = new TreeBuilder($name, 'scalar');
372 36
373 42
        /** @var ScalarNodeDefinition $node */
374
        $node = $builder->getRootNode();
375 36
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 36
        $node
382 42
            ->info('Disabled if equal to false.')
383 42
            ->beforeNormalization()
384 42
                ->ifTrue(fn ($v) => false === $v)
385
                ->then(fn () => $disabledValue)
386
            ->end()
387 42
            ->defaultValue($disabledValue)
388
            ->validate()
389
                ->ifTrue(fn ($v) => is_int($v) && $v < 0)
390
                ->thenInvalid(sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
391
            ->end()
392
        ;
393
394
        return $node;
395 48
    }
396
}
397