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