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 |
||
| 27 | abstract class FieldPluginBase extends PluginBase implements FieldPluginInterface { |
||
| 28 | use CacheablePluginTrait; |
||
| 29 | use DescribablePluginTrait; |
||
| 30 | use TypedPluginTrait; |
||
| 31 | use ArgumentAwarePluginTrait; |
||
| 32 | use DeprecatablePluginTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The language context, for simulating active languages in fields. |
||
| 36 | * |
||
| 37 | * @var \Drupal\graphql\GraphQLLanguageContext |
||
| 38 | */ |
||
| 39 | protected $languageContext; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Set the language context instance. |
||
| 43 | * |
||
| 44 | * @param \Drupal\graphql\GraphQLLanguageContext $languageContext |
||
| 45 | * The language context instance. |
||
| 46 | */ |
||
| 47 | public function setLanguageContext(GraphQLLanguageContext $languageContext) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public static function createInstance(SchemaBuilderInterface $builder, FieldPluginManager $manager, $definition, $id) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | View Code Duplication | public function getDefinition() { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | protected function resolveDeferred(callable $callback, $value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Unwrap the resolved values. |
||
| 139 | * |
||
| 140 | * @param array $result |
||
| 141 | * The resolved values. |
||
| 142 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
| 143 | * The resolve info object. |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | * The extracted values (an array of values in case this is a list, an |
||
| 147 | * arbitrary value if it isn't). |
||
| 148 | */ |
||
| 149 | protected function unwrapResult($result, ResolveInfo $info) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Retrieve the list of cache dependencies for a given value and arguments. |
||
| 169 | * |
||
| 170 | * @param array $result |
||
| 171 | * The result of the field. |
||
| 172 | * @param mixed $parent |
||
| 173 | * The parent value. |
||
| 174 | * @param array $args |
||
| 175 | * The arguments passed to the field. |
||
| 176 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
| 177 | * The resolve context. |
||
| 178 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
| 179 | * The resolve info object. |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | * A list of cacheable dependencies. |
||
| 183 | */ |
||
| 184 | protected function getCacheDependencies(array $result, $parent, array $args, ResolveContext $context, ResolveInfo $info) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Retrieve the list of field values. |
||
| 206 | * |
||
| 207 | * Always returns a list of field values. Even for single value fields. |
||
| 208 | * Single/multi field handling is responsibility of the base class. |
||
| 209 | * |
||
| 210 | * @param mixed $value |
||
| 211 | * The current object value. |
||
| 212 | * @param array $args |
||
| 213 | * Field arguments. |
||
| 214 | * @param $context |
||
| 215 | * The resolve context. |
||
| 216 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
| 217 | * The resolve info object. |
||
| 218 | * |
||
| 219 | * @return \Generator |
||
| 220 | * The value generator. |
||
| 221 | */ |
||
| 222 | protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
| 226 | |||
| 227 | } |
||
| 228 |
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: