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 (#682)
by Timur
06:41
created

TypeDefinition   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
eloc 66
c 0
b 0
f 0
dl 0
loc 147
ccs 71
cts 71
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveTypeSection() 0 5 1
A create() 0 3 1
A __construct() 0 2 1
A nameSection() 0 10 1
A defaultValueSection() 0 3 1
A descriptionSection() 0 5 1
A deprecationReasonSection() 0 7 1
B validationSection() 0 62 8
A typeSection() 0 11 2
A createNode() 0 3 1
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 47
    protected function __construct()
21
    {
22 47
    }
23
24
    /**
25
     * @return static
26
     */
27 47
    public static function create()
28
    {
29 47
        return new static();
30
    }
31
32 47
    protected function resolveTypeSection()
33
    {
34 47
        $node = self::createNode('resolveType', 'variable');
35
36 47
        return $node;
37
    }
38
39 47
    protected function nameSection()
40
    {
41 47
        $node = self::createNode('name', 'scalar');
42 47
        $node->isRequired();
43 47
        $node->validate()
44 47
            ->ifTrue(fn ($name) => !\preg_match('/^[_a-z][_0-9a-z]*$/i', $name))
45 47
            ->thenInvalid('Invalid type name "%s". (see http://spec.graphql.org/June2018/#sec-Names)')
46 47
        ->end();
47
48 47
        return $node;
49
    }
50
51 47
    protected function defaultValueSection()
52
    {
53 47
        return self::createNode('defaultValue', 'variable');
54
    }
55
56
    /**
57
     * @return ArrayNodeDefinition|NodeDefinition
58
     */
59 47
    protected function validationSection(int $level): NodeParentInterface
60
    {
61 47
        $node = self::createNode('validation', 'array');
62
63
        $node
64
            // allow shorthands
65 47
            ->beforeNormalization()
66
                ->always(function ($value) {
67 3
                    if (\is_string($value)) {
68
                        // shorthand: cascade or link
69 2
                        return 'cascade' === $value ? ['cascade' => null] : ['link' => $value];
70
                    }
71
72 2
                    if (\is_array($value)) {
73 2
                        foreach ($value as $k => $a) {
74 2
                            if (!\is_int($k)) {
75
                                // validation: { link: ... , constraints: ..., cascade: ... }
76 1
                                return $value;
77
                            }
78
                        }
79
                        // validation: [list of constraints]
80 2
                        return ['constraints' => $value];
81
                    }
82
83 1
                    return [];
84 47
                })
85 47
            ->end()
86 47
            ->children()
87 47
                ->scalarNode('link')
88
//                    ->defaultNull()
89 47
                    ->validate()
90
                        ->ifTrue(function ($link) use ($level) {
91 1
                            if (self::VALIDATION_LEVEL_PROPERTY === $level) {
92 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);
93
                            } else {
94 1
                                return !\preg_match('/^(?:\\\\?[A-Za-z][A-Za-z\d]+)*[A-Za-z\d]$/m', $link);
95
                            }
96 47
                        })
97 47
                        ->thenInvalid('Invalid link provided: "%s".')
98 47
                    ->end()
99 47
                ->end()
100 47
                ->variableNode('constraints')->end()
101 47
            ->end();
102
103
        // Add the 'cascade' option if it's a property level validation section
104 47
        if (self::VALIDATION_LEVEL_PROPERTY === $level) {
105
            $node
106 47
                ->children()
107 47
                    ->arrayNode('cascade')
108 47
                        ->children()
109 47
                            ->arrayNode('groups')
110 47
                                ->beforeNormalization()
111 47
                                    ->castToArray()
112 47
                                ->end()
113 47
                                ->scalarPrototype()->end()
0 ignored issues
show
Bug introduced by
The method scalarPrototype() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
                                ->/** @scrutinizer ignore-call */ scalarPrototype()->end()
Loading history...
114 47
                            ->end()
115 47
                        ->end()
116 47
                    ->end()
117 47
                ->end();
118
        }
119
120 47
        return $node;
121
    }
122
123 47
    protected function descriptionSection()
124
    {
125 47
        $node = self::createNode('description', 'scalar');
126
127 47
        return $node;
128
    }
129
130 47
    protected function deprecationReasonSection()
131
    {
132 47
        $node = self::createNode('deprecationReason', 'scalar');
133
134 47
        $node->info('Text describing why this field is deprecated. When not empty - field will not be returned by introspection queries (unless forced)');
135
136 47
        return $node;
137
    }
138
139 47
    protected function typeSection($isRequired = false)
140
    {
141 47
        $node = self::createNode('type', 'scalar');
142
143 47
        $node->info('One of internal or custom types.');
144
145 47
        if ($isRequired) {
146 47
            $node->isRequired();
147
        }
148
149 47
        return $node;
150
    }
151
152
    /**
153
     * @return ArrayNodeDefinition|NodeDefinition
154
     *
155
     * @internal
156
     */
157 47
    protected static function createNode(string $name, string $type = 'array')
158
    {
159 47
        return Configuration::getRootNodeWithoutDeprecation(new TreeBuilder($name, $type), $name, $type);
160
    }
161
}
162