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 |
||
411 | public function testMergedFragmentsQueryPlan() : void |
||
412 | { |
||
413 | $image = new ObjectType([ |
||
414 | 'name' => 'Image', |
||
415 | 'fields' => [ |
||
416 | 'url' => ['type' => Type::string()], |
||
417 | 'width' => ['type' => Type::int()], |
||
418 | 'height' => ['type' => Type::int()], |
||
419 | ], |
||
420 | ]); |
||
421 | |||
422 | $article = null; |
||
423 | |||
424 | $author = new ObjectType([ |
||
425 | 'name' => 'Author', |
||
426 | 'fields' => static function () use ($image, &$article) { |
||
427 | return [ |
||
428 | 'id' => ['type' => Type::string()], |
||
429 | 'name' => ['type' => Type::string()], |
||
430 | 'pic' => [ |
||
431 | 'type' => $image, |
||
432 | 'args' => [ |
||
433 | 'width' => ['type' => Type::int()], |
||
434 | 'height' => ['type' => Type::int()], |
||
435 | ], |
||
436 | ], |
||
437 | 'recentArticle' => ['type' => $article], |
||
438 | ]; |
||
439 | }, |
||
440 | ]); |
||
441 | |||
442 | $reply = new ObjectType([ |
||
443 | 'name' => 'Reply', |
||
444 | 'fields' => [ |
||
445 | 'author' => ['type' => $author], |
||
446 | 'body' => ['type' => Type::string()], |
||
447 | ], |
||
448 | ]); |
||
449 | |||
450 | $article = new ObjectType([ |
||
451 | 'name' => 'Article', |
||
452 | 'fields' => [ |
||
453 | 'id' => ['type' => Type::string()], |
||
454 | 'isPublished' => ['type' => Type::boolean()], |
||
455 | 'author' => ['type' => $author], |
||
456 | 'title' => ['type' => Type::string()], |
||
457 | 'body' => ['type' => Type::string()], |
||
458 | 'image' => ['type' => $image], |
||
459 | 'replies' => ['type' => Type::listOf($reply)], |
||
460 | ], |
||
461 | ]); |
||
462 | |||
463 | $doc = ' |
||
464 | query Test { |
||
465 | article { |
||
466 | author { |
||
467 | name |
||
468 | pic(width: 100, height: 200) { |
||
469 | url |
||
470 | width |
||
471 | } |
||
472 | } |
||
473 | image { |
||
474 | width |
||
475 | height |
||
476 | ...MyImage |
||
477 | } |
||
478 | ...Replies01 |
||
479 | ...Replies02 |
||
480 | } |
||
481 | } |
||
482 | fragment MyImage on Image { |
||
483 | url |
||
484 | } |
||
485 | |||
486 | fragment Replies01 on Article { |
||
487 | _replies012: replies { |
||
488 | body |
||
489 | } |
||
490 | } |
||
491 | fragment Replies02 on Article { |
||
492 | _replies012: replies { |
||
493 | author { |
||
494 | id |
||
495 | name |
||
496 | pic { |
||
497 | url |
||
498 | width |
||
499 | ... on Image { |
||
500 | height |
||
501 | } |
||
502 | } |
||
503 | recentArticle { |
||
504 | id |
||
505 | title |
||
506 | body |
||
507 | } |
||
508 | } |
||
509 | } |
||
510 | } |
||
511 | '; |
||
512 | |||
513 | $expectedQueryPlan = [ |
||
514 | 'author' => [ |
||
515 | 'type' => $author, |
||
516 | 'args' => [], |
||
517 | 'fields' => [ |
||
518 | 'name' => [ |
||
519 | 'type' => Type::string(), |
||
520 | 'args' => [], |
||
521 | 'fields' => [], |
||
522 | ], |
||
523 | 'pic' => [ |
||
524 | 'type' => $image, |
||
525 | 'args' => [ |
||
526 | 'width' => 100, |
||
527 | 'height' => 200, |
||
528 | ], |
||
529 | 'fields' => [ |
||
530 | 'url' => [ |
||
531 | 'type' => Type::string(), |
||
532 | 'args' => [], |
||
533 | 'fields' => [], |
||
534 | ], |
||
535 | 'width' => [ |
||
536 | 'type' => Type::int(), |
||
537 | 'args' => [], |
||
538 | 'fields' => [], |
||
539 | ], |
||
540 | ], |
||
541 | ], |
||
542 | ], |
||
543 | ], |
||
544 | 'image' => [ |
||
545 | 'type' => $image, |
||
546 | 'args' => [], |
||
547 | 'fields' => [ |
||
548 | 'url' => [ |
||
549 | 'type' => Type::string(), |
||
550 | 'args' => [], |
||
551 | 'fields' => [], |
||
552 | ], |
||
553 | 'width' => [ |
||
554 | 'type' => Type::int(), |
||
555 | 'args' => [], |
||
556 | 'fields' => [], |
||
557 | ], |
||
558 | 'height' => [ |
||
559 | 'type' => Type::int(), |
||
560 | 'args' => [], |
||
561 | 'fields' => [], |
||
562 | ], |
||
563 | ], |
||
564 | ], |
||
565 | 'replies' => [ |
||
566 | 'type' => Type::listOf($reply), |
||
567 | 'args' => [], |
||
568 | 'fields' => [ |
||
569 | 'body' => [ |
||
570 | 'type' => Type::string(), |
||
571 | 'args' => [], |
||
572 | 'fields' => [], |
||
573 | ], |
||
574 | 'author' => [ |
||
575 | 'type' => $author, |
||
576 | 'args' => [], |
||
577 | 'fields' => [ |
||
578 | 'id' => [ |
||
579 | 'type' => Type::string(), |
||
580 | 'args' => [], |
||
581 | 'fields' => [], |
||
582 | ], |
||
583 | 'name' => [ |
||
584 | 'type' => Type::string(), |
||
585 | 'args' => [], |
||
586 | 'fields' => [], |
||
587 | ], |
||
588 | 'pic' => [ |
||
589 | 'type' => $image, |
||
590 | 'args' => [], |
||
591 | 'fields' => [ |
||
592 | 'url' => [ |
||
593 | 'type' => Type::string(), |
||
594 | 'args' => [], |
||
595 | 'fields' => [], |
||
596 | ], |
||
597 | 'width' => [ |
||
598 | 'type' => Type::int(), |
||
599 | 'args' => [], |
||
600 | 'fields' => [], |
||
601 | ], |
||
602 | 'height' => [ |
||
603 | 'type' => Type::int(), |
||
604 | 'args' => [], |
||
605 | 'fields' => [], |
||
606 | ], |
||
607 | ], |
||
608 | ], |
||
609 | 'recentArticle' => [ |
||
610 | 'type' => $article, |
||
611 | 'args' => [], |
||
612 | 'fields' => [ |
||
613 | 'id' => [ |
||
614 | 'type' => Type::string(), |
||
615 | 'args' => [], |
||
616 | 'fields' => [], |
||
617 | ], |
||
618 | 'title' => [ |
||
619 | 'type' => Type::string(), |
||
620 | 'args' => [], |
||
621 | 'fields' => [], |
||
622 | ], |
||
623 | 'body' => [ |
||
624 | 'type' => Type::string(), |
||
625 | 'args' => [], |
||
626 | 'fields' => [], |
||
627 | ], |
||
628 | ], |
||
629 | ], |
||
630 | ], |
||
631 | ], |
||
632 | ], |
||
633 | ], |
||
634 | ]; |
||
635 | |||
636 | $expectedReferencedTypes = [ |
||
637 | 'Image', |
||
638 | 'Author', |
||
639 | 'Reply', |
||
640 | 'Article', |
||
641 | ]; |
||
642 | |||
643 | $expectedReferencedFields = [ |
||
644 | 'url', |
||
645 | 'width', |
||
646 | 'height', |
||
647 | 'name', |
||
648 | 'pic', |
||
649 | 'id', |
||
650 | 'recentArticle', |
||
651 | 'body', |
||
652 | 'author', |
||
653 | 'replies', |
||
654 | 'title', |
||
655 | 'image', |
||
656 | ]; |
||
657 | |||
658 | $hasCalled = false; |
||
659 | /** @var QueryPlan $queryPlan */ |
||
660 | $queryPlan = null; |
||
661 | |||
662 | $blogQuery = new ObjectType([ |
||
663 | 'name' => 'Query', |
||
664 | 'fields' => [ |
||
665 | 'article' => [ |
||
666 | 'type' => $article, |
||
667 | 'resolve' => static function ( |
||
668 | $value, |
||
669 | $args, |
||
670 | $context, |
||
671 | ResolveInfo $info |
||
672 | ) use ( |
||
673 | &$hasCalled, |
||
674 | &$queryPlan |
||
675 | ) { |
||
676 | $hasCalled = true; |
||
677 | $queryPlan = $info->lookAhead(); |
||
678 | |||
679 | return null; |
||
680 | }, |
||
681 | ], |
||
682 | ], |
||
683 | ]); |
||
684 | |||
685 | $schema = new Schema(['query' => $blogQuery]); |
||
686 | $result = GraphQL::executeQuery($schema, $doc)->toArray(); |
||
687 | |||
688 | self::assertTrue($hasCalled); |
||
689 | self::assertEquals(['data' => ['article' => null]], $result); |
||
690 | self::assertEquals($expectedQueryPlan, $queryPlan->queryPlan()); |
||
691 | self::assertEquals($expectedReferencedTypes, $queryPlan->getReferencedTypes()); |
||
692 | self::assertEquals($expectedReferencedFields, $queryPlan->getReferencedFields()); |
||
693 | self::assertEquals(['url', 'width', 'height'], $queryPlan->subFields('Image')); |
||
694 | |||
695 | self::assertTrue($queryPlan->hasField('url')); |
||
696 | self::assertFalse($queryPlan->hasField('test')); |
||
697 | |||
698 | self::assertTrue($queryPlan->hasType('Image')); |
||
699 | self::assertFalse($queryPlan->hasType('Test')); |
||
700 | } |
||
702 |