Complex classes like FieldResolver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FieldResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class FieldResolver extends ResolverBase |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var mixed $model |
||
| 42 | */ |
||
| 43 | protected $model; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array $fields . |
||
| 47 | */ |
||
| 48 | protected $fields; |
||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * FieldResolver constructor. |
||
| 53 | * |
||
| 54 | * @param mixed $model The model instance. |
||
| 55 | * @param array $fields The fields registered for the type. |
||
| 56 | */ |
||
| 57 | public function __construct($model, array $fields) |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @param mixed $source The source that's passed down the GraphQL queries |
||
| 66 | * @param array $args The inputArgs on the field |
||
| 67 | * @param AppContext $context The AppContext passed down the GraphQL tree |
||
| 68 | * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
||
| 69 | * @return string |
||
| 70 | * @throws EE_Error |
||
| 71 | * @throws Exception |
||
| 72 | * @throws InvalidArgumentException |
||
| 73 | * @throws InvalidDataTypeException |
||
| 74 | * @throws InvalidInterfaceException |
||
| 75 | * @throws ReflectionException |
||
| 76 | * @throws UserError |
||
| 77 | * @throws UnexpectedEntityException |
||
| 78 | * @since $VID:$ |
||
| 79 | */ |
||
| 80 | public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
||
| 81 | { |
||
| 82 | $fieldName = $info->fieldName; |
||
| 83 | $field = isset($this->fields[ $fieldName ]) ? $this->fields[ $fieldName ] : null; |
||
| 84 | // Field should exist in teh registered fields |
||
| 85 | if ($field instanceof GraphQLField) { |
||
| 86 | // check if the field should be resolved. |
||
| 87 | if (! $field->shouldResolve()) { |
||
| 88 | return null; |
||
| 89 | } |
||
| 90 | // Give priority to the internal resolver. |
||
| 91 | if ($field->hasInternalResolver()) { |
||
| 92 | return $field->resolve($source, $args, $context, $info); |
||
| 93 | } |
||
| 94 | if (! ($source instanceof EE_Base_Class)) { |
||
| 95 | return null; |
||
| 96 | } |
||
| 97 | // Check if the field has a key mapped to model. |
||
| 98 | if (! empty($field->key())) { |
||
| 99 | $value = $source->{$field->key()}(); |
||
| 100 | return $field->mayBeFormatValue($value, $source); |
||
| 101 | } |
||
| 102 | switch ($fieldName) { |
||
| 103 | case 'id': // the global ID |
||
| 104 | return $this->resolveId($source); |
||
| 105 | case 'cacheId': |
||
| 106 | return $this->resolveCacheId($source); |
||
| 107 | case 'parent': |
||
| 108 | return $this->resolveParent($source); |
||
| 109 | case 'event': |
||
| 110 | return $this->resolveEvent($source, $args, $context); |
||
| 111 | case 'wpUser': |
||
| 112 | case 'manager': |
||
| 113 | return $this->resolveWpUser($source, $args, $context); |
||
| 114 | case 'userId': |
||
| 115 | return $this->resolveUserId($source, $args, $context); |
||
| 116 | case 'state': // Venue |
||
| 117 | return $this->resolveState($source); |
||
| 118 | case 'country': // State, Venue |
||
| 119 | return $this->resolveCountry($source); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | return null; |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Resolve the global ID |
||
| 128 | * |
||
| 129 | * @param mixed $source |
||
| 130 | * @return string|null |
||
| 131 | * @throws Exception |
||
| 132 | * @since $VID:$ |
||
| 133 | */ |
||
| 134 | public function resolveId($source) |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Resolve the cache ID |
||
| 144 | * |
||
| 145 | * @param mixed $source |
||
| 146 | * @return string |
||
| 147 | * @since $VID:$ |
||
| 148 | */ |
||
| 149 | public function resolveCacheId($source) |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @param mixed $source |
||
| 162 | * @param $args |
||
| 163 | * @param $context |
||
| 164 | * @return Deferred|null |
||
| 165 | * @throws Exception |
||
| 166 | * @since $VID:$ |
||
| 167 | */ |
||
| 168 | public function resolveWpUser($source, $args, $context) |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * @param mixed $source |
||
| 179 | * @param $args |
||
| 180 | * @param $context |
||
| 181 | * @return Deferred|null |
||
| 182 | * @throws Exception |
||
| 183 | * @since $VID:$ |
||
| 184 | */ |
||
| 185 | public function resolveUserId($source, $args, $context) |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @param mixed $source |
||
| 194 | * @return EE_Base_Class|null |
||
| 195 | * @throws EE_Error |
||
| 196 | * @throws InvalidArgumentException |
||
| 197 | * @throws InvalidDataTypeException |
||
| 198 | * @throws InvalidInterfaceException |
||
| 199 | * @throws ReflectionException |
||
| 200 | * @since $VID:$ |
||
| 201 | */ |
||
| 202 | public function resolveParent($source) |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * @param mixed $source |
||
| 210 | * @param $args |
||
| 211 | * @param $context |
||
| 212 | * @return Deferred|null |
||
| 213 | * @throws EE_Error |
||
| 214 | * @throws InvalidArgumentException |
||
| 215 | * @throws InvalidDataTypeException |
||
| 216 | * @throws InvalidInterfaceException |
||
| 217 | * @throws ReflectionException |
||
| 218 | * @throws UserError |
||
| 219 | * @throws UnexpectedEntityException |
||
| 220 | * @since $VID:$ |
||
| 221 | */ |
||
| 222 | public function resolveEvent($source, $args, $context) |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * @param mixed $source The source instance. |
||
| 244 | * @return EE_Base_Class|null |
||
| 245 | * @throws EE_Error |
||
| 246 | * @throws InvalidArgumentException |
||
| 247 | * @throws InvalidDataTypeException |
||
| 248 | * @throws InvalidInterfaceException |
||
| 249 | * @since $VID:$ |
||
| 250 | */ |
||
| 251 | public function resolveState($source) |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * @param mixed $source The source instance. |
||
| 270 | * @return EE_Base_Class|null |
||
| 271 | * @throws EE_Error |
||
| 272 | * @throws InvalidArgumentException |
||
| 273 | * @throws InvalidDataTypeException |
||
| 274 | * @throws InvalidInterfaceException |
||
| 275 | * @since $VID:$ |
||
| 276 | */ |
||
| 277 | public function resolveCountry($source) |
||
| 296 | } |
||
| 297 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: