| Conditions | 1 |
| Paths | 1 |
| Total Lines | 80 |
| Code Lines | 54 |
| 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 |
||
| 20 | protected function buildSchema() |
||
| 21 | { |
||
| 22 | return new Schema([ |
||
| 23 | 'query' => new ObjectType([ |
||
| 24 | 'name' => 'Query', |
||
| 25 | 'fields' => [ |
||
| 26 | 'f1' => [ |
||
| 27 | 'type' => Type::string(), |
||
| 28 | 'resolve' => static function ($root, $args, $context, $info) { |
||
| 29 | return $info->fieldName; |
||
| 30 | }, |
||
| 31 | ], |
||
| 32 | 'fieldWithPhpError' => [ |
||
| 33 | 'type' => Type::string(), |
||
| 34 | 'resolve' => static function ($root, $args, $context, $info) { |
||
| 35 | trigger_error('deprecated', E_USER_DEPRECATED); |
||
| 36 | trigger_error('notice', E_USER_NOTICE); |
||
| 37 | trigger_error('warning', E_USER_WARNING); |
||
| 38 | $a = []; |
||
| 39 | $a['test']; // should produce PHP notice |
||
| 40 | |||
| 41 | return $info->fieldName; |
||
| 42 | }, |
||
| 43 | ], |
||
| 44 | 'fieldWithSafeException' => [ |
||
| 45 | 'type' => Type::string(), |
||
| 46 | 'resolve' => static function () { |
||
| 47 | throw new UserError('This is the exception we want'); |
||
| 48 | }, |
||
| 49 | ], |
||
| 50 | 'fieldWithUnsafeException' => [ |
||
| 51 | 'type' => Type::string(), |
||
| 52 | 'resolve' => static function () { |
||
| 53 | throw new Unsafe('This exception should not be shown to the user'); |
||
| 54 | }, |
||
| 55 | ], |
||
| 56 | 'testContextAndRootValue' => [ |
||
| 57 | 'type' => Type::string(), |
||
| 58 | 'resolve' => static function ($root, $args, $context, $info) { |
||
| 59 | $context->testedRootValue = $root; |
||
| 60 | |||
| 61 | return $info->fieldName; |
||
| 62 | }, |
||
| 63 | ], |
||
| 64 | 'fieldWithArg' => [ |
||
| 65 | 'type' => Type::string(), |
||
| 66 | 'args' => [ |
||
| 67 | 'arg' => [ |
||
| 68 | 'type' => Type::nonNull(Type::string()), |
||
| 69 | ], |
||
| 70 | ], |
||
| 71 | 'resolve' => static function ($root, $args) { |
||
| 72 | return $args['arg']; |
||
| 73 | }, |
||
| 74 | ], |
||
| 75 | 'dfd' => [ |
||
| 76 | 'type' => Type::string(), |
||
| 77 | 'args' => [ |
||
| 78 | 'num' => [ |
||
| 79 | 'type' => Type::nonNull(Type::int()), |
||
| 80 | ], |
||
| 81 | ], |
||
| 82 | 'resolve' => static function ($root, $args, $context) { |
||
| 83 | $context['buffer']($args['num']); |
||
| 84 | |||
| 85 | return new Deferred(static function () use ($args, $context) { |
||
| 86 | return $context['load']($args['num']); |
||
| 87 | }); |
||
| 88 | }, |
||
| 89 | ], |
||
| 90 | ], |
||
| 91 | ]), |
||
| 92 | 'mutation' => new ObjectType([ |
||
| 93 | 'name' => 'Mutation', |
||
| 94 | 'fields' => [ |
||
| 95 | 'm1' => [ |
||
| 96 | 'type' => new ObjectType([ |
||
| 97 | 'name' => 'TestMutation', |
||
| 98 | 'fields' => [ |
||
| 99 | 'result' => Type::string(), |
||
| 100 | ], |
||
| 108 |