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 |
||
24 | abstract class FieldPluginBase extends PluginBase implements TypeSystemPluginInterface, SecureFieldInterface { |
||
25 | use CacheablePluginTrait; |
||
26 | use NamedPluginTrait; |
||
27 | use ArgumentAwarePluginTrait; |
||
28 | |||
29 | /** |
||
30 | * The field instance. |
||
31 | * |
||
32 | * @var \Drupal\graphql\GraphQL\Field\Field |
||
33 | */ |
||
34 | protected $definition; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | View Code Duplication | public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) { |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function isSecure() { |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function resolve($value, array $args, ResolveInfo $info) { |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | protected function resolveDeferred(callable $callback, $value, array $args, ResolveInfo $info) { |
||
108 | |||
109 | /** |
||
110 | * Retrieve the list of cache dependencies for a given value and arguments. |
||
111 | * |
||
112 | * @param array $result |
||
113 | * The result of the field. |
||
114 | * @param mixed $parent |
||
115 | * The parent value. |
||
116 | * @param array $args |
||
117 | * The arguments passed to the field. |
||
118 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
119 | * The resolve info object. |
||
120 | * |
||
121 | * @return array |
||
122 | * A list of cacheable dependencies. |
||
123 | */ |
||
124 | protected function getCacheDependencies(array $result, $parent, array $args, ResolveInfo $info) { |
||
129 | |||
130 | /** |
||
131 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
132 | * |
||
133 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
134 | */ |
||
135 | protected function getCacheCollector(ResolveInfo $info) { |
||
145 | |||
146 | /** |
||
147 | * Commit a cache dependency into the processor's cache collector. |
||
148 | * |
||
149 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
150 | * The resolve info object. |
||
151 | * @param mixed $object |
||
152 | * The object to merge into the cache metadata of the processor. |
||
153 | * |
||
154 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
155 | * The processor's cache metadata object. |
||
156 | */ |
||
157 | public function commitCacheableDependency(ResolveInfo $info, $object) { |
||
160 | |||
161 | /** |
||
162 | * Commit cache contexts into the processor's cache collector. |
||
163 | * |
||
164 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
165 | * The resolve info object. |
||
166 | * @param array $contexts |
||
167 | * The array of cache contexts to commit. |
||
168 | * |
||
169 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
170 | * The processor's cache metadata object. |
||
171 | */ |
||
172 | public function commitCacheContexts(ResolveInfo $info, array $contexts) { |
||
175 | |||
176 | /** |
||
177 | * Commit cache tags into the processor's cache collector. |
||
178 | * |
||
179 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
180 | * The resolve info object. |
||
181 | * @param array $tags |
||
182 | * The array of cache tags to commit. |
||
183 | * |
||
184 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
185 | * The processor's cache metadata object. |
||
186 | */ |
||
187 | public function commitCacheTags(ResolveInfo $info, array $tags) { |
||
190 | |||
191 | /** |
||
192 | * Commit a cache max age into the processor's cache collector. |
||
193 | * |
||
194 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
195 | * The resolve info object. |
||
196 | * @param int $age |
||
197 | * The max age to set. |
||
198 | * |
||
199 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
200 | * The processor's cache metadata object. |
||
201 | */ |
||
202 | public function commitCacheMaxAge(ResolveInfo $info, $age) { |
||
205 | |||
206 | /** |
||
207 | * Retrieve the list of field values. |
||
208 | * |
||
209 | * Always returns a list of field values. Even for single value fields. |
||
210 | * Single/multi field handling is responsibility of the base class. |
||
211 | * |
||
212 | * @param mixed $value |
||
213 | * The current object value. |
||
214 | * @param array $args |
||
215 | * Field arguments. |
||
216 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
217 | * The resolve info object. |
||
218 | * |
||
219 | * @return \Generator |
||
220 | * The value generator. |
||
221 | */ |
||
222 | protected function resolveValues($value, array $args, ResolveInfo $info) { |
||
226 | |||
227 | } |
||
228 |
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.