| Conditions | 2 |
| Paths | 1 |
| Total Lines | 55 |
| Code Lines | 36 |
| 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 |
||
| 39 | protected function setUp() |
||
| 40 | { |
||
| 41 | $this->personType = new ObjectType([ |
||
| 42 | 'name' => 'Person', |
||
| 43 | 'fields' => [ |
||
| 44 | 'firstName' => [ |
||
| 45 | 'type' => Type::string(), |
||
| 46 | 'phoneNumbers' => [ |
||
| 47 | 'type' => Type::listOf(Type::string()), |
||
| 48 | ], |
||
| 49 | ], |
||
| 50 | ], |
||
| 51 | ]); |
||
| 52 | |||
| 53 | $this->bookType = new ObjectType([ |
||
| 54 | 'name' => 'Book', |
||
| 55 | 'fields' => [ |
||
| 56 | 'title' => [ |
||
| 57 | 'type' => Type::string(), |
||
| 58 | 'resolve' => static function ($book) { |
||
| 59 | return $book['title']; |
||
| 60 | }, |
||
| 61 | ], |
||
| 62 | 'author' => [ |
||
| 63 | 'type' => $this->personType, |
||
| 64 | 'resolve' => static function ($book) { |
||
| 65 | return $book['author']; |
||
| 66 | }, |
||
| 67 | ], |
||
| 68 | ], |
||
| 69 | ]); |
||
| 70 | |||
| 71 | $this->schema = new Schema([ |
||
| 72 | 'mutation' => new ObjectType([ |
||
| 73 | 'name' => 'Mutation', |
||
| 74 | 'fields' => function () { |
||
| 75 | return [ |
||
| 76 | 'updateBook' => new ValidatedFieldDefinition([ |
||
| 77 | 'name' => 'updateBook', |
||
| 78 | 'type' => $this->bookType, |
||
| 79 | 'args' => [ |
||
| 80 | 'bookId' => [ |
||
| 81 | 'type' => Type::nonNull(Type::id()), |
||
| 82 | 'errorCodes' => ['bookNotFound'], |
||
| 83 | 'validate' => function ($bookId) { |
||
| 84 | if (isset($this->data['books'][$bookId])) { |
||
| 85 | return 0; |
||
| 86 | } |
||
| 87 | |||
| 88 | return ['bookNotFound', 'Unknown book!']; |
||
| 89 | }, |
||
| 90 | ], |
||
| 91 | ], |
||
| 92 | 'resolve' => function ($value, $args) : array { |
||
| 93 | return $this->data['books'][$args['bookId']]; |
||
| 94 | }, |
||
| 163 |