| Conditions | 1 |
| Paths | 1 |
| Total Lines | 279 |
| 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 |
||
| 19 | public function testQueryPlan() : void |
||
| 20 | { |
||
| 21 | $image = new ObjectType([ |
||
| 22 | 'name' => 'Image', |
||
| 23 | 'fields' => [ |
||
| 24 | 'url' => ['type' => Type::string()], |
||
| 25 | 'width' => ['type' => Type::int()], |
||
| 26 | 'height' => ['type' => Type::int()], |
||
| 27 | ], |
||
| 28 | ]); |
||
| 29 | |||
| 30 | $article = null; |
||
| 31 | |||
| 32 | $author = new ObjectType([ |
||
| 33 | 'name' => 'Author', |
||
| 34 | 'fields' => static function () use ($image, &$article) { |
||
| 35 | return [ |
||
| 36 | 'id' => ['type' => Type::string()], |
||
| 37 | 'name' => ['type' => Type::string()], |
||
| 38 | 'pic' => [ |
||
| 39 | 'type' => $image, |
||
| 40 | 'args' => [ |
||
| 41 | 'width' => ['type' => Type::int()], |
||
| 42 | 'height' => ['type' => Type::int()], |
||
| 43 | ], |
||
| 44 | ], |
||
| 45 | 'recentArticle' => ['type' => $article], |
||
| 46 | ]; |
||
| 47 | }, |
||
| 48 | ]); |
||
| 49 | |||
| 50 | $reply = new ObjectType([ |
||
| 51 | 'name' => 'Reply', |
||
| 52 | 'fields' => [ |
||
| 53 | 'author' => ['type' => $author], |
||
| 54 | 'body' => ['type' => Type::string()], |
||
| 55 | ], |
||
| 56 | ]); |
||
| 57 | |||
| 58 | $article = new ObjectType([ |
||
| 59 | 'name' => 'Article', |
||
| 60 | 'fields' => [ |
||
| 61 | 'id' => ['type' => Type::string()], |
||
| 62 | 'isPublished' => ['type' => Type::boolean()], |
||
| 63 | 'author' => ['type' => $author], |
||
| 64 | 'title' => ['type' => Type::string()], |
||
| 65 | 'body' => ['type' => Type::string()], |
||
| 66 | 'image' => ['type' => $image], |
||
| 67 | 'replies' => ['type' => Type::listOf($reply)], |
||
| 68 | ], |
||
| 69 | ]); |
||
| 70 | |||
| 71 | $doc = ' |
||
| 72 | query Test { |
||
| 73 | article { |
||
| 74 | author { |
||
| 75 | name |
||
| 76 | pic(width: 100, height: 200) { |
||
| 77 | url |
||
| 78 | width |
||
| 79 | } |
||
| 80 | } |
||
| 81 | image { |
||
| 82 | width |
||
| 83 | height |
||
| 84 | ...MyImage |
||
| 85 | } |
||
| 86 | replies { |
||
| 87 | body |
||
| 88 | author { |
||
| 89 | id |
||
| 90 | name |
||
| 91 | pic { |
||
| 92 | url |
||
| 93 | width |
||
| 94 | ... on Image { |
||
| 95 | height |
||
| 96 | } |
||
| 97 | } |
||
| 98 | recentArticle { |
||
| 99 | id |
||
| 100 | title |
||
| 101 | body |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | fragment MyImage on Image { |
||
| 108 | url |
||
| 109 | } |
||
| 110 | '; |
||
| 111 | $expectedQueryPlan = [ |
||
| 112 | 'author' => [ |
||
| 113 | 'type' => $author, |
||
| 114 | 'args' => [], |
||
| 115 | 'fields' => [ |
||
| 116 | 'name' => [ |
||
| 117 | 'type' => Type::string(), |
||
| 118 | 'args' => [], |
||
| 119 | 'fields' => [], |
||
| 120 | ], |
||
| 121 | 'pic' => [ |
||
| 122 | 'type' => $image, |
||
| 123 | 'args' => [ |
||
| 124 | 'width' => 100, |
||
| 125 | 'height' => 200, |
||
| 126 | ], |
||
| 127 | 'fields' => [ |
||
| 128 | 'url' => [ |
||
| 129 | 'type' => Type::string(), |
||
| 130 | 'args' => [], |
||
| 131 | 'fields' => [], |
||
| 132 | ], |
||
| 133 | 'width' => [ |
||
| 134 | 'type' => Type::int(), |
||
| 135 | 'args' => [], |
||
| 136 | 'fields' => [], |
||
| 137 | ], |
||
| 138 | ], |
||
| 139 | ], |
||
| 140 | ], |
||
| 141 | ], |
||
| 142 | 'image' => [ |
||
| 143 | 'type' => $image, |
||
| 144 | 'args' => [], |
||
| 145 | 'fields' => [ |
||
| 146 | 'url' => [ |
||
| 147 | 'type' => Type::string(), |
||
| 148 | 'args' => [], |
||
| 149 | 'fields' => [], |
||
| 150 | ], |
||
| 151 | 'width' => [ |
||
| 152 | 'type' => Type::int(), |
||
| 153 | 'args' => [], |
||
| 154 | 'fields' => [], |
||
| 155 | ], |
||
| 156 | 'height' => [ |
||
| 157 | 'type' => Type::int(), |
||
| 158 | 'args' => [], |
||
| 159 | 'fields' => [], |
||
| 160 | ], |
||
| 161 | ], |
||
| 162 | ], |
||
| 163 | 'replies' => [ |
||
| 164 | 'type' => Type::listOf($reply), |
||
| 165 | 'args' => [], |
||
| 166 | 'fields' => [ |
||
| 167 | 'body' => [ |
||
| 168 | 'type' => Type::string(), |
||
| 169 | 'args' => [], |
||
| 170 | 'fields' => [], |
||
| 171 | ], |
||
| 172 | 'author' => [ |
||
| 173 | 'type' => $author, |
||
| 174 | 'args' => [], |
||
| 175 | 'fields' => [ |
||
| 176 | 'id' => [ |
||
| 177 | 'type' => Type::string(), |
||
| 178 | 'args' => [], |
||
| 179 | 'fields' => [], |
||
| 180 | ], |
||
| 181 | 'name' => [ |
||
| 182 | 'type' => Type::string(), |
||
| 183 | 'args' => [], |
||
| 184 | 'fields' => [], |
||
| 185 | ], |
||
| 186 | 'pic' => [ |
||
| 187 | 'type' => $image, |
||
| 188 | 'args' => [], |
||
| 189 | 'fields' => [ |
||
| 190 | 'url' => [ |
||
| 191 | 'type' => Type::string(), |
||
| 192 | 'args' => [], |
||
| 193 | 'fields' => [], |
||
| 194 | ], |
||
| 195 | 'width' => [ |
||
| 196 | 'type' => Type::int(), |
||
| 197 | 'args' => [], |
||
| 198 | 'fields' => [], |
||
| 199 | ], |
||
| 200 | 'height' => [ |
||
| 201 | 'type' => Type::int(), |
||
| 202 | 'args' => [], |
||
| 203 | 'fields' => [], |
||
| 204 | ], |
||
| 205 | ], |
||
| 206 | ], |
||
| 207 | 'recentArticle' => [ |
||
| 208 | 'type' => $article, |
||
| 209 | 'args' => [], |
||
| 210 | 'fields' => [ |
||
| 211 | 'id' => [ |
||
| 212 | 'type' => Type::string(), |
||
| 213 | 'args' => [], |
||
| 214 | 'fields' => [], |
||
| 215 | ], |
||
| 216 | 'title' => [ |
||
| 217 | 'type' => Type::string(), |
||
| 218 | 'args' => [], |
||
| 219 | 'fields' => [], |
||
| 220 | ], |
||
| 221 | 'body' => [ |
||
| 222 | 'type' => Type::string(), |
||
| 223 | 'args' => [], |
||
| 224 | 'fields' => [], |
||
| 225 | ], |
||
| 226 | ], |
||
| 227 | ], |
||
| 228 | ], |
||
| 229 | ], |
||
| 230 | ], |
||
| 231 | ], |
||
| 232 | ]; |
||
| 233 | |||
| 234 | $expectedReferencedTypes = [ |
||
| 235 | 'Image', |
||
| 236 | 'Author', |
||
| 237 | 'Article', |
||
| 238 | 'Reply', |
||
| 239 | ]; |
||
| 240 | |||
| 241 | $expectedReferencedFields = [ |
||
| 242 | 'url', |
||
| 243 | 'width', |
||
| 244 | 'height', |
||
| 245 | 'name', |
||
| 246 | 'pic', |
||
| 247 | 'id', |
||
| 248 | 'recentArticle', |
||
| 249 | 'title', |
||
| 250 | 'body', |
||
| 251 | 'author', |
||
| 252 | 'image', |
||
| 253 | 'replies', |
||
| 254 | ]; |
||
| 255 | |||
| 256 | $hasCalled = false; |
||
| 257 | /** @var QueryPlan $queryPlan */ |
||
| 258 | $queryPlan = null; |
||
| 259 | |||
| 260 | $blogQuery = new ObjectType([ |
||
| 261 | 'name' => 'Query', |
||
| 262 | 'fields' => [ |
||
| 263 | 'article' => [ |
||
| 264 | 'type' => $article, |
||
| 265 | 'resolve' => static function ( |
||
| 266 | $value, |
||
| 267 | $args, |
||
| 268 | $context, |
||
| 269 | ResolveInfo $info |
||
| 270 | ) use ( |
||
| 271 | &$hasCalled, |
||
| 272 | &$queryPlan |
||
| 273 | ) { |
||
| 274 | $hasCalled = true; |
||
| 275 | $queryPlan = $info->lookAhead(); |
||
| 276 | |||
| 277 | return null; |
||
| 278 | }, |
||
| 279 | ], |
||
| 280 | ], |
||
| 281 | ]); |
||
| 282 | |||
| 283 | $schema = new Schema(['query' => $blogQuery]); |
||
| 284 | $result = GraphQL::executeQuery($schema, $doc)->toArray(); |
||
| 285 | |||
| 286 | self::assertTrue($hasCalled); |
||
| 287 | self::assertEquals(['data' => ['article' => null]], $result); |
||
| 288 | self::assertEquals($expectedQueryPlan, $queryPlan->queryPlan()); |
||
| 289 | self::assertEquals($expectedReferencedTypes, $queryPlan->getReferencedTypes()); |
||
| 290 | self::assertEquals($expectedReferencedFields, $queryPlan->getReferencedFields()); |
||
| 291 | self::assertEquals(['url', 'width', 'height'], $queryPlan->subFields('Image')); |
||
| 292 | |||
| 293 | self::assertTrue($queryPlan->hasField('url')); |
||
| 294 | self::assertFalse($queryPlan->hasField('test')); |
||
| 295 | |||
| 296 | self::assertTrue($queryPlan->hasType('Image')); |
||
| 297 | self::assertFalse($queryPlan->hasType('Test')); |
||
| 298 | } |
||
| 702 |