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 |
||
26 | abstract class FieldPluginBase extends PluginBase implements FieldPluginInterface { |
||
27 | use CacheablePluginTrait; |
||
28 | use DescribablePluginTrait; |
||
29 | use TypedPluginTrait; |
||
30 | use ArgumentAwarePluginTrait; |
||
31 | use DeprecatablePluginTrait; |
||
32 | |||
33 | /** |
||
34 | * The language context service. |
||
35 | * |
||
36 | * @var \Drupal\graphql\GraphQLLanguageContext |
||
37 | */ |
||
38 | protected $languageContext; |
||
39 | |||
40 | /** |
||
41 | * The renderer service. |
||
42 | * |
||
43 | * @var \Drupal\Core\Render\RendererInterface |
||
44 | */ |
||
45 | protected $renderer; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public static function createInstance(SchemaBuilderInterface $builder, FieldPluginManager $manager, $definition, $id) { |
||
63 | |||
64 | /** |
||
65 | * Get the language context instance. |
||
66 | * |
||
67 | * @return \Drupal\graphql\GraphQLLanguageContext |
||
68 | * The language context service. |
||
69 | */ |
||
70 | protected function getLanguageContext() { |
||
76 | |||
77 | /** |
||
78 | * Get the renderer service. |
||
79 | * |
||
80 | * @return \Drupal\Core\Render\RendererInterface |
||
81 | */ |
||
82 | protected function getRenderer() { |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | View Code Duplication | public function getDefinition() { |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
136 | |||
137 | /** |
||
138 | * Indicator if the field is language aware. |
||
139 | * |
||
140 | * Checks for 'languages:*' cache contexts on the fields definition. |
||
141 | * |
||
142 | * @return bool |
||
143 | * The fields language awareness status. |
||
144 | */ |
||
145 | protected function isLanguageAwareField() { |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | protected function resolveDeferred(callable $callback, $value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
186 | |||
187 | /** |
||
188 | * Unwrap the resolved values. |
||
189 | * |
||
190 | * @param array $result |
||
191 | * The resolved values. |
||
192 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
193 | * The resolve info object. |
||
194 | * |
||
195 | * @return mixed |
||
196 | * The extracted values (an array of values in case this is a list, an |
||
197 | * arbitrary value if it isn't). |
||
198 | */ |
||
199 | protected function unwrapResult($result, ResolveInfo $info) { |
||
216 | |||
217 | /** |
||
218 | * Retrieve the list of cache dependencies for a given value and arguments. |
||
219 | * |
||
220 | * @param array $result |
||
221 | * The result of the field. |
||
222 | * @param mixed $parent |
||
223 | * The parent value. |
||
224 | * @param array $args |
||
225 | * The arguments passed to the field. |
||
226 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
227 | * The resolve context. |
||
228 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
229 | * The resolve info object. |
||
230 | * |
||
231 | * @return array |
||
232 | * A list of cacheable dependencies. |
||
233 | */ |
||
234 | protected function getCacheDependencies(array $result, $parent, array $args, ResolveContext $context, ResolveInfo $info) { |
||
253 | |||
254 | /** |
||
255 | * Retrieve the list of field values. |
||
256 | * |
||
257 | * Always returns a list of field values. Even for single value fields. |
||
258 | * Single/multi field handling is responsibility of the base class. |
||
259 | * |
||
260 | * @param mixed $value |
||
261 | * The current object value. |
||
262 | * @param array $args |
||
263 | * Field arguments. |
||
264 | * @param $context |
||
265 | * The resolve context. |
||
266 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
267 | * The resolve info object. |
||
268 | * |
||
269 | * @return \Generator |
||
270 | * The value generator. |
||
271 | */ |
||
272 | protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
276 | |||
277 | } |
||
278 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.