Completed
Push — master ( c9c830...8d8462 )
by Vladimir
89:58 queued 86:21
created

Node::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Language\AST;
6
7
use GraphQL\Utils\Utils;
8
use function get_object_vars;
9
use function is_array;
10
use function is_scalar;
11
use function json_encode;
12
13
/**
14
 * type Node = NameNode
15
 * | DocumentNode
16
 * | OperationDefinitionNode
17
 * | VariableDefinitionNode
18
 * | VariableNode
19
 * | SelectionSetNode
20
 * | FieldNode
21
 * | ArgumentNode
22
 * | FragmentSpreadNode
23
 * | InlineFragmentNode
24
 * | FragmentDefinitionNode
25
 * | IntValueNode
26
 * | FloatValueNode
27
 * | StringValueNode
28
 * | BooleanValueNode
29
 * | EnumValueNode
30
 * | ListValueNode
31
 * | ObjectValueNode
32
 * | ObjectFieldNode
33
 * | DirectiveNode
34
 * | ListTypeNode
35
 * | NonNullTypeNode
36
 */
37
abstract class Node
38
{
39
    /** @var Location */
40
    public $loc;
41
42
    /** @var string */
43
    public $kind;
44
45
    /**
46
     * @param (NameNode|NodeList|SelectionSetNode|Location|string|int|bool|float|null)[] $vars
47
     */
48 965
    public function __construct(array $vars)
49
    {
50 965
        if (empty($vars)) {
51 9
            return;
52
        }
53
54 964
        Utils::assign($this, $vars);
55 964
    }
56
57
    /**
58
     * @return self
59
     */
60 4
    public function cloneDeep()
61
    {
62 4
        return $this->cloneValue($this);
63
    }
64
65
    /**
66
     * @param string|NodeList|Location|Node|(Node|NodeList|Location)[] $value
67
     *
68
     * @return string|NodeList|Location|Node
69
     */
70 4
    private function cloneValue($value)
71
    {
72 4
        if (is_array($value)) {
73
            $cloned = [];
74
            foreach ($value as $key => $arrValue) {
75
                $cloned[$key] = $this->cloneValue($arrValue);
76
            }
77 4
        } elseif ($value instanceof self) {
78 4
            $cloned = clone $value;
79 4
            foreach (get_object_vars($cloned) as $prop => $propValue) {
80 4
                $cloned->{$prop} = $this->cloneValue($propValue);
81
            }
82
        } else {
83 4
            $cloned = $value;
84
        }
85
86 4
        return $cloned;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function __toString()
93
    {
94
        $tmp = $this->toArray(true);
95
96
        return (string) json_encode($tmp);
97
    }
98
99
    /**
100
     * @param bool $recursive
101
     *
102
     * @return mixed[]
103
     */
104 39
    public function toArray($recursive = false)
105
    {
106 39
        if ($recursive) {
107 18
            return $this->recursiveToArray($this);
108
        }
109
110 21
        $tmp = (array) $this;
111
112 21
        if ($this->loc !== null) {
113 9
            $tmp['loc'] = [
114 9
                'start' => $this->loc->start,
115 9
                'end'   => $this->loc->end,
116
            ];
117
        }
118
119 21
        return $tmp;
120
    }
121
122
    /**
123
     * @return mixed[]
124
     */
125 18
    private function recursiveToArray(Node $node)
126
    {
127
        $result = [
128 18
            'kind' => $node->kind,
129
        ];
130
131 18
        if ($node->loc !== null) {
132 4
            $result['loc'] = [
133 4
                'start' => $node->loc->start,
134 4
                'end'   => $node->loc->end,
135
            ];
136
        }
137
138 18
        foreach (get_object_vars($node) as $prop => $propValue) {
139 18
            if (isset($result[$prop])) {
140 18
                continue;
141
            }
142
143 18
            if ($propValue === null) {
144 17
                continue;
145
            }
146
147 18
            if (is_array($propValue) || $propValue instanceof NodeList) {
148 18
                $tmp = [];
149 18
                foreach ($propValue as $tmp1) {
150 18
                    $tmp[] = $tmp1 instanceof Node ? $this->recursiveToArray($tmp1) : (array) $tmp1;
151
                }
152 18
            } elseif ($propValue instanceof Node) {
153 18
                $tmp = $this->recursiveToArray($propValue);
154 18
            } elseif (is_scalar($propValue) || $propValue === null) {
155 18
                $tmp = $propValue;
156
            } else {
157
                $tmp = null;
158
            }
159
160 18
            $result[$prop] = $tmp;
161
        }
162
163 18
        return $result;
164
    }
165
}
166