Completed
Pull Request — master (#22)
by
unknown
03:33
created
src/Execution/Visitor/MaxComplexityQueryVisitor.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@
 block discarded – undo
24 24
     }
25 25
     $this->memo += $cost ?: $this->defaultScore;
26 26
     if ($this->memo > $this->maxScore) {
27
-      throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore);
27
+      throw new \Exception('query exceeded max allowed complexity of '.$this->maxScore);
28 28
     }
29 29
     return $this->memo;
30 30
   }
Please login to merge, or discard this patch.
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -16,34 +16,34 @@
 block discarded – undo
16 16
 
17 17
 class MaxComplexityQueryVisitor extends AbstractQueryVisitor {
18 18
 
19
-  public $maxScore;
19
+    public $maxScore;
20 20
 
21
-  protected $defaultScore = 1;
21
+    protected $defaultScore = 1;
22 22
 
23
-  public function __construct($max) {
23
+    public function __construct($max) {
24 24
     parent::__construct();
25 25
 
26 26
     $this->maxScore = $max;
27
-  }
28
-
29
-  /**
30
-   * @param array       $args
31
-   * @param FieldConfig $fieldConfig
32
-   * @param int         $childScore
33
-   *
34
-   * @return int|null
35
-   * @throws \Exception
36
-   */
37
-  public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) {
27
+    }
28
+
29
+    /**
30
+     * @param array       $args
31
+     * @param FieldConfig $fieldConfig
32
+     * @param int         $childScore
33
+     *
34
+     * @return int|null
35
+     * @throws \Exception
36
+     */
37
+    public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0) {
38 38
     $cost = $fieldConfig->get('cost');
39 39
     if (is_callable($cost)) {
40
-      $cost = $cost($args, $fieldConfig, $childScore);
40
+        $cost = $cost($args, $fieldConfig, $childScore);
41 41
     }
42 42
     $cost = $cost ?: $this->defaultScore;
43 43
     $this->memo += $cost;
44 44
     if ($this->memo > $this->maxScore) {
45
-      throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore);
45
+        throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore);
46 46
     }
47 47
     return $cost;
48
-  }
48
+    }
49 49
 }
50 50
\ No newline at end of file
Please login to merge, or discard this patch.
src/Execution/Processor.php 1 patch
Doc Comments   +6 added lines, -3 removed lines patch added patch discarded remove patch
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
     }
110 110
 
111 111
     /**
112
-     * @param Query|Field        $query
112
+     * @param Query        $query
113 113
      * @param AbstractObjectType $currentLevelSchema
114 114
      * @return array|bool|mixed
115 115
      */
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
     }
313 313
 
314 314
     /**
315
-     * @param $query         Query|FragmentInterface
315
+     * @param Query $query         Query|FragmentInterface
316 316
      * @param $queryType     AbstractObjectType|TypeInterface|Field|AbstractType
317 317
      * @param $resolvedValue mixed
318 318
      * @param $value         array
@@ -415,6 +415,9 @@  discard block
 block discarded – undo
415 415
         return $result;
416 416
     }
417 417
 
418
+    /**
419
+     * @param integer $max
420
+     */
418 421
     public function setMaxComplexity($max) {
419 422
         $this->maxComplexity = $max;
420 423
     }
@@ -470,7 +473,7 @@  discard block
 block discarded – undo
470 473
      *
471 474
      * childScore costs are accumulated via values sent into the coroutine.
472 475
      *
473
-     * @param Query|Field|FragmentInterface $queryNode
476
+     * @param Query $queryNode
474 477
      * @param AbstractField                 $currentLevelAST
475 478
      *
476 479
      * @return \Generator
Please login to merge, or discard this patch.
Tests/Schema/ProcessorTest.php 2 patches
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 
56 56
     }
57 57
 
58
-  public function testNestedVariables() {
58
+    public function testNestedVariables() {
59 59
     $processor = new Processor(new TestSchema());
60 60
     $noArgsQuery = '{ me { echo(value:"foo") } }';
61 61
     $expectedData = ['data' => ['me' => ['echo' => 'foo']]];
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
         }';
82 82
     $processor->processPayload($parameterizedQueryQuery, ['value' => 1]);
83 83
     $this->assertArrayNotHasKey('errors', $processor->getResponseData());
84
-  }
84
+    }
85 85
 
