Passed
Pull Request — master (#3)
by
unknown
05:56 queued 02:23
created

Fragment   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fields() 0 19 5
A __toString() 0 7 1
A printFields() 0 25 6
A __construct() 0 7 1
1
<?php
2
3
namespace Commadore\GraphQL;
4
5
final class Fragment
6
{
7
    /**
8
     * @var string
9
     */
10
    public $name;
11
12
    /**
13
     * @var string
14
     */
15
    public $type;
16
17
    /**
18
     * @var array
19
     */
20
    public $fields = [];
21
22
    /**
23
     * @param string $name
24
     * @param string $type
25
     * @param array $fields
26
     */
27
    public function __construct(string $name, string $type, array $fields = [])
28
    {
29
        $this->name = $name;
30
31
        $this->type = $type;
32
33
        $this->fields($fields);
34
    }
35
36
    /**
37
     * @param array $fields
38
     *
39
     * @return Fragment
40
     */
41
    public function fields(array $fields = []): Fragment
42
    {
43
        foreach ($fields as $fieldAlias => $field) {
44
            if (\is_string($field)) {
45
                if (\is_string($fieldAlias)) {
46
                    $this->fields[$fieldAlias] = $field;
47
                } else {
48
                    $this->fields[$field] = $field;
49
                }
50
            }
51
52
            if ($field instanceof Query) {
53
                $this->fields[$fieldAlias] = $field;
54
            }
55
        }
56
57
        ksort($this->fields);
58
59
        return $this;
60
    }
61
62
    public function __toString()
63
    {
64
        $query = sprintf('fragment %s on %s { %s }', $this->name, $this->type, static::printFields($this->fields));
65
66
        $query = \GraphQL\Language\Printer::doPrint(\GraphQL\Language\Parser::parse((string) $query));
67
68
        return $query;
69
    }
70
71
    /**
72
     * @param array $value
73
     *
74
     * @return string
75
     */
76
    private static function printFields(array $value): string
77
    {
78
        $fields = [];
79
80
        foreach ($value as $fieldAlias => $field) {
81
            $directive = '';
82
83
            if (\is_string($field)) {
84
                if ($fieldAlias !== $field) {
85
                    $fields[] = sprintf('%s: %s %s', $fieldAlias, $field, $directive);
86
                } else {
87
                    $fields[] = sprintf('%s %s', $field, $directive);
88
                }
89
            }
90
91
            if ($field instanceof Query) {
92
                if (null !== $field->type) {
93
                    $fieldAlias = sprintf('%s: %s', $fieldAlias, $field->type);
0 ignored issues
show
Bug introduced by
$field->type of type array is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
                    $fieldAlias = sprintf('%s: %s', $fieldAlias, /** @scrutinizer ignore-type */ $field->type);
Loading history...
94
                }
95
96
                $fields[] = sprintf('%s %s { %s }', $fieldAlias, $directive, static::printFields($field->fields));
97
            }
98
        }
99
100
        return implode(', ', $fields);
101
    }
102
103
}
104