| Conditions | 1 |
| Paths | 1 |
| Total Lines | 79 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 2 | Features | 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 |
||
| 62 | public function testCustomTypes() |
||
| 63 | { |
||
| 64 | $authorType = null; |
||
| 65 | |||
| 66 | $userInterface = new ObjectType([ |
||
| 67 | 'name' => 'UserInterface', |
||
| 68 | 'fields' => [ |
||
| 69 | 'name' => new StringType(), |
||
| 70 | ], |
||
| 71 | 'resolveType' => function () use ($authorType) { |
||
| 72 | return $authorType; |
||
| 73 | } |
||
| 74 | ]); |
||
| 75 | |||
| 76 | $authorType = new ObjectType([ |
||
| 77 | 'name' => 'Author', |
||
| 78 | 'fields' => [ |
||
| 79 | 'name' => new StringType(), |
||
| 80 | ], |
||
| 81 | 'interfaces' => [$userInterface] |
||
| 82 | ]); |
||
| 83 | |||
| 84 | $schema = new Schema([ |
||
| 85 | 'query' => new ObjectType([ |
||
| 86 | 'name' => 'QueryType', |
||
| 87 | 'fields' => [ |
||
| 88 | 'user' => [ |
||
| 89 | 'type' => $userInterface, |
||
| 90 | 'resolve' => function () { |
||
| 91 | return [ |
||
| 92 | 'name' => 'Alex' |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | ] |
||
| 96 | ] |
||
| 97 | ]) |
||
| 98 | ]); |
||
| 99 | $schema->getTypesList()->addType($authorType); |
||
| 100 | $processor = new Processor($schema); |
||
| 101 | $processor->processPayload('{ user { name } }'); |
||
| 102 | $this->assertEquals(['data' => ['user' => ['name' => 'Alex']]], $processor->getResponseData()); |
||
| 103 | |||
| 104 | $processor->processPayload('{ |
||
| 105 | __schema { |
||
| 106 | types { |
||
| 107 | name |
||
| 108 | } |
||
| 109 | } |
||
| 110 | }'); |
||
| 111 | $data = $processor->getResponseData(); |
||
| 112 | $this->assertArraySubset([10 => ['name' => 'Author']], $data['data']['__schema']['types']); |
||
| 113 | |||
| 114 | $processor->processPayload('{ user { name { } } }'); |
||
| 115 | $result = $processor->getResponseData(); |
||
| 116 | |||
| 117 | $this->assertEquals(['errors' => [[ |
||
| 118 | 'message' => 'Unexpected token "RBRACE"', |
||
| 119 | 'locations' => [ |
||
| 120 | [ |
||
| 121 | 'line' => 1, |
||
| 122 | 'column' => 19 |
||
| 123 | ] |
||
| 124 | ] |
||
| 125 | ]]], $result); |
||
| 126 | $processor->getExecutionContext()->clearErrors(); |
||
| 127 | |||
| 128 | $processor->processPayload('{ user { name { invalidSelection } } }'); |
||
| 129 | $result = $processor->getResponseData(); |
||
| 130 | |||
| 131 | $this->assertEquals(['data' => ['user' => null], 'errors' => [[ |
||
| 132 | 'message' => 'You can\'t specify fields for scalar type "String"', |
||
| 133 | 'locations' => [ |
||
| 134 | [ |
||
| 135 | 'line' => 1, |
||
| 136 | 'column' => 10 |
||
| 137 | ] |
||
| 138 | ] |
||
| 139 | ]]], $result); |
||
| 140 | } |
||
| 141 | |||
| 143 |