ResolveType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 33
ccs 16
cts 16
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectType() 0 17 4
A __construct() 0 3 1
A __invoke() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Common;
6
7
use Andi\GraphQL\TypeRegistryInterface;
8
use GraphQL\Type\Definition as Webonyx;
9
10
final class ResolveType
11
{
12 7
    public function __construct(
13
        private readonly TypeRegistryInterface $typeRegistry,
14
    ) {
15 7
    }
16
17 6
    public function __invoke(mixed $value, mixed $context, Webonyx\ResolveInfo $info): ?Webonyx\ObjectType
18
    {
19 6
        if (! \is_object($value)) {
20 2
            return null;
21
        }
22
23 4
        return $this->getObjectType(new \ReflectionClass($value));
24
    }
25
26 4
    private function getObjectType(\ReflectionClass $class): ?Webonyx\ObjectType
27
    {
28 4
        $name = $class->getName();
29
30 4
        if ($this->typeRegistry->has($name)) {
31 3
            $type = $this->typeRegistry->get($name);
32
33 3
            return $type instanceof Webonyx\ObjectType
34 2
                ? $type
35 3
                : null;
36
        }
37
38 2
        if ($parent = $class->getParentClass()) {
39 1
            return $this->getObjectType($parent);
40
        }
41
42 1
        return null;
43
    }
44
}
45