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 (#750)
by Timur
22:37
created

TypesConfiguration::getConfigTreeBuilder()   B

Complexity

Conditions 8
Paths 1

Size

Total Lines 96
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 68
c 0
b 0
f 0
dl 0
loc 96
rs 7.4537
ccs 67
cts 67
cp 1
cc 8
nc 1
nop 0
crap 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\DependencyInjection;
6
7
use Overblog\GraphQLBundle\Config;
8
use Overblog\GraphQLBundle\Config\Processor\InheritanceProcessor;
9
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
10
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
11
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12
use Symfony\Component\Config\Definition\ConfigurationInterface;
13
use function array_keys;
14
use function array_map;
15
use function implode;
16
use function in_array;
17
use function is_array;
18
use function is_string;
19
use function preg_match;
20
use function sprintf;
21
use function str_replace;
22
23
class TypesConfiguration implements ConfigurationInterface
24
{
25
    private static array $types = [
26
        'object',
27
        'enum',
28
        'interface',
29
        'union',
30
        'input-object',
31
        'custom-scalar',
32
    ];
33 47
34
    public function getConfigTreeBuilder()
35 47
    {
36
        $treeBuilder = new TreeBuilder('overblog_graphql_types');
37
38 47
        /** @var ArrayNodeDefinition $rootNode */
39
        $rootNode = $treeBuilder->getRootNode();
40 47
41
        $configTypeKeys = array_map(fn ($type) => $this->normalizedConfigTypeKey($type), self::$types);
42 47
43
        $this->addBeforeNormalization($rootNode);
44
45
        // @phpstan-ignore-next-line
46 47
        $rootNode
47 47
            ->useAttributeAsKey('name')
48
            ->prototype('array')
49 47
                # config is the unique config entry allowed
50 47
                ->beforeNormalization()
51 43
                    ->ifTrue(function ($v) use ($configTypeKeys) {
52 43
                        if (!empty($v) && is_array($v)) {
53 43
                            $keys = array_keys($v);
54 43
                            foreach ($configTypeKeys as $configTypeKey) {
55 5
                                if (in_array($configTypeKey, $keys)) {
56
                                    return true;
57
                                }
58
                            }
59
                        }
60 38
61 47
                        return  false;
62 47
                    })
63 47
                        ->thenInvalid(
64
                            sprintf(
65 47
                                'Don\'t use internal config keys %s, replace it by "config" instead.',
66
                                implode(', ', $configTypeKeys)
67
                            )
68 47
                        )
69
                ->end()
70 47
                # temporarily rename 'config' into '_{TYPE}_config'
71 47
                ->beforeNormalization()
72 47
                    ->ifTrue(fn ($v) => isset($v['type']) && is_string($v['type']))
73 38
                    ->then(function ($v) {
74
                        $key = $this->normalizedConfigTypeKey($v['type']);
75 38
76 38
                        if (empty($v[$key])) {
77
                            $v[$key] = $v['config'] ?? [];
78 38
                        }
79
                        unset($v['config']);
80 38
81 47
                        return $v;
82 47
                    })
83 47
                ->end()
84 47
                # temporarily convert '{MODEL}' into '{TYPE}_{MODEL}'
85 47
                ->beforeNormalization()
86 47
                    ->ifTrue(fn($v) => isset($v['model']) && is_string($v['model']))
87 47
                    ->then(function($v) {
88 47
                        $v['model'] = "{$v['type']}_{$v['model']}";
89 47
                        return $v;
90 47
                    })
91 47
                ->end()
92 47
                ->cannotBeOverwritten()
93 47
                ->children()
94 47
                    ->scalarNode('class_name')
95 47
                        ->isRequired()
96 47
                        ->validate()
97 47
                            ->ifTrue(fn ($name) => !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name))
98 47
                            ->thenInvalid('A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.')
99 47
                        ->end()
100 47
                    ->end()
101 47
                    ->enumNode('type')->values(self::$types)->isRequired()->end()
102 47
                    ->arrayNode(InheritanceProcessor::INHERITS_KEY)
103 47
                        ->prototype('scalar')->info('Types to inherit of.')->end()
104 47
                    ->end()
105
                    ->booleanNode('decorator')->info('Decorator will not be generated.')->defaultFalse()->end()
106 47
                    ->append(Config\ObjectTypeDefinition::create()->getDefinition())
107 47
                    ->append(Config\EnumTypeDefinition::create()->getDefinition())
108 47
                    ->append(Config\InterfaceTypeDefinition::create()->getDefinition())
109 37
                    ->append(Config\UnionTypeDefinition::create()->getDefinition())
110 37
                    ->append(Config\InputObjectTypeDefinition::create()->getDefinition())
111 37
                    ->append(Config\CustomScalarTypeDefinition::create()->getDefinition())
112
                    ->variableNode('config')->end()
113 37
                    ->append($this->modelSection())
114 47
                ->end()
115 47
                // rename '_{TYPE}_config' back into 'config'
116
                ->validate()
117 47
                    ->ifTrue(fn ($v) => isset($v[$this->normalizedConfigTypeKey($v['type'])]))
118
                    ->then(function ($v) {
119 47
                        $key = $this->normalizedConfigTypeKey($v['type']);
120
                        $v['config'] = $v[$key];
121
                        unset($v[$key]);
122 47
123
                        return $v;
124
                    })
125
                ->end()
126 47
127 47
            ->end();
128 47
129 47
        return $treeBuilder;
130
    }
131 47
132
    private function modelSection()
133 47
    {
134
        $node = new ScalarNodeDefinition('model');
135 47
136
        return $node
137
            ->validate()
138 6
                ->ifTrue(fn($v) => substr($v, 0, 12) !== "input-object")
139
                ->thenInvalid("The 'model' option can be defined only for 'input-object' types.")
140
            ->end()
141
            ->validate()
142
                ->always(fn($v) => ltrim($v, 'input-object_'))
143
            ->end()
144
            ->validate()
145
                ->ifTrue(fn($v) => !class_exists($v))
146
                ->thenInvalid("Model class %s doesn't exist")
147
            ->end()
148
        ;
149
    }
150
151
    private function addBeforeNormalization(ArrayNodeDefinition $node): void
152
    {
153
        $node
154
            // process beforeNormalization (should be execute after relay normalization)
155
            ->beforeNormalization()
156
                ->ifTrue(fn ($types) => is_array($types))
157
                ->then(fn ($types) => Config\Processor::process($types, Config\Processor::BEFORE_NORMALIZATION))
158
            ->end()
159
            ;
160
    }
161
162
    private function normalizedConfigTypeKey(string $type): string
163
    {
164
        return '_'.str_replace('-', '_', $type).'_config';
165
    }
166
}
167