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 (#536)
by
unknown
30:49 queued 05:45
created

TypeDefinition::validationSection()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 65
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 65
ccs 10
cts 10
cp 1
rs 7.9715
c 0
b 0
f 0
cc 8
nc 2
nop 1
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\Config;
6
7
use Overblog\GraphQLBundle\DependencyInjection\Configuration;
8
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
9
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
10
use Symfony\Component\Config\Definition\Builder\NodeParentInterface;
11
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
12
13
abstract class TypeDefinition
14 42
{
15
    public const VALIDATION_LEVEL_CLASS = 0;
16 42
    public const VALIDATION_LEVEL_PROPERTY = 1;
17
18
    abstract public function getDefinition();
19
20
    protected function __construct()
21 42
    {
22
    }
23 42
24
    /**
25
     * @return static
26 42
     */
27
    public static function create()
28 42
    {
29
        return new static();
30 42
    }
31
32
    protected function resolveTypeSection()
33 42
    {
34
        $node = self::createNode('resolveType', 'variable');
35 42
36 42
        return $node;
37 42
    }
38
39 33
    protected function nameSection()
40 42
    {
41 42
        $node = self::createNode('name', 'scalar');
42 42
        $node->isRequired();
43
        $node->validate()
44 42
            ->ifTrue(function ($name) {
45
                return !\preg_match('/^[_a-z][_0-9a-z]*$/i', $name);
46
            })
47 42
                ->thenInvalid('Invalid type name "%s". (see https://facebook.github.io/graphql/October2016/#Name)')
48
        ->end();
49 42
50
        return $node;
51 42
    }
52
53
    protected function defaultValueSection()
54 42
    {
55
        $node = self::createNode('defaultValue', 'variable');
56 42
57
        return $node;
58 42
    }
59
60
    /**
61 42
     * @param int $level
62
     *
63 42
     * @return ArrayNodeDefinition|NodeDefinition
64
     */
65 42
    protected function validationSection(int $level): NodeParentInterface
66
    {
67 42
        $node = self::createNode('validation', 'array');
68
69
        $node
70 42
            // allow shorthands
71
            ->beforeNormalization()
72 42
                ->always(function ($value) {
73
                    if (\is_string($value)) {
74 42
                        // cascade or link
75
                        return 'cascade' === $value ? ['cascade' => null] : ['link' => $value];
76 42
                    }
77 42
78
                    if (\is_array($value)) {
79
                        foreach ($value as $k => $a) {
80 42
                            if (!\is_int($k)) {
81
                                // validation: { link: ... , constraints: ..., cascade: ... }
82
                                return $value;
83
                            }
84
                        }
85
                        // validation: [<validation constraints>]
86
                        return ['constraints' => $value];
87
                    }
88
89
                    return [];
90
                })
91 42
            ->end()
92
            ->children()
93 42
                ->scalarNode('link')
94
                    ->defaultNull()
95
                    ->validate()
96
                        ->ifTrue(function ($link) use ($level) {
97
                            if (self::VALIDATION_LEVEL_PROPERTY === $level) {
98
                                return !\preg_match('/^(?:\\\\?[A-Za-z][A-Za-z\d]+)*[A-Za-z\d]+::(?:[$]?[A-Za-z][A-Za-z_\d]+|[A-Za-z_\d]+\(\))$/m', $link);
99
                            } else {
100
                                return !\preg_match('/^(?:\\\\?[A-Za-z][A-Za-z\d]+)*[A-Za-z\d]$/m', $link);
101
                            }
102
                        })
103
                        ->thenInvalid('Invalid link provided: "%s".')
104
                    ->end()
105
                ->end()
106
107
                ->variableNode('constraints')
108
                    ->defaultNull()
109
                ->end()
110
            ->end();
111
112
        // Add the 'cascade' option if it's a property level validation section
113
        if (self::VALIDATION_LEVEL_PROPERTY === $level) {
114
            $node
115
                ->children()
116
                    ->arrayNode('cascade')
117
                        ->children()
118
                            ->arrayNode('groups')
119
                                ->beforeNormalization()
120
                                    ->castToArray()
121
                                ->end()
122
                                ->scalarPrototype()->end()
123
                            ->end()
124
                        ->end()
125
                    ->end()
126
                ->end();
127
        }
128
129
        return $node;
130
    }
131
132
    protected function descriptionSection()
133
    {
134
        $node = self::createNode('description', 'scalar');
135
136
        return $node;
137
    }
138
139
    protected function deprecationReasonSelection()
140
    {
141
        $node = self::createNode('deprecationReason', 'scalar');
142
143
        $node->info('Text describing why this field is deprecated. When not empty - field will not be returned by introspection queries (unless forced)');
144
145
        return $node;
146
    }
147
148
    protected function typeSelection($isRequired = false)
149
    {
150
        $node = self::createNode('type', 'scalar');
151
152
        $node->info('One of internal or custom types.');
153
154
        if ($isRequired) {
155
            $node->isRequired();
156
        }
157
158
        return $node;
159
    }
160
161
    /**
162
     * @param string $name
163
     * @param string $type
164
     *
165
     * @return ArrayNodeDefinition|NodeDefinition
166
     *
167
     *@internal
168
     */
169
    protected static function createNode(string $name, string $type = 'array')
170
    {
171
        return Configuration::getRootNodeWithoutDeprecation(new TreeBuilder($name, $type), $name, $type);
172
    }
173
}
174