86 86
     public function testListNullResponse()
87 87
     {
@@ -192,19 +192,19 @@  discard block
 block discarded – undo
192 192
         $this->assertEquals(['data' => ['me' => ['firstName' => 'JOHN']]], $processor->getResponseData());
193 193
 
194 194
         $schema->getMutationType()
195
-               ->addField(new Field([
196
-                   'name'    => 'increaseCounter',
197
-                   'type'    => new IntType(),
198
-                   'resolve' => function ($value, $args, ResolveInfo $info) {
199
-                       return $this->_counter += $args['amount'];
200
-                   },
201
-                   'args'    => [
202
-                       'amount' => [
203
-                           'type'    => new IntType(),
204
-                           'default' => 1
205
-                       ]
206
-                   ]
207
-               ]))->addField(new Field([
195
+                ->addField(new Field([
196
+                    'name'    => 'increaseCounter',
197
+                    'type'    => new IntType(),
198
+                    'resolve' => function ($value, $args, ResolveInfo $info) {
199
+                        return $this->_counter += $args['amount'];
200
+                    },
201
+                    'args'    => [
202
+                        'amount' => [
203
+                            'type'    => new IntType(),
204
+                            'default' => 1
205
+                        ]
206
+                    ]
207
+                ]))->addField(new Field([
208 208
                 'name'    => 'invalidResolveTypeMutation',
209 209
                 'type'    => new NonNullType(new IntType()),
210 210
                 'resolve' => function () {
@@ -462,82 +462,82 @@  discard block
 block discarded – undo
462 462
     }
463 463
 
464 464
     public function testComplexityReducer() {
465
-      $schema = new Schema(
466
-          [
467
-              'query' => new ObjectType(
468
-                  [
469
-                      'name'   => 'RootQuery',
470
-                      'fields' => [
471
-                          'me' => [
472
-                              'type'    => new ObjectType(
473
-                                  [
474
-                                      'name'   => 'User',
475
-                                      'fields' => [
476
-                                          'firstName' => [
477
-                                              'type'    => new StringType(),
478
-                                              'args'    => [
479
-                                                  'shorten' => new BooleanType()
480
-                                              ],
481
-                                              'resolve' => function ($value, $args) {
465
+        $schema = new Schema(
466
+            [
467
+                'query' => new ObjectType(
468
+                    [
469
+                        'name'   => 'RootQuery',
470
+                        'fields' => [
471
+                            'me' => [
472
+                                'type'    => new ObjectType(
473
+                                    [
474
+                                        'name'   => 'User',
475
+                                        'fields' => [
476
+                                            'firstName' => [
477
+                                                'type'    => new StringType(),
478
+                                                'args'    => [
479
+                                                    'shorten' => new BooleanType()
480
+                                                ],
481
+                                                'resolve' => function ($value, $args) {
482 482
                                                 return empty($args['shorten']) ? $value : $value;
483
-                                              }
484
-                                          ],
485
-                                          'lastName'  => new StringType(),
486
-                                          'code'      => new StringType(),
487
-                                          'likes'     => [
488
-                                              'type'    => new IntType(),
489
-                                              'cost'    => 10,
490
-                                              'resolve' => function () {
483
+                                                }
484
+                                            ],
485
+                                            'lastName'  => new StringType(),
486
+                                            'code'      => new StringType(),
487
+                                            'likes'     => [
488
+                                                'type'    => new IntType(),
489
+                                                'cost'    => 10,
490
+                                                'resolve' => function () {
491 491
                                                 return 42;
492
-                                              }
493
-                                          ]
494
-                                      ]
495
-                                  ]
496
-                              ),
497
-                              'cost' => function ($args, $context, $childCost) {
492
+                                                }
493
+                                            ]
494
+                                        ]
495
+                                    ]
496
+                                ),
497
+                                'cost' => function ($args, $context, $childCost) {
498 498
                                 $argsCost = isset($args['cost']) ? $args['cost'] : 1;
499 499
                                 return 1 + $argsCost * $childCost;
500
-                              },
501
-                              'resolve' => function ($value, $args) {
500
+                                },
501
+                                'resolve' => function ($value, $args) {
502 502
                                 $data = ['firstName' => 'John', 'code' => '007'];
503 503
 
504 504
                                 return $data;
505
-                              },
506
-                              'args'    => [
507
-                                  'cost' => [
508
-                                      'type'    => new IntType(),
509
-                                      'default' => 1
510
-                                  ]
511
-                              ]
512
-                          ]
513
-                      ]
514
-                  ]
515
-              )
516
-          ]
517
-      );
518
-      $processor = new Processor($schema);
519
-
520
-      $processor->setMaxComplexity(10);
521
-
522
-      $processor->processPayload('{ me { firstName, lastName } }');
523
-      $this->assertArrayNotHasKey('error', $processor->getResponseData());
524
-
525
-      $processor->processPayload('{ me { firstName, likes } }');
526
-      $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData());
527
-
528
-      // don't let complexity reducer affect query errors
529
-      $processor->processPayload('{ me { badfield } }');
530
-      $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData());
505
+                                },
506
+                                'args'    => [
507
+                                    'cost' => [
508
+                                        'type'    => new IntType(),
509
+                                        'default' => 1
510
+                                    ]
511
+                                ]
512
+                            ]
513
+                        ]
514
+                    ]
515
+                )
516
+            ]
517
+        );
518
+        $processor = new Processor($schema);
531 519
 
532
-      foreach (range(1,5) as $cost_multiplier) {
520
+        $processor->setMaxComplexity(10);
521
+
522
+        $processor->processPayload('{ me { firstName, lastName } }');
523
+        $this->assertArrayNotHasKey('error', $processor->getResponseData());
524
+
525
+        $processor->processPayload('{ me { firstName, likes } }');
526
+        $this->assertEquals(['errors' => [['message' => 'query exceeded max allowed complexity of 10']]], $processor->getResponseData());
527
+
528
+        // don't let complexity reducer affect query errors
529
+        $processor->processPayload('{ me { badfield } }');
530
+        $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData());
531
+
532
+        foreach (range(1,5) as $cost_multiplier) {
533 533
         $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost
534 534
         $processor->processPayload("{ me (cost: $cost_multiplier) { firstName, lastName, code, likes } }", ['cost' => $cost_multiplier], [$visitor]);
535 535
         $expected = 1 + 13 * (1 + $cost_multiplier);
536 536
         $this->assertEquals($expected, $visitor->getMemo());
537
-      }
537
+        }
538 538
 
539
-      // TODO, variables not yet supported
540
-      /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }';
539
+        // TODO, variables not yet supported
540
+        /*$query = 'query costQuery ($cost: Int) { me (cost: $cost) { firstName, lastName, code, likes } }';
541 541
       foreach (range(1,5) as $cost_multiplier) {
542 542
         $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost
543 543
         $processor->processPayload($query, ['cost' => $cost_multiplier], [$visitor]);
Please login to merge, or discard this patch.
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
 
125 125
     public function testSchemaOperations()
126 126
     {
127
-        $schema    = new Schema([
127
+        $schema = new Schema([
128 128
             'query' => new ObjectType([
129 129
                 'name'   => 'RootQuery',
130 130
                 'fields' => [
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
                                     'args'    => [
138 138
                                         'shorten' => new BooleanType()
139 139
                                     ],
140
-                                    'resolve' => function ($value, $args) {
140
+                                    'resolve' => function($value, $args) {
141 141
                                         return empty($args['shorten']) ? $value : $value;
142 142
                                     }
143 143
                                 ],
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
                                 'code'      => new StringType(),
146 146
                             ]
147 147
                         ]),
148
-                        'resolve' => function ($value, $args) {
148
+                        'resolve' => function($value, $args) {
149 149
                             $data = ['firstName' => 'John', 'code' => '007'];
150 150
                             if (!empty($args['upper'])) {
151 151
                                 foreach ($data as $key => $value) {
@@ -164,13 +164,13 @@  discard block
 block discarded – undo
164 164
                     ],
165 165
                     'randomUser'        => [
166 166
                         'type'    => new TestObjectType(),
167
-                        'resolve' => function () {
167
+                        'resolve' => function() {
168 168
                             return ['invalidField' => 'John'];
169 169
                         }
170 170
                     ],
171 171
                     'invalidValueQuery' => [
172 172
                         'type'    => new TestObjectType(),
173
-                        'resolve' => function () {
173
+                        'resolve' => function() {
174 174
                             return 'stringValue';
175 175
                         }
176 176
                     ],
@@ -195,7 +195,7 @@  discard block
 block discarded – undo
195 195
                ->addField(new Field([
196 196
                    'name'    => 'increaseCounter',
197 197
                    'type'    => new IntType(),
198
-                   'resolve' => function ($value, $args, ResolveInfo $info) {
198
+                   'resolve' => function($value, $args, ResolveInfo $info) {
199 199
                        return $this->_counter += $args['amount'];
200 200
                    },
201 201
                    'args'    => [
@@ -207,13 +207,13 @@  discard block
 block discarded – undo
207 207
                ]))->addField(new Field([
208 208
                 'name'    => 'invalidResolveTypeMutation',
209 209
                 'type'    => new NonNullType(new IntType()),
210
-                'resolve' => function () {
210
+                'resolve' => function() {
211 211
                     return null;
212 212
                 }
213 213
             ]))->addField(new Field([
214 214
                 'name'    => 'interfacedMutation',
215 215
                 'type'    => new TestInterfaceType(),
216
-                'resolve' => function () {
216
+                'resolve' => function() {
217 217
                     return ['name' => 'John'];
218 218
                 }
219 219
             ]));
@@ -259,43 +259,43 @@  discard block
 block discarded – undo
259 259
                 'fields' => [
260 260
                     'listQuery'                 => [
261 261
                         'type'    => new ListType(new TestEnumType()),
262
-                        'resolve' => function () {
262
+                        'resolve' => function() {
263 263
                             return 'invalid list';
264 264
                         }
265 265
                     ],
266 266
                     'listEnumQuery'             => [
267 267
                         'type'    => new ListType(new TestEnumType()),
268
-                        'resolve' => function () {
268
+                        'resolve' => function() {
269 269
                             return ['invalid enum'];
270 270
                         }
271 271
                     ],
272 272
                     'invalidEnumQuery'          => [
273 273
                         'type'    => new TestEnumType(),
274
-                        'resolve' => function () {
274
+                        'resolve' => function() {
275 275
                             return 'invalid enum';
276 276
                         }
277 277
                     ],
278 278
                     'enumQuery'                 => [
279 279
                         'type'    => new TestEnumType(),
280
-                        'resolve' => function () {
280
+                        'resolve' => function() {
281 281
                             return 1;
282 282
                         }
283 283
                     ],
284 284
                     'invalidNonNullQuery'       => [
285 285
                         'type'    => new NonNullType(new IntType()),
286
-                        'resolve' => function () {
286
+                        'resolve' => function() {
287 287
                             return null;
288 288
                         }
289 289
                     ],
290 290
                     'invalidNonNullInsideQuery' => [
291 291
                         'type'    => new NonNullType(new IntType()),
292
-                        'resolve' => function () {
292
+                        'resolve' => function() {
293 293
                             return 'hello';
294 294
                         }
295 295
                     ],
296 296
                     'objectQuery'               => [
297 297
                         'type'    => new TestObjectType(),
298
-                        'resolve' => function () {
298
+                        'resolve' => function() {
299 299
                             return ['name' => 'John'];
300 300
                         }
301 301
                     ],
@@ -307,7 +307,7 @@  discard block
 block discarded – undo
307 307
                                 'enum'   => new TestEnumType(),
308 308
                             ],
309 309
                         ]),
310
-                        'resolve' => function () {
310
+                        'resolve' => function() {
311 311
                             return [
312 312
                                 'object' => [
313 313
                                     'name' => 'John'
@@ -376,10 +376,10 @@  discard block
 block discarded – undo
376 376
             ]
377 377
         ]);
378 378
 
379
-        $union        = new UnionType([
379
+        $union = new UnionType([
380 380
             'name'        => 'TestUnion',
381 381
             'types'       => [$object1, $object2],
382
-            'resolveType' => function ($object) use ($object1, $object2) {
382
+            'resolveType' => function($object) use ($object1, $object2) {
383 383
                 if (isset($object['id'])) {
384 384
                     return $object1;
385 385
                 }
@@ -390,11 +390,11 @@  discard block
 block discarded – undo
390 390
         $invalidUnion = new UnionType([
391 391
             'name'        => 'TestUnion',
392 392
             'types'       => [$object1, $object2],
393
-            'resolveType' => function ($object) use ($object3) {
393
+            'resolveType' => function($object) use ($object3) {
394 394
                 return $object3;
395 395
             }
396 396
         ]);
397
-        $processor    = new Processor(new Schema([
397
+        $processor = new Processor(new Schema([
398 398
             'query' => new ObjectType([
399 399
                 'name'   => 'RootQuery',
400 400
                 'fields' => [
@@ -404,7 +404,7 @@  discard block
 block discarded – undo
404 404
                             'type' => ['type' => 'string']
405 405
                         ],
406 406
                         'cost' => 10,
407
-                        'resolve' => function ($value, $args) {
407
+                        'resolve' => function($value, $args) {
408 408
                             if ($args['type'] == 'object1') {
409 409
                                 return [
410 410
                                     'id' => 43
@@ -418,7 +418,7 @@  discard block
 block discarded – undo
418 418
                     ],
419 419
                     'invalidUnion' => [
420 420
                         'type'    => $invalidUnion,
421
-                        'resolve' => function () {
421
+                        'resolve' => function() {
422 422
                             return ['name' => 'name resolved'];
423 423
                         }
424 424
                     ],
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
                                               'args'    => [
479 479
                                                   'shorten' => new BooleanType()
480 480
                                               ],
481
-                                              'resolve' => function ($value, $args) {
481
+                                              'resolve' => function($value, $args) {
482 482
                                                 return empty($args['shorten']) ? $value : $value;
483 483
                                               }
484 484
                                           ],
@@ -487,18 +487,18 @@  discard block
 block discarded – undo
487 487
                                           'likes'     => [
488 488
                                               'type'    => new IntType(),
489 489
                                               'cost'    => 10,
490
-                                              'resolve' => function () {
490
+                                              'resolve' => function() {
491 491
                                                 return 42;
492 492
                                               }
493 493
                                           ]
494 494
                                       ]
495 495
                                   ]
496 496
                               ),
497
-                              'cost' => function ($args, $context, $childCost) {
497
+                              'cost' => function($args, $context, $childCost) {
498 498
                                 $argsCost = isset($args['cost']) ? $args['cost'] : 1;
499 499
                                 return 1 + $argsCost * $childCost;
500 500
                               },
501
-                              'resolve' => function ($value, $args) {
501
+                              'resolve' => function($value, $args) {
502 502
                                 $data = ['firstName' => 'John', 'code' => '007'];
503 503
 
504 504
                                 return $data;
@@ -529,7 +529,7 @@  discard block
 block discarded – undo
529 529
       $processor->processPayload('{ me { badfield } }');
530 530
       $this->assertArraySubset(['errors' => [['message' => 'Field "badfield" not found in type "User"']]], $processor->getResponseData());
531 531
 
532
-      foreach (range(1,5) as $cost_multiplier) {
532
+      foreach (range(1, 5) as $cost_multiplier) {
533 533
         $visitor = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor(1000); // arbitrarily high cost
534 534
         $processor->processPayload("{ me (cost: $cost_multiplier) { firstName, lastName, code, likes } }", ['cost' => $cost_multiplier], [$visitor]);
535 535
         $expected = 1 + 13 * (1 + $cost_multiplier);
Please login to merge, or discard this patch.
src/Execution/Visitor/AbstractQueryVisitor.php 1 patch
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -13,24 +13,24 @@
 block discarded – undo
13 13
 
14 14
 abstract class AbstractQueryVisitor {
15 15
 
16
-  protected $initialValue = 0;
16
+    protected $initialValue = 0;
17 17
 
18
-  protected $memo;
18
+    protected $memo;
19 19
 
20
-  public function __construct() {
20
+    public function __construct() {
21 21
     $this->memo = $this->initialValue;
22
-  }
22
+    }
23 23
 
24
-  public function getMemo() {
24
+    public function getMemo() {
25 25
     return $this->memo;
26
-  }
27
-
28
-  /**
29
-   * @param array       $args
30
-   * @param FieldConfig $fieldConfig
31
-   * @param int         $childScore
32
-   *
33
-   * @return int|null
34
-   */
35
-  abstract public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0);
26
+    }
27
+
28
+    /**
29
+     * @param array       $args
30
+     * @param FieldConfig $fieldConfig
31
+     * @param int         $childScore
32
+     *
33
+     * @return int|null
34
+     */
35
+    abstract public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0);
36 36
 }
37 37
\ No newline at end of file
Please login to merge, or discard this patch.