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 |
||
7 | class PluggableSchemaBuilder implements PluggableSchemaBuilderInterface { |
||
8 | use DependencySerializationTrait { |
||
9 | __sleep as sleepDependencies; |
||
10 | } |
||
11 | |||
12 | /** |
||
13 | * The type system plugin manager aggregator service. |
||
14 | * |
||
15 | * @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator |
||
16 | */ |
||
17 | protected $pluginManagers; |
||
18 | |||
19 | /** |
||
20 | * Static cache of type system plugin instances. |
||
21 | * |
||
22 | * @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface |
||
23 | */ |
||
24 | protected $instances = []; |
||
25 | |||
26 | /** |
||
27 | * PluggableSchemaBuilder constructor. |
||
28 | * |
||
29 | * @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers |
||
30 | * Type system plugin manager aggregator service. |
||
31 | */ |
||
32 | public function __construct(TypeSystemPluginManagerAggregator $pluginManagers) { |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | public function getInstance($pluginType, $pluginId, array $pluginConfiguration = []) { |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function find(callable $selector, array $types) { |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function findByDataType($type, array $types) { |
||
131 | |||
132 | public function findByName($name, array $types) { |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function findByDataTypeOrName($input, array $types) { |
||
154 | |||
155 | /** |
||
156 | * Creates a plugin instance cache identifier. |
||
157 | * |
||
158 | * @param string $pluginType |
||
159 | * The plugin type. |
||
160 | * @param string $pluginId |
||
161 | * The plugin id. |
||
162 | * @param array $pluginConfiguration |
||
163 | * The plugin configuration. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | protected function getCacheIdentifier($pluginType, $pluginId, array $pluginConfiguration) { |
||
175 | |||
176 | /** |
||
177 | * Recursively sorts an array. |
||
178 | * |
||
179 | * Useful for generating a cache identifiers. |
||
180 | * |
||
181 | * @param array $subject |
||
182 | * The array to sort. |
||
183 | * |
||
184 | * @return array |
||
185 | * The sorted array. |
||
186 | */ |
||
187 | protected function sortRecursive(array $subject) { |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function __sleep() { |
||
205 | |||
206 | } |
||
207 |
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.