ObjectType   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 18
c 3
b 0
f 2
dl 0
loc 49
ccs 0
cts 18
cp 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addAdditionalField() 0 5 1
A extractFields() 0 16 6
A __construct() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\WebonyxType;
6
7
use Andi\GraphQL\ObjectFieldResolver\ObjectFieldResolverInterface;
8
use Andi\GraphQL\Type\DynamicObjectTypeInterface;
9
use GraphQL\Type\Definition as Webonyx;
10
11
/**
12
 * @see Webonyx\ObjectType
13
 *
14
 * @phpstan-import-type ObjectConfig from Webonyx\ObjectType
15
 */
16
class ObjectType extends Webonyx\ObjectType implements DynamicObjectTypeInterface
17
{
18
    /**
19
     * @var callable():iterable|iterable|null
20
     */
21
    private readonly mixed $nativeFields;
22
23
    private array $additionalFields = [];
24
25
    /**
26
     * @param ObjectConfig $config
0 ignored issues
show
Bug introduced by
The type Andi\GraphQL\WebonyxType\ObjectConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
     * @param ObjectFieldResolverInterface $objectFieldResolver
28
     */
29
    public function __construct(
30
        array $config,
31
        private readonly ObjectFieldResolverInterface $objectFieldResolver,
32
    ) {
33
        if (isset($config['fields'])) {
34
            $this->nativeFields = $config['fields'];
0 ignored issues
show
Bug introduced by
The property nativeFields is declared read-only in Andi\GraphQL\WebonyxType\ObjectType.
Loading history...
35
        }
36
37
        $config['fields'] = $this->extractFields(...);
38
39
        parent::__construct($config);
40
    }
41
42
    public function addAdditionalField(mixed $field): static
43
    {
44
        $this->additionalFields[] = $field;
45
46
        return $this;
47
    }
48
49
    private function extractFields(): iterable
0 ignored issues
show
Unused Code introduced by
The method extractFields() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
50
    {
51
        if (isset($this->nativeFields)) {
52
            $fields = \is_callable($this->nativeFields)
53
                ? \call_user_func($this->nativeFields)
54
                : $this->nativeFields;
55
56
            if (\is_iterable($fields)) {
57
                foreach ($fields as $field) {
58
                    yield $this->objectFieldResolver->resolve($field);
59
                }
60
            }
61
        }
62
63
        foreach ($this->additionalFields as $field) {
64
            yield $this->objectFieldResolver->resolve($field);
65
        }
66
    }
67
}
68