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 |
||
35 | class EntityQueryEntities extends FieldPluginBase implements ContainerFactoryPluginInterface { |
||
36 | use DependencySerializationTrait; |
||
37 | |||
38 | /** |
||
39 | * The entity buffer service. |
||
40 | * |
||
41 | * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer |
||
42 | */ |
||
43 | protected $entityBuffer; |
||
44 | |||
45 | /** |
||
46 | * The entity type manager service. |
||
47 | * |
||
48 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
||
49 | */ |
||
50 | protected $entityTypeManager; |
||
51 | |||
52 | /** |
||
53 | * The entity repository service. |
||
54 | * |
||
55 | * @var \Drupal\Core\Entity\EntityRepositoryInterface |
||
56 | */ |
||
57 | protected $entityRepository; |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | View Code Duplication | public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
|
72 | |||
73 | /** |
||
74 | * EntityQueryEntities constructor. |
||
75 | * |
||
76 | * @param array $configuration |
||
77 | * The plugin configuration array. |
||
78 | * @param string $pluginId |
||
79 | * The plugin id. |
||
80 | * @param mixed $pluginDefinition |
||
81 | * The plugin definition array. |
||
82 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
||
83 | * The entity type manager service. |
||
84 | * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository |
||
85 | * The entity repository service. |
||
86 | * @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer |
||
87 | * The entity buffer service. |
||
88 | */ |
||
89 | View Code Duplication | public function __construct( |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) { |
||
120 | |||
121 | /** |
||
122 | * Resolves entities lazily through the entity buffer. |
||
123 | * |
||
124 | * @param string $type |
||
125 | * The entity type. |
||
126 | * @param array $ids |
||
127 | * The entity ids to load. |
||
128 | * @param mixed $metadata |
||
129 | * The query context. |
||
130 | * @param array $args |
||
131 | * The field arguments array. |
||
132 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
133 | * The resolve context. |
||
134 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
135 | * The resolve info object. |
||
136 | * |
||
137 | * @return \Closure |
||
138 | * The deferred resolver. |
||
139 | */ |
||
140 | protected function resolveFromEntityIds($type, $ids, $metadata, array $args, ResolveContext $context, ResolveInfo $info) { |
||
146 | |||
147 | /** |
||
148 | * Resolves entity revisions. |
||
149 | * |
||
150 | * @param string $type |
||
151 | * The entity type. |
||
152 | * @param array $ids |
||
153 | * The entity revision ids to load. |
||
154 | * @param mixed $metadata |
||
155 | * The query context. |
||
156 | * @param array $args |
||
157 | * The field arguments array. |
||
158 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
159 | * The resolve context. |
||
160 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
161 | * The resolve info object. |
||
162 | * |
||
163 | * @return \Generator |
||
164 | * The resolved revisions. |
||
165 | */ |
||
166 | protected function resolveFromRevisionIds($type, $ids, $metadata, array $args, ResolveContext $context, ResolveInfo $info) { |
||
174 | |||
175 | /** |
||
176 | * Resolves entity objects and checks view permissions. |
||
177 | * |
||
178 | * @param array $entities |
||
179 | * The entities to resolve. |
||
180 | * @param mixed $metadata |
||
181 | * The query context. |
||
182 | * @param array $args |
||
183 | * The field arguments array. |
||
184 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
185 | * The resolve context. |
||
186 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
187 | * The resolve info object. |
||
188 | * |
||
189 | * @return \Generator |
||
190 | * The resolved entities. |
||
191 | */ |
||
192 | protected function resolveEntities(array $entities, $metadata, array $args, ResolveContext $context, ResolveInfo $info) { |
||
213 | |||
214 | /** |
||
215 | * Negotiate the language for the resolved entities. |
||
216 | * |
||
217 | * @param mixed $metadata |
||
218 | * The query context. |
||
219 | * @param array $args |
||
220 | * The field arguments array. |
||
221 | * @param \Drupal\graphql\GraphQL\Execution\ResolveContext $context |
||
222 | * The resolve context. |
||
223 | * @param \GraphQL\Type\Definition\ResolveInfo $info |
||
224 | * The resolve info object. |
||
225 | * |
||
226 | * @return string|null |
||
227 | * The negotiated language id. |
||
228 | */ |
||
229 | protected function negotiateLanguage($metadata, $args, ResolveContext $context, ResolveInfo $info) { |
||
240 | |||
241 | } |
||
242 |
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.