Field::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 6
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Attribute;
6
7
use Attribute;
8
use GraphQL\Type\Definition\Type;
9
10
/**
11
 * Attribute used to override values for an output field in GraphQL.
12
 *
13
 * All values are optional and should only be used to override
14
 * what is declared by the original method.
15
 */
16
#[Attribute(Attribute::TARGET_METHOD)]
17
final class Field implements ApiAttribute
18
{
19
    /**
20
     * @var Argument[]
21
     */
22
    public array $args = [];
23
24
    public null|string|Type $type = null;
25
26
    /**
27
     * @param null|string $name Can be used to alias the field
28
     * @param null|string $type FQCN of PHP class implementing the GraphQL type, see README.md#type-syntax
29
     */
30 17
    public function __construct(
31
        public ?string $name = null,
32
        ?string $type = null,
33
        public ?string $description = null,
34
    ) {
35 17
        $this->type = $type;
36
    }
37
38 11
    public function toArray(): array
39
    {
40 11
        $args = [];
41 11
        foreach ($this->args as $arg) {
42 7
            $args[] = $arg->toArray();
43
        }
44
45 11
        return [
46 11
            'name' => $this->name,
47 11
            'type' => $this->type,
48 11
            'description' => $this->description,
49 11
            'args' => $args,
50 11
        ];
51
    }
52
}
53