| Conditions | 1 |
| Paths | 1 |
| Total Lines | 166 |
| Code Lines | 89 |
| 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 |
||
| 16 | public function testFieldSelection() : void |
||
| 17 | { |
||
| 18 | $image = new ObjectType([ |
||
| 19 | 'name' => 'Image', |
||
| 20 | 'fields' => [ |
||
| 21 | 'url' => ['type' => Type::string()], |
||
| 22 | 'width' => ['type' => Type::int()], |
||
| 23 | 'height' => ['type' => Type::int()], |
||
| 24 | ], |
||
| 25 | ]); |
||
| 26 | |||
| 27 | $article = null; |
||
| 28 | |||
| 29 | $author = new ObjectType([ |
||
| 30 | 'name' => 'Author', |
||
| 31 | 'fields' => static function () use ($image, &$article) { |
||
| 32 | return [ |
||
| 33 | 'id' => ['type' => Type::string()], |
||
| 34 | 'name' => ['type' => Type::string()], |
||
| 35 | 'pic' => [ |
||
| 36 | 'type' => $image, |
||
| 37 | 'args' => [ |
||
| 38 | 'width' => ['type' => Type::int()], |
||
| 39 | 'height' => ['type' => Type::int()], |
||
| 40 | ], |
||
| 41 | ], |
||
| 42 | 'recentArticle' => ['type' => $article], |
||
| 43 | ]; |
||
| 44 | }, |
||
| 45 | ]); |
||
| 46 | |||
| 47 | $reply = new ObjectType([ |
||
| 48 | 'name' => 'Reply', |
||
| 49 | 'fields' => [ |
||
| 50 | 'author' => ['type' => $author], |
||
| 51 | 'body' => ['type' => Type::string()], |
||
| 52 | ], |
||
| 53 | ]); |
||
| 54 | |||
| 55 | $article = new ObjectType([ |
||
| 56 | 'name' => 'Article', |
||
| 57 | 'fields' => [ |
||
| 58 | 'id' => ['type' => Type::string()], |
||
| 59 | 'isPublished' => ['type' => Type::boolean()], |
||
| 60 | 'author' => ['type' => $author], |
||
| 61 | 'title' => ['type' => Type::string()], |
||
| 62 | 'body' => ['type' => Type::string()], |
||
| 63 | 'image' => ['type' => $image], |
||
| 64 | 'replies' => ['type' => Type::listOf($reply)], |
||
| 65 | ], |
||
| 66 | ]); |
||
| 67 | |||
| 68 | $doc = ' |
||
| 69 | query Test { |
||
| 70 | article { |
||
| 71 | author { |
||
| 72 | name |
||
| 73 | pic { |
||
| 74 | url |
||
| 75 | width |
||
| 76 | } |
||
| 77 | } |
||
| 78 | image { |
||
| 79 | width |
||
| 80 | height |
||
| 81 | ...MyImage |
||
| 82 | } |
||
| 83 | replies { |
||
| 84 | body |
||
| 85 | author { |
||
| 86 | id |
||
| 87 | name |
||
| 88 | pic { |
||
| 89 | url |
||
| 90 | width |
||
| 91 | ... on Image { |
||
| 92 | height |
||
| 93 | } |
||
| 94 | } |
||
| 95 | recentArticle { |
||
| 96 | id |
||
| 97 | title |
||
| 98 | body |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | fragment MyImage on Image { |
||
| 105 | url |
||
| 106 | } |
||
| 107 | '; |
||
| 108 | $expectedDefaultSelection = [ |
||
| 109 | 'author' => true, |
||
| 110 | 'image' => true, |
||
| 111 | 'replies' => true, |
||
| 112 | ]; |
||
| 113 | $expectedDeepSelection = [ |
||
| 114 | 'author' => [ |
||
| 115 | 'name' => true, |
||
| 116 | 'pic' => [ |
||
| 117 | 'url' => true, |
||
| 118 | 'width' => true, |
||
| 119 | ], |
||
| 120 | ], |
||
| 121 | 'image' => [ |
||
| 122 | 'width' => true, |
||
| 123 | 'height' => true, |
||
| 124 | 'url' => true, |
||
| 125 | ], |
||
| 126 | 'replies' => [ |
||
| 127 | 'body' => true, |
||
| 128 | 'author' => [ |
||
| 129 | 'id' => true, |
||
| 130 | 'name' => true, |
||
| 131 | 'pic' => [ |
||
| 132 | 'url' => true, |
||
| 133 | 'width' => true, |
||
| 134 | 'height' => true, |
||
| 135 | ], |
||
| 136 | 'recentArticle' => [ |
||
| 137 | 'id' => true, |
||
| 138 | 'title' => true, |
||
| 139 | 'body' => true, |
||
| 140 | ], |
||
| 141 | ], |
||
| 142 | ], |
||
| 143 | ]; |
||
| 144 | |||
| 145 | $hasCalled = false; |
||
| 146 | $actualDefaultSelection = null; |
||
| 147 | $actualDeepSelection = null; |
||
| 148 | |||
| 149 | $blogQuery = new ObjectType([ |
||
| 150 | 'name' => 'Query', |
||
| 151 | 'fields' => [ |
||
| 152 | 'article' => [ |
||
| 153 | 'type' => $article, |
||
| 154 | 'resolve' => static function ( |
||
| 155 | $value, |
||
| 156 | $args, |
||
| 157 | $context, |
||
| 158 | ResolveInfo $info |
||
| 159 | ) use ( |
||
| 160 | &$hasCalled, |
||
| 161 | & |
||
| 162 | $actualDefaultSelection, |
||
| 163 | &$actualDeepSelection |
||
| 164 | ) { |
||
| 165 | $hasCalled = true; |
||
| 166 | $actualDefaultSelection = $info->getFieldSelection(); |
||
| 167 | $actualDeepSelection = $info->getFieldSelection(5); |
||
| 168 | |||
| 169 | return null; |
||
| 170 | }, |
||
| 171 | ], |
||
| 172 | ], |
||
| 173 | ]); |
||
| 174 | |||
| 175 | $schema = new Schema(['query' => $blogQuery]); |
||
| 176 | $result = GraphQL::executeQuery($schema, $doc)->toArray(); |
||
| 177 | |||
| 178 | self::assertTrue($hasCalled); |
||
| 179 | self::assertEquals(['data' => ['article' => null]], $result); |
||
| 180 | self::assertEquals($expectedDefaultSelection, $actualDefaultSelection); |
||
| 181 | self::assertEquals($expectedDeepSelection, $actualDeepSelection); |
||
| 182 | } |
||
| 382 |