Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 29 | class ProcessorTest extends \PHPUnit_Framework_TestCase |
||
| 30 | { |
||
| 31 | |||
| 32 | private $_counter = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @expectedException \Youshido\GraphQL\Validator\Exception\ConfigurationException |
||
| 36 | * @expectedExceptionMessage Schema has to have fields |
||
| 37 | */ |
||
| 38 | public function testInit() |
||
| 42 | |||
| 43 | public function testEmptyQueries() |
||
| 57 | |||
| 58 | public function testNestedVariables() { |
||
| 85 | |||
| 86 | View Code Duplication | public function testListNullResponse() |
|
| 104 | |||
| 105 | |||
| 106 | View Code Duplication | public function testSubscriptionNullResponse() |
|
| 124 | |||
| 125 | public function testSchemaOperations() |
||
| 126 | { |
||
| 127 | $schema = new Schema([ |
||
| 128 | 'query' => new ObjectType([ |
||
| 129 | 'name' => 'RootQuery', |
||
| 130 | 'fields' => [ |
||
| 131 | 'me' => [ |
||
| 132 | 'type' => new ObjectType([ |
||
| 133 | 'name' => 'User', |
||
| 134 | 'fields' => [ |
||
| 135 | 'firstName' => [ |
||
| 136 | 'type' => new StringType(), |
||
| 137 | 'args' => [ |
||
| 138 | 'shorten' => new BooleanType() |
||
| 139 | ], |
||
| 140 | 'resolve' => function ($value, $args) { |
||
| 141 | return empty($args['shorten']) ? $value : $value; |
||
| 142 | } |
||
| 143 | ], |
||
| 144 | 'lastName' => new StringType(), |
||
| 145 | 'code' => new StringType(), |
||
| 146 | ] |
||
| 147 | ]), |
||
| 148 | View Code Duplication | 'resolve' => function ($value, $args) { |
|
| 149 | $data = ['firstName' => 'John', 'code' => '007']; |
||
| 150 | if (!empty($args['upper'])) { |
||
| 151 | foreach ($data as $key => $value) { |
||
| 152 | $data[$key] = strtoupper($value); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return $data; |
||
| 157 | }, |
||
| 158 | 'args' => [ |
||
| 159 | 'upper' => [ |
||
| 160 | 'type' => new BooleanType(), |
||
| 161 | 'default' => false |
||
| 162 | ] |
||
| 163 | ] |
||
| 164 | ], |
||
| 165 | 'randomUser' => [ |
||
| 166 | 'type' => new TestObjectType(), |
||
| 167 | 'resolve' => function () { |
||
| 168 | return ['invalidField' => 'John']; |
||
| 169 | } |
||
| 170 | ], |
||
| 171 | 'invalidValueQuery' => [ |
||
| 172 | 'type' => new TestObjectType(), |
||
| 173 | 'resolve' => function () { |
||
| 174 | return 'stringValue'; |
||
| 175 | } |
||
| 176 | ], |
||
| 177 | ], |
||
| 178 | ]) |
||
| 179 | ]); |
||
| 180 | $processor = new Processor($schema); |
||
| 181 | |||
| 182 | $processor->processPayload('{ me { firstName } }'); |
||
| 183 | $this->assertEquals(['data' => ['me' => ['firstName' => 'John']]], $processor->getResponseData()); |
||
| 184 | |||
| 185 | $processor->processPayload('{ me { firstName, lastName } }'); |
||
| 186 | $this->assertEquals(['data' => ['me' => ['firstName' => 'John', 'lastName' => null]]], $processor->getResponseData()); |
||
| 187 | |||
| 188 | $processor->processPayload('{ me { code } }'); |
||
| 189 | $this->assertEquals(['data' => ['me' => ['code' => 7]]], $processor->getResponseData()); |
||
| 190 | |||
| 191 | $processor->processPayload('{ me(upper:true) { firstName } }'); |
||
| 192 | $this->assertEquals(['data' => ['me' => ['firstName' => 'JOHN']]], $processor->getResponseData()); |
||
| 193 | |||
| 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([ |
||
| 208 | 'name' => 'invalidResolveTypeMutation', |
||
| 209 | 'type' => new NonNullType(new IntType()), |
||
| 210 | 'resolve' => function () { |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | ]))->addField(new Field([ |
||
| 214 | 'name' => 'interfacedMutation', |
||
| 215 | 'type' => new TestInterfaceType(), |
||
| 216 | 'resolve' => function () { |
||
| 217 | return ['name' => 'John']; |
||
| 218 | } |
||
| 219 | ])); |
||
| 220 | $processor->processPayload('mutation { increaseCounter }'); |
||
| 221 | $this->assertEquals(['data' => ['increaseCounter' => 1]], $processor->getResponseData()); |
||
| 222 | |||
| 223 | $processor->processPayload('mutation { invalidMutation }'); |
||
| 224 | $this->assertEquals(['errors' => [['message' => 'Field "invalidMutation" not found in type "RootSchemaMutation"']]], $processor->getResponseData()); |
||
| 225 | |||
| 226 | $processor->processPayload('mutation { increaseCounter(noArg: 2) }'); |
||
| 227 | $this->assertEquals(['errors' => [['message' => 'Unknown argument "noArg" on field "increaseCounter"']]], $processor->getResponseData()); |
||
| 228 | |||
| 229 | $processor->processPayload('mutation { increaseCounter(amount: 2) { invalidProp } }'); |
||
| 230 | $this->assertEquals(['errors' => [['message' => 'Field "invalidProp" not found in type "Int"']], 'data' => ['increaseCounter' => null]], $processor->getResponseData()); |
||
| 231 | |||
| 232 | $processor->processPayload('mutation { increaseCounter(amount: 2) }'); |
||
| 233 | $this->assertEquals(['data' => ['increaseCounter' => 5]], $processor->getResponseData()); |
||
| 234 | |||
| 235 | $processor->processPayload('{ invalidQuery }'); |
||
| 236 | $this->assertEquals(['errors' => [['message' => 'Field "invalidQuery" not found in type "RootQuery"']]], $processor->getResponseData()); |
||
| 237 | |||
| 238 | $processor->processPayload('{ invalidValueQuery { id } }'); |
||
| 239 | $this->assertEquals(['errors' => [['message' => 'Not valid value for OBJECT field invalidValueQuery']], 'data' => ['invalidValueQuery' => null]], $processor->getResponseData()); |
||
| 240 | |||
| 241 | $processor->processPayload('{ me { firstName(shorten: true), middle }}'); |
||
| 242 | $this->assertEquals(['errors' => [['message' => 'Field "middle" not found in type "User"']], 'data' => ['me' => null]], $processor->getResponseData()); |
||
| 243 | |||
| 244 | $processor->processPayload('{ randomUser { region }}'); |
||
| 245 | $this->assertEquals(['errors' => [['message' => 'Property "region" not found in resolve result']]], $processor->getResponseData()); |
||
| 246 | |||
| 247 | $processor->processPayload('mutation { invalidResolveTypeMutation }'); |
||
| 248 | $this->assertEquals(['errors' => [['message' => 'Cannot return null for non-nullable field invalidResolveTypeMutation']], 'data' => ['invalidResolveTypeMutation' => null]], $processor->getResponseData()); |
||
| 249 | |||
| 250 | $processor->processPayload('mutation { user:interfacedMutation { name } }'); |
||
| 251 | $this->assertEquals(['data' => ['user' => ['name' => 'John']]], $processor->getResponseData()); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function testListEnumsSchemaOperations() |
||
| 354 | |||
| 355 | public function testTypedFragment() |
||
| 450 | |||
| 451 | public function testComplexityReducer() { |
||
| 516 | } |
||
| 517 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.