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) { |
||
104 | |||
105 | /** |
||
106 | * @param $result |
||
107 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
108 | * |
||
109 | * @return array|mixed|null |
||
110 | */ |
||
111 | protected function unwrapResult($result, ResolveInfo $info) { |
||
124 | |||
125 | /** |
||
126 | * Retrieve the list of cache dependencies for a given value and arguments. |
||
127 | * |
||
128 | * @param array $result |
||
129 | * The result of the field. |
||
130 | * @param mixed $parent |
||
131 | * The parent value. |
||
132 | * @param array $args |
||
133 | * The arguments passed to the field. |
||
134 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
135 | * The resolve info object. |
||
136 | * |
||
137 | * @return array |
||
138 | * A list of cacheable dependencies. |
||
139 | */ |
||
140 | protected function getCacheDependencies(array $result, $parent, array $args, ResolveInfo $info) { |
||
145 | |||
146 | /** |
||
147 | * Retrieve the list of field values. |
||
148 | * |
||
149 | * Always returns a list of field values. Even for single value fields. |
||
150 | * Single/multi field handling is responsibility of the base class. |
||
151 | * |
||
152 | * @param mixed $value |
||
153 | * The current object value. |
||
154 | * @param array $args |
||
155 | * Field arguments. |
||
156 | * @param \Youshido\GraphQL\Execution\ResolveInfo $info |
||
157 | * The resolve info object. |
||
158 | * |
||
159 | * @return \Generator |
||
160 | * The value generator. |
||
161 | */ |
||
162 | protected function resolveValues($value, array $args, ResolveInfo $info) { |
||
166 | |||
167 | } |
||
168 |
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.