QueryFieldListener::finalize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Spiral\Listener;
6
7
use Andi\GraphQL\Field\QueryFieldInterface;
8
use Andi\GraphQL\Spiral\Config\GraphQLConfig;
9
use Andi\GraphQL\Type\DynamicObjectTypeInterface;
10
use Andi\GraphQL\TypeRegistryInterface;
11
use Psr\Container\ContainerInterface;
12
use ReflectionClass;
13
use Spiral\Tokenizer\Attribute\TargetClass;
14
use Spiral\Tokenizer\TokenizationListenerInterface;
15
16
#[TargetClass(class: QueryFieldInterface::class)]
17
final class QueryFieldListener implements TokenizationListenerInterface
18
{
19
    private array $classes = [];
20
21 5
    public function __construct(
22
        private readonly ContainerInterface $container,
23
        private readonly TypeRegistryInterface $typeRegistry,
24
        private readonly GraphQLConfig $config,
25
    ) {
26 5
    }
27
28 4
    public function listen(ReflectionClass $class): void
29
    {
30 4
        if ($class->isAbstract() || $class->isTrait() || $class->isEnum()) {
0 ignored issues
show
Bug introduced by
The method isEnum() does not exist on ReflectionClass. ( Ignorable by Annotation )

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

30
        if ($class->isAbstract() || $class->isTrait() || $class->/** @scrutinizer ignore-call */ isEnum()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31 3
            return;
32
        }
33
34 1
        $this->classes[] = $class->getName();
35
    }
36
37 4
    public function finalize(): void
38
    {
39 4
        $query = $this->typeRegistry->get($this->config->getQueryType());
40 4
        if (! $query instanceof DynamicObjectTypeInterface) {
41
            return;
42
        }
43
44 4
        foreach ($this->classes as $class) {
45 1
            $query->addAdditionalField($this->container->get($class));
46
        }
47
    }
48
}
49