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:27
created

TypeDefinition::deprecationReasonSelection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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