Passed
Pull Request — master (#190)
by Sebastian
03:03
created

GraphQLException::resolvePositions()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Error;
4
5
use Digia\GraphQL\Language\Node\NodeInterface;
6
use Digia\GraphQL\Language\Source;
7
use Digia\GraphQL\Language\SourceLocation;
8
use Digia\GraphQL\Util\ArrayToJsonTrait;
9
use Digia\GraphQL\Util\SerializationInterface;
10
11
/**
12
 * An GraphQLException describes an exception thrown during the execute
13
 * phase of performing a GraphQL operation. In addition to a message
14
 * and stack trace, it also includes information about the locations in a
15
 * GraphQL document and/or execution result that correspond to the Error.
16
 */
17
class GraphQLException extends AbstractException implements SerializationInterface
18
{
19
20
    use ArrayToJsonTrait;
21
22
    /**
23
     * An array of { line, column } locations within the source GraphQL
24
     * document
25
     * which correspond to this error.
26
     *
27
     * Errors during validation often contain multiple locations, for example
28
     * to
29
     * point out two things with the same name. Errors during execution include
30
     * a single location, the field which produced the error.
31
     *
32
     * @var array|null
33
     */
34
    protected $locations;
35
36
    /**
37
     * An array describing the JSON-path into the execution response which
38
     * corresponds to this error. Only included for errors during execution.
39
     *
40
     * @var string[]|null
41
     */
42
    protected $path;
43
44
    /**
45
     * An array of GraphQL AST Nodes corresponding to this error.
46
     *
47
     * @var NodeInterface[]|null
48
     */
49
    protected $nodes;
50
51
    /**
52
     * The source GraphQL document for the first location of this error.
53
     *
54
     * Note that if this Error represents more than one node, the source may not
55
     * represent nodes after the first node.
56
     *
57
     * @var Source|null
58
     */
59
    protected $source;
60
61
    /**
62
     * An array of character offsets within the source GraphQL document
63
     * which correspond to this error.
64
     *
65
     * @var int[]|null
66
     */
67
    protected $positions;
68
69
    /**
70
     * Extension fields to add to the formatted error.
71
     *
72
     * @var \Exception|null
73
     */
74
    protected $originalException;
75
76
    /**
77
     * ExecutionException constructor.
78
     *
79
     * @param string $message
80
     * @param array|null $nodes
81
     * @param Source|null $source
82
     * @param array|null $positions
83
     * @param array|null $path
84
     * @param \Throwable|null $originalException
85
     */
86
    public function __construct(
87
      string $message,
88
      ?array $nodes = null,
89
      ?Source $source = null,
90
      ?array $positions = null,
91
      ?array $path = null,
92
      ?\Throwable $originalException = null
93
    ) {
94
        parent::__construct($message);
95
96
        $this->resolveNodes($nodes);
97
        $this->resolveSource($source);
98
        $this->resolvePositions($positions);
99
        $this->resolveLocations($positions, $source);
100
101
        $this->path = $path;
102
        $this->originalException = $originalException;
103
    }
104
105
    /**
106
     * @param array|null $nodes
107
     *
108
     * @return $this
109
     */
110
    protected function resolveNodes(?array $nodes)
111
    {
112
        $nodes = \is_array($nodes)
113
          ? (!empty($nodes) ? $nodes : [])
114
          : (null !== $nodes ? [$nodes] : []);
0 ignored issues
show
introduced by
The condition null !== $nodes is always false.
Loading history...
115
116
        $this->nodes = \array_filter($nodes, function ($node) {
117
            return null !== $node;
118
        });
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param Source|null $source
125
     *
126
     * @return $this
127
     */
128
    protected function resolveSource(?Source $source)
129
    {
130
        if (null === $source && !empty($this->nodes)) {
131
            $firstNode = $this->nodes[0] ?? null;
132
            $location = null !== $firstNode ? $firstNode->getLocation() : null;
133
            $source = null !== $location ? $location->getSource() : null;
134
        }
135
136
        $this->source = $source;
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param array|null $positions
143
     *
144
     * @return $this
145
     */
146
    protected function resolvePositions(?array $positions)
147
    {
148
        if (null === $positions && !empty($this->nodes)) {
149
            $positions = array_reduce($this->nodes,
150
              function (array $list, ?NodeInterface $node) {
151
                  if (null !== $node) {
152
                      $location = $node->getLocation();
153
                      if (null !== $location) {
154
                          $list[] = $location->getStart();
155
                      }
156
                  }
157
                  return $list;
158
              }, []);
159
        }
160
161
        if (null !== $positions && empty($positions)) {
162
            $positions = null;
163
        }
164
165
        $this->positions = $positions;
166
167
        return $this;
168
    }
169
170
    /**
171
     * @param array|null $positions
172
     * @param Source|null $source
173
     *
174
     * @return $this
175
     */
176
    protected function resolveLocations(?array $positions, ?Source $source)
177
    {
178
        if (null !== $positions && null !== $source) {
179
            $locations = array_map(function ($position) use ($source) {
180
                return SourceLocation::fromSource($source, $position);
181
            }, $positions);
182
        } elseif (!empty($this->nodes)) {
183
            $locations = array_reduce($this->nodes,
184
              function (array $list, NodeInterface $node) {
185
                  $location = $node->getLocation();
186
                  if (null !== $location) {
187
                      $list[] = SourceLocation::fromSource($location->getSource(),
188
                        $location->getStart());
189
                  }
190
                  return $list;
191
              }, []);
192
        }
193
194
        if (isset($locations)) {
195
            $this->locations = $locations;
196
        }
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return NodeInterface[]
203
     */
204
    public function getNodes(): ?array
205
    {
206
        return $this->nodes;
207
    }
208
209
    /**
210
     * @return bool
211
     */
212
    public function hasSource(): bool
213
    {
214
        return null !== $this->source;
215
    }
216
217
    /**
218
     * @return Source|null
219
     */
220
    public function getSource(): ?Source
221
    {
222
        return $this->source;
223
    }
224
225
    /**
226
     * @return int[]|null
227
     */
228
    public function getPositions(): ?array
229
    {
230
        return $this->positions;
231
    }
232
233
    /**
234
     * @return bool
235
     */
236
    public function hasLocations(): bool
237
    {
238
        return !empty($this->locations);
239
    }
240
241
    /**
242
     * @return array|null
243
     */
244
    public function getLocations(): ?array
245
    {
246
        return $this->locations;
247
    }
248
249
    /**
250
     * @return array|null
251
     */
252
    public function getPath(): ?array
253
    {
254
        return $this->path;
255
    }
256
257
    /**
258
     * @return \Exception|null
259
     */
260
    public function getOriginalException(): ?\Exception
261
    {
262
        return $this->originalException;
263
    }
264
265
    /**
266
     * @return null|string
267
     */
268
    public function getOriginalErrorMessage(): ?string
269
    {
270
        return null !== $this->originalException ? $this->originalException->getMessage() : null;
271
    }
272
273
    /**
274
     * @inheritdoc
275
     */
276
    public function toArray(): array
277
    {
278
        return [
279
          'message' => $this->message,
280
          'locations' => $this->getLocationsAsArray(),
281
          'path' => $this->path,
282
        ];
283
    }
284
285
    /**
286
     * @return array|null
287
     */
288
    public function getLocationsAsArray(): ?array
289
    {
290
        return !empty($this->locations) ? array_map(function (
291
          SourceLocation $location
292
        ) {
293
            return $location->toArray();
294
        }, $this->locations) : null;
295
    }
296
297
    /**
298
     * @inheritdoc
299
     */
300
    public function __toString(): string
301
    {
302
        return printError($this);
303
    }
304
}
305