SchemaWarmupper::warmupType()   C
last analyzed

Complexity

Conditions 17
Paths 16

Size

Total Lines 71
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 18.6244

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 43
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 71
ccs 37
cts 45
cp 0.8222
crap 18.6244
rs 5.2166

How to fix   Long Method    Complexity   

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 Andi\GraphQL\Spiral\Common;
6
7
use GraphQL\Type\Definition as Webonyx;
8
use GraphQL\Type\Schema;
9
10
final class SchemaWarmupper
11
{
12 7
    public static function warmup(Schema $schema): void
13
    {
14 7
        $allTypes = [];
15
16 7
        $types = $schema->getConfig()->getTypes();
17 7
        if (\is_callable($types)) {
18 7
            $types = $types();
19
        }
20
21 7
        foreach ($types as $type) {
22 7
            self::warmupType(Schema::resolveType($type), $allTypes);
23
        }
24
25 7
        foreach ($schema->getDirectives() as $directive) {
26 7
            foreach ($directive->args as $arg) {
27 7
                self::warmupType($arg->getType(), $allTypes);
28
            }
29
        }
30
31 7
        if ($type = $schema->getQueryType()) {
32 7
            self::warmupType($type, $allTypes);
33
        }
34
35 7
        if ($type = $schema->getMutationType()) {
36 7
            self::warmupType($type, $allTypes);
37
        }
38
39 7
        if ($type = $schema->getSubscriptionType()) {
40
            self::warmupType($type, $allTypes);
41
        }
42
43 7
        $schema->getTypeMap();
44
    }
45
46 7
    private static function warmupType(Webonyx\Type $type, array &$allTypes): void
47
    {
48 7
        if ($type instanceof Webonyx\WrappingType) {
49 7
            self::warmupType($type->getInnermostType(), $allTypes);
50
51 7
            return;
52
        }
53 7
        \assert($type instanceof Webonyx\NamedType);
54
        /**
55
         * @psalm-suppress NoInterfaceProperties
56
         * @psalm-suppress UndefinedPropertyFetch
57
         */
58 7
        $name = $type->name;
59 7
        if (isset($allTypes[$name])) {
60 7
            return;
61
        }
62
63 7
        $allTypes[$name] = true;
64
65 7
        if ($type instanceof Webonyx\EnumType) {
66 7
            $enumValues = $type->getValues();
67 7
            if (\is_callable($type->config['values'])) {
68
                $values = [];
69
                foreach ($enumValues as $value) {
70
                    $values[$value->name] = [
71
                        'value' => $value->value,
72
                        'description' => $value->description,
73
                        'deprecationReason' => $value->deprecationReason,
74
                    ];
75
                }
76
                $type->config['values'] = $values;
77
            }
78 7
            return;
79
        }
80
81 7
        if ($type instanceof Webonyx\UnionType) {
82 7
            $type->config['types'] = $type->getTypes();
83 7
            foreach ($type->config['types'] as $member) {
84 7
                self::warmupType($member, $allTypes);
85
            }
86 7
            return;
87
        }
88
89 7
        if ($type instanceof Webonyx\InputObjectType) {
90 7
            $type->config['fields'] = $type->getFields();
91 7
            foreach ($type->config['fields'] as $field) {
92 7
                self::warmupType($field->getType(), $allTypes);
93
            }
94 7
            return;
95
        }
96
97 7
        if ($type instanceof Webonyx\ImplementingType) {
98 7
            $interfaces = $type->getInterfaces();
99 7
            foreach ($interfaces as $interface) {
100 7
                self::warmupType($interface, $allTypes);
101
            }
102 7
            \assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType);
103 7
            $type->config['interfaces'] = $interfaces;
104
        }
105
106 7
        if ($type instanceof Webonyx\HasFieldsType) {
107 7
            $fields = $type->getFields();
108 7
            foreach ($fields as $field) {
109 7
                foreach ($field->args as $arg) {
110 7
                    self::warmupType($arg->config['type'] = $arg->getType(), $allTypes);
111
                }
112 7
                self::warmupType($field->getType(), $allTypes);
113
            }
114
115 7
            \assert($type instanceof Webonyx\ObjectType || $type instanceof Webonyx\InterfaceType);
116 7
            $type->config['fields'] = $fields;
117
        }
118
    }
119
}
120