Total Complexity | 40 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like GraphQLException often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GraphQLException, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class GraphQLException extends AbstractException implements SerializationInterface |
||
18 | { |
||
19 | use ArrayToJsonTrait; |
||
20 | |||
21 | /** |
||
22 | * An array of { line, column } locations within the source GraphQL document |
||
23 | * which correspond to this error. |
||
24 | * |
||
25 | * Errors during validation often contain multiple locations, for example to |
||
26 | * point out two things with the same name. Errors during execution include a |
||
27 | * single location, the field which produced the error. |
||
28 | * |
||
29 | * @var array|null |
||
30 | */ |
||
31 | protected $locations; |
||
32 | |||
33 | /** |
||
34 | * An array describing the JSON-path into the execution response which |
||
35 | * corresponds to this error. Only included for errors during execution. |
||
36 | * |
||
37 | * @var string[]|null |
||
38 | */ |
||
39 | protected $path; |
||
40 | |||
41 | /** |
||
42 | * An array of GraphQL AST Nodes corresponding to this error. |
||
43 | * |
||
44 | * @var NodeInterface[]|null |
||
45 | */ |
||
46 | protected $nodes; |
||
47 | |||
48 | /** |
||
49 | * The source GraphQL document for the first location of this error. |
||
50 | * |
||
51 | * Note that if this Error represents more than one node, the source may not |
||
52 | * represent nodes after the first node. |
||
53 | * |
||
54 | * @var Source|null |
||
55 | */ |
||
56 | protected $source; |
||
57 | |||
58 | /** |
||
59 | * An array of character offsets within the source GraphQL document |
||
60 | * which correspond to this error. |
||
61 | * |
||
62 | * @var int[]|null |
||
63 | */ |
||
64 | protected $positions; |
||
65 | |||
66 | /** |
||
67 | * Extension fields to add to the formatted error. |
||
68 | * |
||
69 | * @var array|null |
||
70 | */ |
||
71 | protected $extensions; |
||
72 | |||
73 | /** |
||
74 | * @var null|\Throwable |
||
75 | */ |
||
76 | protected $originalException; |
||
77 | |||
78 | /** |
||
79 | * ExecutionException constructor. |
||
80 | * |
||
81 | * @param string $message |
||
82 | * @param array|null $nodes |
||
83 | * @param Source|null $source |
||
84 | * @param array|null $positions |
||
85 | * @param array|null $path |
||
86 | * @param array|null $extensions |
||
87 | * @param \Throwable|null $originalException |
||
88 | */ |
||
89 | public function __construct( |
||
90 | string $message, |
||
91 | ?array $nodes = null, |
||
92 | ?Source $source = null, |
||
93 | ?array $positions = null, |
||
94 | ?array $path = null, |
||
95 | ?array $extensions = null, |
||
96 | ?\Throwable $originalException = null |
||
97 | ) { |
||
98 | parent::__construct($message); |
||
99 | |||
100 | $this->resolveNodes($nodes); |
||
101 | $this->resolveSource($source); |
||
102 | $this->resolvePositions($positions); |
||
103 | $this->resolveLocations($positions, $source); |
||
104 | |||
105 | $this->path = $path; |
||
106 | $this->extensions = $extensions; |
||
107 | $this->originalException = $originalException; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return NodeInterface[] |
||
112 | */ |
||
113 | public function getNodes(): ?array |
||
114 | { |
||
115 | return $this->nodes; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function hasSource(): bool |
||
122 | { |
||
123 | return null !== $this->source; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return Source|null |
||
128 | */ |
||
129 | public function getSource(): ?Source |
||
130 | { |
||
131 | return $this->source; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return int[]|null |
||
136 | */ |
||
137 | public function getPositions(): ?array |
||
138 | { |
||
139 | return $this->positions; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function hasLocations(): bool |
||
146 | { |
||
147 | return !empty($this->locations); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return array|null |
||
152 | */ |
||
153 | public function getLocations(): ?array |
||
154 | { |
||
155 | return $this->locations; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array|null |
||
160 | */ |
||
161 | public function getLocationsAsArray(): ?array |
||
162 | { |
||
163 | return !empty($this->locations) ? \array_map(function (SourceLocation $location) { |
||
164 | return $location->toArray(); |
||
165 | }, $this->locations) : null; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @return array|null |
||
170 | */ |
||
171 | public function getPath(): ?array |
||
172 | { |
||
173 | return $this->path; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return array|null |
||
178 | */ |
||
179 | public function getExtensions(): ?array |
||
180 | { |
||
181 | return $this->extensions; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param array|null $extensions |
||
186 | * @return self |
||
187 | */ |
||
188 | public function setExtensions(?array $extensions): self |
||
189 | { |
||
190 | $this->extensions = $extensions; |
||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return \Throwable|null |
||
196 | */ |
||
197 | public function getOriginalException(): ?\Throwable |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return null|string |
||
204 | */ |
||
205 | public function getOriginalErrorMessage(): ?string |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @inheritdoc |
||
212 | */ |
||
213 | public function toArray(): array |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | public function __toString(): string |
||
236 | { |
||
237 | return printError($this); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param array|null $nodes |
||
242 | * @return $this |
||
243 | */ |
||
244 | protected function resolveNodes(?array $nodes): self |
||
245 | { |
||
246 | if (\is_array($nodes)) { |
||
|
|||
247 | $nodes = !empty($nodes) ? $nodes : []; |
||
248 | } else { |
||
249 | $nodes = [$nodes]; |
||
250 | } |
||
251 | |||
252 | $this->nodes = \array_filter($nodes, function ($node) { |
||
253 | return null !== $node; |
||
254 | }); |
||
255 | |||
256 | return $this; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param Source|null $source |
||
261 | * @return $this |
||
262 | */ |
||
263 | protected function resolveSource(?Source $source): self |
||
264 | { |
||
265 | if (null === $source && !empty($this->nodes)) { |
||
266 | $firstNode = $this->nodes[0] ?? null; |
||
267 | $location = null !== $firstNode ? $firstNode->getLocation() : null; |
||
268 | $source = null !== $location ? $location->getSource() : null; |
||
269 | } |
||
270 | |||
271 | $this->source = $source; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param array|null $positions |
||
278 | * @return $this |
||
279 | */ |
||
280 | protected function resolvePositions(?array $positions): self |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param array|null $positions |
||
305 | * @param Source|null $source |
||
306 | * @return $this |
||
307 | */ |
||
308 | protected function resolveLocations(?array $positions, ?Source $source): self |
||
331 | } |
||
332 | } |
||
333 |