FieldExtractorTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 38
ccs 13
cts 15
cp 0.8667
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A extract() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Field;
6
7
use GraphQL\Type\Definition as Webonyx;
8
9
trait FieldExtractorTrait
10
{
11
    /**
12
     * @param array{
13
     *     name: string,
14
     *     type: Webonyx\Type|string,
15
     *     mode: int,
16
     *     description: string,
17
     *     deprecationReason: string,
18
     *     defaultValue: mixed
19
     * } $config
20
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
21
     *
22
     * @return mixed
23
     */
24 4
    private function extract(array $config, string $class): mixed
25
    {
26 4
        if (! isset($config['name'], $config['type'])) {
27
            return $config;
28
        }
29
30 4
        if ($config['type'] instanceof Webonyx\Type) {
31
            return $config;
32
        }
33
34 4
        $parameters = [
35 4
            'name' => $config['name'],
36 4
            'type' => $config['type'],
37 4
            'mode' => $config['mode'] ?? 0,
38 4
            'description' => $config['description'] ?? null,
39 4
            'deprecationReason'=> $config['deprecationReason'] ?? null,
40 4
        ];
41
42 4
        if (isset($config['defaultValue']) || \array_key_exists('defaultValue', $config)) {
43 1
            $parameters['defaultValue'] = $config['defaultValue'];
44
        }
45
46 4
        return new $class(...$parameters);
47
    }
48
}
49