Passed
Push — master ( 645e63...6cbe16 )
by Andrey
52s queued 14s
created

FieldExtractorTrait   A

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
     *     typeMode: 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 3
    private function extract(array $config, string $class): mixed
25
    {
26 3
        if (! isset($config['name'], $config['type'])) {
27
            return $config;
28
        }
29
30 3
        if ($config['type'] instanceof Webonyx\Type) {
31
            return $config;
32
        }
33
34 3
        $parameters = [
35 3
            'name' => $config['name'],
36 3
            'type' => $config['type'],
37 3
            'typeMode' => $config['typeMode'] ?? 0,
38 3
            'description' => $config['description'] ?? null,
39 3
            'deprecationReason'=> $config['deprecationReason'] ?? null,
40 3
        ];
41
42 3
        if (isset($config['defaultValue']) || array_key_exists('defaultValue', $config)) {
43 1
            $parameters['defaultValue'] = $config['defaultValue'];
44
        }
45
46 3
        return new $class(...$parameters);
47
    }
48
}
49