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