| Total Lines | 86 |
| Code Lines | 28 |
| 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 |
||
| 22 | public function setUp() |
||
| 23 | { |
||
| 24 | $typeMapper1 = new class() implements TypeMapperInterface { |
||
| 25 | public function mapClassToType(string $className, RecursiveTypeMapperInterface $recursiveTypeMapper): ObjectType |
||
| 26 | { |
||
| 27 | if ($className === TestObject::class) { |
||
| 28 | return new ObjectType([ |
||
| 29 | 'name' => 'TestObject', |
||
| 30 | 'fields' => [ |
||
| 31 | 'test' => Type::string(), |
||
| 32 | ], |
||
| 33 | ]); |
||
| 34 | } else { |
||
| 35 | throw TypeMappingException::createFromType(TestObject::class); |
||
|
|
|||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | public function mapClassToInputType(string $className): InputType |
||
| 40 | { |
||
| 41 | if ($className === TestObject::class) { |
||
| 42 | return new InputObjectType([ |
||
| 43 | 'name' => 'TestObject', |
||
| 44 | 'fields' => [ |
||
| 45 | 'test' => Type::string(), |
||
| 46 | ], |
||
| 47 | ]); |
||
| 48 | } else { |
||
| 49 | throw TypeMappingException::createFromType(TestObject::class); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | public function canMapClassToType(string $className): bool |
||
| 54 | { |
||
| 55 | return $className === TestObject::class; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function canMapClassToInputType(string $className): bool |
||
| 59 | { |
||
| 60 | return $className === TestObject::class; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the list of classes that have matching input GraphQL types. |
||
| 65 | * |
||
| 66 | * @return string[] |
||
| 67 | */ |
||
| 68 | public function getSupportedClasses(): array |
||
| 69 | { |
||
| 70 | return [TestObject::class]; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns a GraphQL type by name (can be either an input or output type) |
||
| 75 | * |
||
| 76 | * @param string $typeName The name of the GraphQL type |
||
| 77 | * @param RecursiveTypeMapperInterface $recursiveTypeMapper |
||
| 78 | * @return Type&(InputType|OutputType) |
||
| 79 | */ |
||
| 80 | public function mapNameToType(string $typeName, RecursiveTypeMapperInterface $recursiveTypeMapper): Type |
||
| 81 | { |
||
| 82 | switch ($typeName) { |
||
| 83 | case 'TestObject': |
||
| 84 | return new ObjectType([ |
||
| 85 | 'name' => 'TestObject', |
||
| 86 | 'fields' => [ |
||
| 87 | 'test' => Type::string(), |
||
| 88 | ], |
||
| 89 | ]); |
||
| 90 | default: |
||
| 91 | throw CannotMapTypeException::createForName($typeName); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns true if this type mapper can map the $typeName GraphQL name to a GraphQL type. |
||
| 97 | * |
||
| 98 | * @param string $typeName The name of the GraphQL type |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function canMapNameToType(string $typeName): bool |
||
| 102 | { |
||
| 103 | return $typeName === 'TestObject'; |
||
| 104 | } |
||
| 105 | }; |
||
| 106 | |||
| 107 | $this->composite = new CompositeTypeMapper([$typeMapper1]); |
||
| 108 | } |
||
| 143 |