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 (#742)
by Vincent
08:42
created

Configuration::getDeprecationArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 9
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
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 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 38
     */
40
    public function __construct(bool $debug, string $cacheDir = null)
41 38
    {
42 38
        $this->debug = (bool) $debug;
43 38
        $this->cacheDir = $cacheDir;
44
    }
45 38
46
    public function getConfigTreeBuilder()
47 38
    {
48
        $treeBuilder = new TreeBuilder(self::NAME);
49 38
50
        $rootNode = self::getRootNodeWithoutDeprecation($treeBuilder, self::NAME);
51
52 38
        $rootNode
53 38
            ->children()
54 38
                ->append($this->batchingMethodSection())
55 38
                ->append($this->definitionsSection())
56 38
                ->append($this->errorsHandlerSection())
57 38
                ->append($this->servicesSection())
58 38
                ->append($this->securitySection())
59 38
                ->append($this->doctrineSection())
60
            ->end();
61 38
62
        return $treeBuilder;
63
    }
64 38
65
    private function batchingMethodSection()
66 38
    {
67
        $builder = new TreeBuilder('batching_method', 'enum');
68 38
        /** @var EnumNodeDefinition $node */
69
        $node = self::getRootNodeWithoutDeprecation($builder, 'batching_method', 'enum');
70
71 38
        $node
72 38
            ->values(['relay', 'apollo'])
73 38
            ->defaultValue('relay')
74
        ->end();
75 38
76
        return $node;
77
    }
78 38
79
    private function errorsHandlerSection()
80 38
    {
81
        $builder = new TreeBuilder('errors_handler');
82 38
        /** @var ArrayNodeDefinition $node */
83
        $node = self::getRootNodeWithoutDeprecation($builder, 'errors_handler');
84 38
        $node
85 38
            ->treatFalseLike(['enabled' => false])
86 38
            ->treatTrueLike(['enabled' => true])
87 38
            ->treatNullLike(['enabled' => true])
88 38
            ->addDefaultsIfNotSet()
89 38
            ->children()
90 38
                ->booleanNode('enabled')->defaultTrue()->end()
91 38
                ->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 38
                ->booleanNode('rethrow_internal_exceptions')->defaultFalse()->end()
93 38
                ->booleanNode('debug')->defaultValue($this->debug)->end()
94 38
                ->booleanNode('log')->defaultTrue()->end()
95 38
                ->scalarNode('logger_service')->defaultValue(ErrorLoggerListener::DEFAULT_LOGGER_SERVICE)->end()
96 38
                ->booleanNode('map_exceptions_to_parent')->defaultFalse()->end()
97 38
                ->arrayNode('exceptions')
98 38
                    ->addDefaultsIfNotSet()
99 38
                    ->children()
100 38
                        ->arrayNode('warnings')
101 38
                            ->treatNullLike([])
102 38
                            ->prototype('scalar')->end()
103 38
                        ->end()
104 38
                        ->arrayNode('errors')
105 38
                            ->treatNullLike([])
106 38
                            ->prototype('scalar')->end()
107 38
                    ->end()
108 38
                ->end()
109
            ->end();
110 38
111
        return $node;
112
    }
113 38
114
    private function definitionsSection()
115 38
    {
116
        $builder = new TreeBuilder('definitions');
117 38
        /** @var ArrayNodeDefinition $node */
118
        $node = self::getRootNodeWithoutDeprecation($builder, 'definitions');
119 38
        $node
120 38
            ->addDefaultsIfNotSet()
121 38
            ->children()
122 38
                ->scalarNode('argument_class')->defaultValue(Argument::class)->end()
123 38
                ->scalarNode('use_experimental_executor')->defaultFalse()->end()
124 38
                ->scalarNode('default_field_resolver')->defaultValue(FieldResolver::class)->end()
125 38
                ->scalarNode('class_namespace')->defaultValue('Overblog\\GraphQLBundle\\__DEFINITIONS__')->end()
126 38
                ->scalarNode('cache_dir')->defaultNull()->end()
127 38
                ->scalarNode('cache_dir_permissions')->defaultNull()->end()
128 38
                ->booleanNode('use_classloader_listener')->defaultTrue()->end()
129 38
                ->scalarNode('auto_compile')->defaultTrue()->end()
130 38
                ->booleanNode('show_debug_info')->info('Show some performance stats in extensions')->defaultFalse()->end()
131 38
                ->booleanNode('config_validation')->defaultValue($this->debug)->end()
132 38
                ->append($this->definitionsSchemaSection())
133 38
                ->append($this->definitionsMappingsSection())
134 38
                ->arrayNode('builders')
135 38
                    ->children()
136 38
                        ->append($this->builderSection('field'))
137 38
                        ->append($this->builderSection('fields'))
138 38
                        ->append($this->builderSection('args'))
139 38
                    ->end()
140
                ->end()
141 38
142 38
            ->end()
143
        ->end();
144 38
145
        return $node;
146
    }
147 38
148
    private function servicesSection()
149 38
    {
150
        $builder = new TreeBuilder('services');
151 38
        /** @var ArrayNodeDefinition $node */
152
        $node = self::getRootNodeWithoutDeprecation($builder, 'services');
153 38
        $node
154 38
            ->addDefaultsIfNotSet()
155 38
            ->children()
156 38
                ->scalarNode('executor')
157 38
                    ->defaultValue(Executor::class)
158 38
                ->end()
159 38
                ->scalarNode('promise_adapter')
160 38
                    ->defaultValue(SyncPromiseAdapter::class)
161 38
                ->end()
162 38
                ->scalarNode('expression_language')
163 38
                    ->defaultValue(ExpressionLanguage::class)
164 38
                ->end()
165 38
                ->scalarNode('cache_expression_language_parser')->end()
166 38
            ->end()
167
        ->end();
168 38
169
        return $node;
170
    }
171 38
172
    private function securitySection()
173 38
    {
174
        $builder = new TreeBuilder('security');
175 38
        /** @var ArrayNodeDefinition $node */
176
        $node = self::getRootNodeWithoutDeprecation($builder, 'security');
177 38
        $node
178 38
            ->addDefaultsIfNotSet()
179 38
            ->children()
180 38
                ->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 38
                ->append($this->securityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
182 38
                ->booleanNode('enable_introspection')->defaultTrue()->end()
183 38
                ->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 38
            ->end()
185
        ->end();
186 38
187
        return $node;
188
    }
189 38
190
    private function definitionsSchemaSection()
191 38
    {
192
        $builder = new TreeBuilder('schema');
193 38
        /** @var ArrayNodeDefinition $node */
194
        $node = self::getRootNodeWithoutDeprecation($builder, 'schema');
195 38
        $node
196
            ->beforeNormalization()
197 30
                ->ifTrue(function ($v) {
198 38
                    return isset($v['query']) && \is_string($v['query']) || isset($v['mutation']) && \is_string($v['mutation']);
199
                })
200 28
                ->then(function ($v) {
201 38
                    return ['default' => $v];
202 38
                })
203 38
            ->end()
204 38
            ->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 38
            ->prototype('array')
206 38
                ->addDefaultsIfNotSet()
207 38
                ->children()
208 38
                    ->scalarNode('query')->defaultNull()->end()
209 38
                    ->scalarNode('mutation')->defaultNull()->end()
210 38
                    ->scalarNode('subscription')->defaultNull()->end()
211 38
                    ->arrayNode('resolver_maps')
212 38
                        ->defaultValue([])
213 38
                        ->prototype('scalar')->end()
214 38
                        ->setDeprecated(...$this->getDeprecationArgs())
215 38
                    ->end()
216 38
                    ->arrayNode('types')
217 38
                        ->defaultValue([])
218 38
                        ->prototype('scalar')->end()
219 38
                    ->end()
220 38
                ->end()
221
            ->end()
222 38
        ->end();
223
224
        return $node;
225 38
    }
226
227 38
    /**
228 38
     * BC layer for symfony/config <5.1 .
229
     */
230 38
    private function getDeprecationArgs()
231 38
    {
232 38
        $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 38
234 38
        if (Kernel::VERSION_ID < 50100) {
235 38
            return [$msg];
236 38
        }
237 38
238 38
        return ['overblog/graphql-bundle', '0.13', $msg];
239 38
    }
240 38
241 38
    private function definitionsMappingsSection()
242 38
    {
243 38
        $builder = new TreeBuilder('mappings');
244 38
        $node = self::getRootNodeWithoutDeprecation($builder, 'mappings');
245
        $node
246 29
            ->children()
247 38
                ->arrayNode('auto_discover')
248
                    ->treatFalseLike(['bundles' => false, 'root_dir' => false])
249 28
                    ->treatTrueLike(['bundles' => true, 'root_dir' => true])
250 7
                    ->treatNullLike(['bundles' => true, 'root_dir' => true])
251
                    ->addDefaultsIfNotSet()
252 21
                    ->children()
253
                        ->booleanNode('bundles')->defaultFalse()->end()
254 28
                        ->booleanNode('root_dir')->defaultFalse()->end()
255
                    ->end()
256 28
                ->end()
257 38
                ->arrayNode('types')
258 38
                    ->prototype('array')
259 38
                        ->addDefaultsIfNotSet()
260 38
                        ->beforeNormalization()
261 38
                            ->ifTrue(function ($v) {
262 38
                                return isset($v['type']) && \is_string($v['type']);
263 38
                            })
264 38
                            ->then(function ($v) {
265 38
                                if ('yml' === $v['type']) {
266 38
                                    $v['types'] = ['yaml'];
267 38
                                } else {
268 38
                                    $v['types'] = [$v['type']];
269
                                }
270
                                unset($v['type']);
271 38
272
                                return $v;
273
                            })
274 38
                        ->end()
275
                        ->children()
276 38
                            ->arrayNode('types')
277
                                ->prototype('enum')->values(\array_keys(ConfigParserPass::SUPPORTED_TYPES_EXTENSIONS))->isRequired()->end()
278 38
                            ->end()
279
                            ->scalarNode('dir')->defaultNull()->end()
280 38
                            ->scalarNode('suffix')->defaultValue(ConfigParserPass::DEFAULT_TYPES_SUFFIX)->end()
281 38
                        ->end()
282 38
                    ->end()
283 38
                ->end()
284 38
            ->end()
285 38
        ;
286 38
287
        return $node;
288
    }
289 38
290
    private function doctrineSection()
291
    {
292
        $builder = new TreeBuilder('doctrine');
293
        /** @var ArrayNodeDefinition $node */
294
        $node = self::getRootNodeWithoutDeprecation($builder, 'doctrine');
295
        $node
296
            ->addDefaultsIfNotSet()
297 38
            ->children()
298
                ->arrayNode('types_mapping')
299 38
                    ->defaultValue([])
300
                    ->prototype('scalar')->end()
301 38
                ->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 38
            ->end()
303
        ;
304 5
305 38
        return $node;
306
    }
307 5
308 5
    /**
309
     * @param string $name
310 5
     *
311 5
     * @return ArrayNodeDefinition
312
     */
313
    private function builderSection($name)
314
    {
315
        $builder = new TreeBuilder($name);
316 5
        /** @var ArrayNodeDefinition $node */
317 38
        $node = self::getRootNodeWithoutDeprecation($builder, $name);
318 38
        $node->beforeNormalization()
319
            ->ifTrue(function ($v) {
320 38
                return \is_array($v) && !empty($v);
321 38
            })
322 38
            ->then(function ($v) {
323 38
                foreach ($v as $key => &$config) {
324 38
                    if (\is_string($config)) {
325 38
                        $config = [
326
                            'alias' => $key,
327
                            'class' => $config,
328 38
                        ];
329
                    }
330
                }
331
332
                return $v;
333
            })
334
        ->end();
335
336
        $node->prototype('array')
337 38
            ->children()
338
                ->scalarNode('alias')->isRequired()->end()
339 38
                ->scalarNode('class')->isRequired()->end()
340
            ->end()
341 38
        ->end()
342 38
        ;
343
344 32
        return $node;
345 38
    }
346
347 2
    /**
348 38
     * @param string $name
349 38
     * @param bool   $disabledValue
350
     *
351
     * @return ScalarNodeDefinition
352 38
     */
353 38
    private function securityQuerySection($name, $disabledValue)
354
    {
355 32
        $builder = new TreeBuilder($name, 'scalar');
356 38
        /** @var ScalarNodeDefinition $node */
357
        $node = self::getRootNodeWithoutDeprecation($builder, $name, 'scalar');
358 32
        $node->beforeNormalization()
359 38
                ->ifTrue(function ($v) {
360 38
                    return \is_string($v) && \is_numeric($v);
361 38
                })
362 38
                ->then(function ($v) {
363
                    return (int) $v;
364 32
                })
365 38
            ->end();
366 38
367 38
        $node
368
            ->info('Disabled if equal to false.')
369
            ->beforeNormalization()
370 38
                ->ifTrue(function ($v) {
371
                    return false === $v;
372
                })
373
                ->then(function () use ($disabledValue) {
374
                    return $disabledValue;
375
                })
376
            ->end()
377
            ->defaultValue($disabledValue)
378
            ->validate()
379
                ->ifTrue(function ($v) {
380
                    return \is_int($v) && $v < 0;
381
                })
382 44
                ->thenInvalid(\sprintf('"%s.security.%s" must be greater or equal to 0.', self::NAME, $name))
383
            ->end()
384
        ;
385 44
386
        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
    public static function getRootNodeWithoutDeprecation(TreeBuilder $builder, string $name, string $type = 'array')
399
    {
400
        // BC layer for symfony/config 4.1 and older
401
        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