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

Passed
Pull Request — master (#536)
by
unknown
21:29
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 function preg_match;
9
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
10
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
11
use Symfony\Component\Config\Definition\Builder\NodeParentInterface;
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
14 42
abstract class TypeDefinition
15
{
16 42
    public const VALIDATION_LEVEL_CLASS    = 0;
17
    public const VALIDATION_LEVEL_PROPERTY = 1;
18
19
    abstract public function getDefinition();
20
21 42
    protected function __construct()
22
    {
23 42
    }
24
25
    /**
26 42
     * @return static
27
     */
28 42
    public static function create()
29
    {
30 42
        return new static();
31
    }
32
33 42
    protected function resolveTypeSection()
34
    {
35 42
        $node = self::createNode('resolveType', 'variable');
36 42
37 42
        return $node;
38
    }
39 33
40 42
    protected function nameSection()
41 42
    {
42 42
        $node = self::createNode('name', 'scalar');
43
        $node->isRequired();
44 42
        $node->validate()
45
            ->ifTrue(function ($name) {
46
                return !preg_match('/^[_a-z][_0-9a-z]*$/i', $name);
47 42
            })
48
                ->thenInvalid('Invalid type name "%s". (see https://facebook.github.io/graphql/October2016/#Name)')
49 42
        ->end();
50
51 42
        return $node;
52
    }
53
54 42
    protected function defaultValueSection()
55
    {
56 42
        $node = self::createNode('defaultValue', 'variable');
57
58 42
        return $node;
59
    }
60
61 42
    /**
62
     * @param int $level
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 $value === 'cascade' ? ['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 ($level === self::VALIDATION_LEVEL_PROPERTY) {
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 ($level === self::VALIDATION_LEVEL_PROPERTY) {
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
     *@internal
167
     *
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