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 |
||
39 | class FieldResolver extends ResolverBase |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * @var mixed $model |
||
44 | */ |
||
45 | protected $model; |
||
46 | |||
47 | /** |
||
48 | * @var array $fields . |
||
49 | */ |
||
50 | protected $fields; |
||
51 | |||
52 | |||
53 | /** |
||
54 | * FieldResolver constructor. |
||
55 | * |
||
56 | * @param mixed $model The model instance. |
||
57 | * @param array $fields The fields registered for the type. |
||
58 | */ |
||
59 | public function __construct($model, array $fields) |
||
64 | |||
65 | |||
66 | /** |
||
67 | * @param mixed $source The source that's passed down the GraphQL queries |
||
68 | * @param array $args The inputArgs on the field |
||
69 | * @param AppContext $context The AppContext passed down the GraphQL tree |
||
70 | * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
||
71 | * @return string |
||
72 | * @throws EE_Error |
||
73 | * @throws Exception |
||
74 | * @throws InvalidArgumentException |
||
75 | * @throws InvalidDataTypeException |
||
76 | * @throws InvalidInterfaceException |
||
77 | * @throws ReflectionException |
||
78 | * @throws UserError |
||
79 | * @throws UnexpectedEntityException |
||
80 | * @since $VID:$ |
||
81 | */ |
||
82 | public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Resolve the global ID |
||
127 | * |
||
128 | * @param mixed $source |
||
129 | * @return string|null |
||
130 | * @throws Exception |
||
131 | * @since $VID:$ |
||
132 | */ |
||
133 | public function resolveId($source) |
||
139 | |||
140 | |||
141 | /** |
||
142 | * Resolve the cache ID |
||
143 | * |
||
144 | * @param mixed $source |
||
145 | * @return string |
||
146 | * @since $VID:$ |
||
147 | */ |
||
148 | public function resolveCacheId($source) |
||
157 | |||
158 | |||
159 | /** |
||
160 | * @param mixed $source |
||
161 | * @param $args |
||
162 | * @param $context |
||
163 | * @return Deferred|null |
||
164 | * @throws Exception |
||
165 | * @since $VID:$ |
||
166 | */ |
||
167 | public function resolveWpUser($source, $args, $context) |
||
174 | |||
175 | |||
176 | /** |
||
177 | * @param mixed $source |
||
178 | * @return EE_Base_Class|null |
||
179 | * @throws EE_Error |
||
180 | * @throws InvalidArgumentException |
||
181 | * @throws InvalidDataTypeException |
||
182 | * @throws InvalidInterfaceException |
||
183 | * @throws ReflectionException |
||
184 | * @since $VID:$ |
||
185 | */ |
||
186 | public function resolveParent($source) |
||
190 | |||
191 | |||
192 | /** |
||
193 | * @param mixed $source |
||
194 | * @param $args |
||
195 | * @param $context |
||
196 | * @return Deferred|null |
||
197 | * @throws EE_Error |
||
198 | * @throws InvalidArgumentException |
||
199 | * @throws InvalidDataTypeException |
||
200 | * @throws InvalidInterfaceException |
||
201 | * @throws ReflectionException |
||
202 | * @throws UserError |
||
203 | * @throws UnexpectedEntityException |
||
204 | * @since $VID:$ |
||
205 | */ |
||
206 | public function resolveEvent($source, $args, $context) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * @param mixed $source The source instance. |
||
228 | * @return EE_Base_Class|null |
||
229 | * @throws EE_Error |
||
230 | * @throws InvalidArgumentException |
||
231 | * @throws InvalidDataTypeException |
||
232 | * @throws InvalidInterfaceException |
||
233 | * @since $VID:$ |
||
234 | */ |
||
235 | View Code Duplication | public function resolveState($source) |
|
250 | |||
251 | |||
252 | /** |
||
253 | * @param mixed $source The source instance. |
||
254 | * @return EE_Base_Class|null |
||
255 | * @throws EE_Error |
||
256 | * @throws InvalidArgumentException |
||
257 | * @throws InvalidDataTypeException |
||
258 | * @throws InvalidInterfaceException |
||
259 | * @since $VID:$ |
||
260 | */ |
||
261 | View Code Duplication | public function resolveCountry($source) |
|
279 | } |
||
280 |
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: