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 |
||
11 | trait CacheableEdgeTrait { |
||
12 | |||
13 | /** |
||
14 | * {@inheritdoc} |
||
15 | */ |
||
16 | abstract public function getPlugin(PluggableSchemaBuilderInterface $schemaBuilder); |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | public function getSchemaCacheMetadata(AbstractSchema $schema) { |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function getResponseCacheMetadata(AbstractSchema $schema) { |
||
35 | |||
36 | /** |
||
37 | * Helper function to load cache metadata from a schema's edges. |
||
38 | * |
||
39 | * @param \Youshido\GraphQL\Schema\AbstractSchema $schema |
||
40 | * The schema. |
||
41 | * @param callable $callback |
||
42 | * Callback to return the cache metadata from an edge. |
||
43 | * |
||
44 | * @return \Drupal\Core\Cache\RefinableCacheableDependencyInterface |
||
45 | * The cache metadata. |
||
46 | */ |
||
47 | View Code Duplication | protected function getCacheMetadata(AbstractSchema $schema, callable $callback) { |
|
64 | |||
65 | } |
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.