Conditions | 1 |
Paths | 1 |
Total Lines | 168 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
212 | public function testMergedFragmentsFieldSelection() : void |
||
213 | { |
||
214 | $image = new ObjectType([ |
||
215 | 'name' => 'Image', |
||
216 | 'fields' => [ |
||
217 | 'url' => ['type' => Type::string()], |
||
218 | 'width' => ['type' => Type::int()], |
||
219 | 'height' => ['type' => Type::int()], |
||
220 | ], |
||
221 | ]); |
||
222 | |||
223 | $article = null; |
||
224 | |||
225 | $author = new ObjectType([ |
||
226 | 'name' => 'Author', |
||
227 | 'fields' => static function () use ($image, &$article) { |
||
228 | return [ |
||
229 | 'id' => ['type' => Type::string()], |
||
230 | 'name' => ['type' => Type::string()], |
||
231 | 'pic' => [ |
||
232 | 'type' => $image, |
||
233 | 'args' => [ |
||
234 | 'width' => ['type' => Type::int()], |
||
235 | 'height' => ['type' => Type::int()], |
||
236 | ], |
||
237 | ], |
||
238 | 'recentArticle' => ['type' => $article], |
||
239 | ]; |
||
240 | }, |
||
241 | ]); |
||
242 | |||
243 | $reply = new ObjectType([ |
||
244 | 'name' => 'Reply', |
||
245 | 'fields' => [ |
||
246 | 'author' => ['type' => $author], |
||
247 | 'body' => ['type' => Type::string()], |
||
248 | ], |
||
249 | ]); |
||
250 | |||
251 | $article = new ObjectType([ |
||
252 | 'name' => 'Article', |
||
253 | 'fields' => [ |
||
254 | 'id' => ['type' => Type::string()], |
||
255 | 'isPublished' => ['type' => Type::boolean()], |
||
256 | 'author' => ['type' => $author], |
||
257 | 'title' => ['type' => Type::string()], |
||
258 | 'body' => ['type' => Type::string()], |
||
259 | 'image' => ['type' => $image], |
||
260 | 'replies' => ['type' => Type::listOf($reply)], |
||
261 | ], |
||
262 | ]); |
||
263 | |||
264 | $doc = ' |
||
265 | query Test { |
||
266 | article { |
||
267 | author { |
||
268 | name |
||
269 | pic { |
||
270 | url |
||
271 | width |
||
272 | } |
||
273 | } |
||
274 | image { |
||
275 | width |
||
276 | height |
||
277 | ...MyImage |
||
278 | } |
||
279 | ...Replies01 |
||
280 | ...Replies02 |
||
281 | } |
||
282 | } |
||
283 | fragment MyImage on Image { |
||
284 | url |
||
285 | } |
||
286 | |||
287 | fragment Replies01 on Article { |
||
288 | _replies012: replies { |
||
289 | body |
||
290 | } |
||
291 | } |
||
292 | fragment Replies02 on Article { |
||
293 | _replies012: replies { |
||
294 | author { |
||
295 | id |
||
296 | name |
||
297 | pic { |
||
298 | url |
||
299 | width |
||
300 | ... on Image { |
||
301 | height |
||
302 | } |
||
303 | } |
||
304 | recentArticle { |
||
305 | id |
||
306 | title |
||
307 | body |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | } |
||
312 | '; |
||
313 | |||
314 | $expectedDeepSelection = [ |
||
315 | 'author' => [ |
||
316 | 'name' => true, |
||
317 | 'pic' => [ |
||
318 | 'url' => true, |
||
319 | 'width' => true, |
||
320 | ], |
||
321 | ], |
||
322 | 'image' => [ |
||
323 | 'width' => true, |
||
324 | 'height' => true, |
||
325 | 'url' => true, |
||
326 | ], |
||
327 | 'replies' => [ |
||
328 | 'body' => true, //this would be missing if not for the fix https://github.com/webonyx/graphql-php/pull/98 |
||
329 | 'author' => [ |
||
330 | 'id' => true, |
||
331 | 'name' => true, |
||
332 | 'pic' => [ |
||
333 | 'url' => true, |
||
334 | 'width' => true, |
||
335 | 'height' => true, |
||
336 | ], |
||
337 | 'recentArticle' => [ |
||
338 | 'id' => true, |
||
339 | 'title' => true, |
||
340 | 'body' => true, |
||
341 | ], |
||
342 | ], |
||
343 | ], |
||
344 | ]; |
||
345 | |||
346 | $hasCalled = false; |
||
347 | $actualDefaultSelection = null; |
||
|
|||
348 | $actualDeepSelection = null; |
||
349 | |||
350 | $blogQuery = new ObjectType([ |
||
351 | 'name' => 'Query', |
||
352 | 'fields' => [ |
||
353 | 'article' => [ |
||
354 | 'type' => $article, |
||
355 | 'resolve' => static function ( |
||
356 | $value, |
||
357 | $args, |
||
358 | $context, |
||
359 | ResolveInfo $info |
||
360 | ) use ( |
||
361 | &$hasCalled, |
||
362 | & |
||
363 | $actualDeepSelection |
||
364 | ) { |
||
365 | $hasCalled = true; |
||
366 | $actualDeepSelection = $info->getFieldSelection(5); |
||
367 | |||
368 | return null; |
||
369 | }, |
||
370 | ], |
||
371 | ], |
||
372 | ]); |
||
373 | |||
374 | $schema = new Schema(['query' => $blogQuery]); |
||
375 | $result = GraphQL::executeQuery($schema, $doc)->toArray(); |
||
376 | |||
377 | self::assertTrue($hasCalled); |
||
378 | self::assertEquals(['data' => ['article' => null]], $result); |
||
379 | self::assertEquals($expectedDeepSelection, $actualDeepSelection); |
||
380 | } |
||
382 |