MutationFieldListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Spiral\Listener;
6
7
use Andi\GraphQL\Field\MutationFieldInterface;
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: MutationFieldInterface::class)]
17
final class MutationFieldListener 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
        $type = $this->config->getMutationType();
40
41 4
        $mutation = null !== $type && $this->typeRegistry->has($type)
42 4
            ? $this->typeRegistry->get($type)
43
            : null;
44
45 4
        if (! $mutation instanceof DynamicObjectTypeInterface) {
46
            return;
47
        }
48
49 4
        foreach ($this->classes as $class) {
50 1
            $mutation->addAdditionalField($this->container->get($class));
51
        }
52
    }
53
}
54