getTypeFromReflectionUnionType()   C
last analyzed

Complexity

Conditions 12
Paths 42

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 34
c 0
b 0
f 0
nc 42
nop 0
dl 0
loc 52
ccs 35
cts 35
cp 1
crap 12
rs 6.9666

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\Common;
6
7
use Andi\GraphQL\Exception\CantResolveGraphQLTypeException;
8
use Andi\GraphQL\TypeRegistryInterface;
9
use GraphQL\Type\Definition as Webonyx;
10
use ReflectionNamedType;
11
use ReflectionType;
12
use ReflectionUnionType;
13
use UnhandledMatchError;
14
15
class LazyTypeByReflectionType
16
{
17 18
    public function __construct(
18
        private readonly ReflectionType $type,
19
        private readonly TypeRegistryInterface $typeRegistry,
20
        private readonly string $selfClassName,
21
    ) {
22 18
    }
23
24 17
    public function __invoke(): Webonyx\Type
25
    {
26 17
        if ($this->type instanceof ReflectionNamedType) {
27 9
            return $this->getTypeFromReflectionNamedType();
28
        }
29
30 8
        if ($this->type instanceof ReflectionUnionType) {
31 7
            return $this->getTypeFromReflectionUnionType();
32
        }
33
34 1
        throw new CantResolveGraphQLTypeException('Can\'t resolve GraphQL type from ReflectionType');
35
    }
36
37 9
    private function getTypeFromReflectionNamedType(): Webonyx\Type
38
    {
39 9
        \assert($this->type instanceof ReflectionNamedType);
40 9
        if ($this->type->isBuiltin()) {
41
            try {
42 5
                $type = match ($this->type->getName()) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

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

42
                $type = match ($this->type->/** @scrutinizer ignore-call */ getName()) {
Loading history...
43 5
                    'int' => $this->typeRegistry->get(Webonyx\IntType::class),
44 5
                    'string' => $this->typeRegistry->get(Webonyx\StringType::class),
45 5
                    'bool', 'true', 'false' => $this->typeRegistry->get(Webonyx\BooleanType::class),
46 5
                    'float' => $this->typeRegistry->get(Webonyx\FloatType::class),
47 5
                };
48 1
            } catch (UnhandledMatchError $exception) {
49 1
                $message = 'Can\'t resolve GraphQL type from ReflectionType';
50 5
                throw new CantResolveGraphQLTypeException($message, 0, $exception);
51
            }
52
        } else {
53 4
            $name = $this->type->getName();
54
55
            /** @psalm-suppress TypeDoesNotContainType */
56 4
            $type = 'self' === $name || 'static' === $name
57 3
                ? $this->typeRegistry->get($this->selfClassName)
58 1
                : $this->typeRegistry->get($name);
59
        }
60
61 8
        return !$this->type->allowsNull() && $type instanceof Webonyx\NullableType
62 4
            ? Webonyx\Type::nonNull($type)
63 8
            : $type;
64
    }
65
66
    /**
67
     * @return Webonyx\Type
68
     */
69 7
    private function getTypeFromReflectionUnionType(): Webonyx\Type
70
    {
71 7
        $names = [];
72 7
        $types = [];
73 7
        $allowsNull = false;
74 7
        \assert($this->type instanceof ReflectionUnionType);
75 7
        foreach ($this->type->getTypes() as $type) {
0 ignored issues
show
Bug introduced by
The method getTypes() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionUnionType. ( Ignorable by Annotation )

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

75
        foreach ($this->type->/** @scrutinizer ignore-call */ getTypes() as $type) {
Loading history...
76 7
            \assert($type instanceof ReflectionNamedType);
77 7
            if ($type->isBuiltin()) {
78 2
                $allowsNull = 'null' === $type->getName()
79 2
                    || throw new CantResolveGraphQLTypeException('UnionType must contains only ObjectTypes');
80
81 2
                continue;
82
            }
83
84 7
            $name = $type->getName();
85 7
            if ($this->typeRegistry->has($name)) {
86 6
                $gqlType = $this->typeRegistry->get($name);
87
88 6
                if (! $gqlType instanceof Webonyx\ObjectType) {
89 1
                    throw new CantResolveGraphQLTypeException('UnionType must contains only ObjectTypes');
90
                }
91
92 5
                $names[] = (string) $gqlType;
93 5
                $types[] = $name;
94
            } else {
95 1
                throw new CantResolveGraphQLTypeException(\sprintf('Undefined ObjectType "%s" for UnionType', $name));
96
            }
97
        }
98
99 5
        \sort($names);
100 5
        $name = \implode('', $names) . 'UnionType';
101
102 5
        if ($this->typeRegistry->has($name)) {
103 2
            $existsType = $this->typeRegistry->get($name);
104
105 2
            return !$this->type->allowsNull() && !$allowsNull && $existsType instanceof Webonyx\NullableType
106 1
                ? Webonyx\Type::nonNull($existsType)
107 2
                : $existsType;
108
        }
109
110 3
        $unionType = new Webonyx\UnionType([
111 3
            'name' => $name,
112 3
            'types' => new LazyTypeIterator(fn() => $types, $this->typeRegistry),
113 3
            'resolveType' => new ResolveType($this->typeRegistry),
114 3
        ]);
115
116 3
        $this->typeRegistry->register($unionType);
117
118 3
        return $this->type->allowsNull() || $allowsNull
119 1
            ? $unionType
120 3
            : Webonyx\Type::nonNull($unionType);
121
    }
122
}
123