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
20:26 queued 09:21
created

TypeDefinition::validationSection()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 65
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 8

Importance

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