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 |
||
| 40 | class FieldResolver extends ResolverBase |
||
| 41 | { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var mixed $model |
||
| 45 | */ |
||
| 46 | protected $model; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array $fields . |
||
| 50 | */ |
||
| 51 | protected $fields; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * FieldResolver constructor. |
||
| 56 | * |
||
| 57 | * @param mixed $model The model instance. |
||
| 58 | * @param array $fields The fields registered for the type. |
||
| 59 | */ |
||
| 60 | public function __construct($model, array $fields) |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * @param mixed $source The source that's passed down the GraphQL queries |
||
| 69 | * @param array $args The inputArgs on the field |
||
| 70 | * @param AppContext $context The AppContext passed down the GraphQL tree |
||
| 71 | * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
||
| 72 | * @return string |
||
| 73 | * @throws EE_Error |
||
| 74 | * @throws Exception |
||
| 75 | * @throws InvalidArgumentException |
||
| 76 | * @throws InvalidDataTypeException |
||
| 77 | * @throws InvalidInterfaceException |
||
| 78 | * @throws ReflectionException |
||
| 79 | * @throws UserError |
||
| 80 | * @throws UnexpectedEntityException |
||
| 81 | * @since $VID:$ |
||
| 82 | */ |
||
| 83 | public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param mixed $source |
||
| 121 | * @param $args |
||
| 122 | * @param $context |
||
| 123 | * @return Deferred|null |
||
| 124 | * @throws Exception |
||
| 125 | * @since $VID:$ |
||
| 126 | */ |
||
| 127 | public function resolveWpUser($source, $args, $context) |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * @param mixed $source |
||
| 138 | * @return EE_Base_Class|null |
||
| 139 | * @throws EE_Error |
||
| 140 | * @throws InvalidArgumentException |
||
| 141 | * @throws InvalidDataTypeException |
||
| 142 | * @throws InvalidInterfaceException |
||
| 143 | * @throws ReflectionException |
||
| 144 | * @since $VID:$ |
||
| 145 | */ |
||
| 146 | public function resolveParent($source) |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @param mixed $source |
||
| 154 | * @param $args |
||
| 155 | * @param $context |
||
| 156 | * @return Deferred|null |
||
| 157 | * @throws EE_Error |
||
| 158 | * @throws InvalidArgumentException |
||
| 159 | * @throws InvalidDataTypeException |
||
| 160 | * @throws InvalidInterfaceException |
||
| 161 | * @throws ReflectionException |
||
| 162 | * @throws UserError |
||
| 163 | * @throws UnexpectedEntityException |
||
| 164 | * @since $VID:$ |
||
| 165 | */ |
||
| 166 | public function resolveEvent($source, $args, $context) |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @param mixed $source The source instance. |
||
| 188 | * @return EE_Base_Class|null |
||
| 189 | * @throws EE_Error |
||
| 190 | * @throws InvalidArgumentException |
||
| 191 | * @throws InvalidDataTypeException |
||
| 192 | * @throws InvalidInterfaceException |
||
| 193 | * @since $VID:$ |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function resolveState($source) |
|
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * @param mixed $source The source instance. |
||
| 214 | * @return EE_Base_Class|null |
||
| 215 | * @throws EE_Error |
||
| 216 | * @throws InvalidArgumentException |
||
| 217 | * @throws InvalidDataTypeException |
||
| 218 | * @throws InvalidInterfaceException |
||
| 219 | * @since $VID:$ |
||
| 220 | */ |
||
| 221 | View Code Duplication | public function resolveCountry($source) |
|
| 239 | } |
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: