Conditions | 1 |
Paths | 1 |
Total Lines | 289 |
Code Lines | 174 |
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 |
||
298 | public function testMergedFragmentsQueryPlan() : void |
||
299 | { |
||
300 | $image = new ObjectType([ |
||
301 | 'name' => 'Image', |
||
302 | 'fields' => [ |
||
303 | 'url' => ['type' => Type::string()], |
||
304 | 'width' => ['type' => Type::int()], |
||
305 | 'height' => ['type' => Type::int()], |
||
306 | ], |
||
307 | ]); |
||
308 | |||
309 | $article = null; |
||
310 | |||
311 | $author = new ObjectType([ |
||
312 | 'name' => 'Author', |
||
313 | 'fields' => static function () use ($image, &$article) { |
||
314 | return [ |
||
315 | 'id' => ['type' => Type::string()], |
||
316 | 'name' => ['type' => Type::string()], |
||
317 | 'pic' => [ |
||
318 | 'type' => $image, |
||
319 | 'args' => [ |
||
320 | 'width' => ['type' => Type::int()], |
||
321 | 'height' => ['type' => Type::int()], |
||
322 | ], |
||
323 | ], |
||
324 | 'recentArticle' => ['type' => $article], |
||
325 | ]; |
||
326 | }, |
||
327 | ]); |
||
328 | |||
329 | $reply = new ObjectType([ |
||
330 | 'name' => 'Reply', |
||
331 | 'fields' => [ |
||
332 | 'author' => ['type' => $author], |
||
333 | 'body' => ['type' => Type::string()], |
||
334 | ], |
||
335 | ]); |
||
336 | |||
337 | $article = new ObjectType([ |
||
338 | 'name' => 'Article', |
||
339 | 'fields' => [ |
||
340 | 'id' => ['type' => Type::string()], |
||
341 | 'isPublished' => ['type' => Type::boolean()], |
||
342 | 'author' => ['type' => $author], |
||
343 | 'title' => ['type' => Type::string()], |
||
344 | 'body' => ['type' => Type::string()], |
||
345 | 'image' => ['type' => $image], |
||
346 | 'replies' => ['type' => Type::listOf($reply)], |
||
347 | ], |
||
348 | ]); |
||
349 | |||
350 | $doc = ' |
||
351 | query Test { |
||
352 | article { |
||
353 | author { |
||
354 | name |
||
355 | pic(width: 100, height: 200) { |
||
356 | url |
||
357 | width |
||
358 | } |
||
359 | } |
||
360 | image { |
||
361 | width |
||
362 | height |
||
363 | ...MyImage |
||
364 | } |
||
365 | ...Replies01 |
||
366 | ...Replies02 |
||
367 | } |
||
368 | } |
||
369 | fragment MyImage on Image { |
||
370 | url |
||
371 | } |
||
372 | |||
373 | fragment Replies01 on Article { |
||
374 | _replies012: replies { |
||
375 | body |
||
376 | } |
||
377 | } |
||
378 | fragment Replies02 on Article { |
||
379 | _replies012: replies { |
||
380 | author { |
||
381 | id |
||
382 | name |
||
383 | pic { |
||
384 | url |
||
385 | width |
||
386 | ... on Image { |
||
387 | height |
||
388 | } |
||
389 | } |
||
390 | recentArticle { |
||
391 | id |
||
392 | title |
||
393 | body |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | } |
||
398 | '; |
||
399 | |||
400 | $expectedQueryPlan = [ |
||
401 | 'author' => [ |
||
402 | 'type' => $author, |
||
403 | 'args' => [], |
||
404 | 'fields' => [ |
||
405 | 'name' => [ |
||
406 | 'type' => Type::string(), |
||
407 | 'args' => [], |
||
408 | 'fields' => [], |
||
409 | ], |
||
410 | 'pic' => [ |
||
411 | 'type' => $image, |
||
412 | 'args' => [ |
||
413 | 'width' => 100, |
||
414 | 'height' => 200, |
||
415 | ], |
||
416 | 'fields' => [ |
||
417 | 'url' => [ |
||
418 | 'type' => Type::string(), |
||
419 | 'args' => [], |
||
420 | 'fields' => [], |
||
421 | ], |
||
422 | 'width' => [ |
||
423 | 'type' => Type::int(), |
||
424 | 'args' => [], |
||
425 | 'fields' => [], |
||
426 | ], |
||
427 | ], |
||
428 | ], |
||
429 | ], |
||
430 | ], |
||
431 | 'image' => [ |
||
432 | 'type' => $image, |
||
433 | 'args' => [], |
||
434 | 'fields' => [ |
||
435 | 'url' => [ |
||
436 | 'type' => Type::string(), |
||
437 | 'args' => [], |
||
438 | 'fields' => [], |
||
439 | ], |
||
440 | 'width' => [ |
||
441 | 'type' => Type::int(), |
||
442 | 'args' => [], |
||
443 | 'fields' => [], |
||
444 | ], |
||
445 | 'height' => [ |
||
446 | 'type' => Type::int(), |
||
447 | 'args' => [], |
||
448 | 'fields' => [], |
||
449 | ], |
||
450 | ], |
||
451 | ], |
||
452 | 'replies' => [ |
||
453 | 'type' => Type::listOf($reply), |
||
454 | 'args' => [], |
||
455 | 'fields' => [ |
||
456 | 'body' => [ |
||
457 | 'type' => Type::string(), |
||
458 | 'args' => [], |
||
459 | 'fields' => [], |
||
460 | ], |
||
461 | 'author' => [ |
||
462 | 'type' => $author, |
||
463 | 'args' => [], |
||
464 | 'fields' => [ |
||
465 | 'id' => [ |
||
466 | 'type' => Type::string(), |
||
467 | 'args' => [], |
||
468 | 'fields' => [], |
||
469 | ], |
||
470 | 'name' => [ |
||
471 | 'type' => Type::string(), |
||
472 | 'args' => [], |
||
473 | 'fields' => [], |
||
474 | ], |
||
475 | 'pic' => [ |
||
476 | 'type' => $image, |
||
477 | 'args' => [], |
||
478 | 'fields' => [ |
||
479 | 'url' => [ |
||
480 | 'type' => Type::string(), |
||
481 | 'args' => [], |
||
482 | 'fields' => [], |
||
483 | ], |
||
484 | 'width' => [ |
||
485 | 'type' => Type::int(), |
||
486 | 'args' => [], |
||
487 | 'fields' => [], |
||
488 | ], |
||
489 | 'height' => [ |
||
490 | 'type' => Type::int(), |
||
491 | 'args' => [], |
||
492 | 'fields' => [], |
||
493 | ], |
||
494 | ], |
||
495 | ], |
||
496 | 'recentArticle' => [ |
||
497 | 'type' => $article, |
||
498 | 'args' => [], |
||
499 | 'fields' => [ |
||
500 | 'id' => [ |
||
501 | 'type' => Type::string(), |
||
502 | 'args' => [], |
||
503 | 'fields' => [], |
||
504 | ], |
||
505 | 'title' => [ |
||
506 | 'type' => Type::string(), |
||
507 | 'args' => [], |
||
508 | 'fields' => [], |
||
509 | ], |
||
510 | 'body' => [ |
||
511 | 'type' => Type::string(), |
||
512 | 'args' => [], |
||
513 | 'fields' => [], |
||
514 | ], |
||
515 | ], |
||
516 | ], |
||
517 | ], |
||
518 | ], |
||
519 | ], |
||
520 | ], |
||
521 | ]; |
||
522 | |||
523 | $expectedReferencedTypes = [ |
||
524 | 'Image', |
||
525 | 'Author', |
||
526 | 'Reply', |
||
527 | 'Article', |
||
528 | ]; |
||
529 | |||
530 | $expectedReferencedFields = [ |
||
531 | 'url', |
||
532 | 'width', |
||
533 | 'height', |
||
534 | 'name', |
||
535 | 'pic', |
||
536 | 'id', |
||
537 | 'recentArticle', |
||
538 | 'body', |
||
539 | 'author', |
||
540 | 'replies', |
||
541 | 'title', |
||
542 | 'image', |
||
543 | ]; |
||
544 | |||
545 | $hasCalled = false; |
||
546 | /** @var QueryPlan $queryPlan */ |
||
547 | $queryPlan = null; |
||
548 | |||
549 | $blogQuery = new ObjectType([ |
||
550 | 'name' => 'Query', |
||
551 | 'fields' => [ |
||
552 | 'article' => [ |
||
553 | 'type' => $article, |
||
554 | 'resolve' => static function ( |
||
555 | $value, |
||
556 | $args, |
||
557 | $context, |
||
558 | ResolveInfo $info |
||
559 | ) use ( |
||
560 | &$hasCalled, |
||
561 | &$queryPlan |
||
562 | ) { |
||
563 | $hasCalled = true; |
||
564 | $queryPlan = $info->lookAhead(); |
||
565 | |||
566 | return null; |
||
567 | }, |
||
568 | ], |
||
569 | ], |
||
570 | ]); |
||
571 | |||
572 | $schema = new Schema(['query' => $blogQuery]); |
||
573 | $result = GraphQL::executeQuery($schema, $doc)->toArray(); |
||
574 | |||
575 | self::assertTrue($hasCalled); |
||
576 | self::assertEquals(['data' => ['article' => null]], $result); |
||
577 | self::assertEquals($expectedQueryPlan, $queryPlan->queryPlan()); |
||
578 | self::assertEquals($expectedReferencedTypes, $queryPlan->getReferencedTypes()); |
||
579 | self::assertEquals($expectedReferencedFields, $queryPlan->getReferencedFields()); |
||
580 | self::assertEquals(['url', 'width', 'height'], $queryPlan->subFields('Image')); |
||
581 | |||
582 | self::assertTrue($queryPlan->hasField('url')); |
||
583 | self::assertFalse($queryPlan->hasField('test')); |
||
584 | |||
585 | self::assertTrue($queryPlan->hasType('Image')); |
||
586 | self::assertFalse($queryPlan->hasType('Test')); |
||
587 | } |
||
589 |