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:
Complex classes like SchemaBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SchemaBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SchemaBuilder { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
| 12 | */ |
||
| 13 | protected $fieldManager; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
| 17 | */ |
||
| 18 | protected $mutationManager; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var \Drupal\graphql\Plugin\TypePluginManager[] |
||
| 22 | */ |
||
| 23 | protected $typeManagers; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $fields; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $mutations; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $types; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * SchemaBuilder constructor. |
||
| 42 | * |
||
| 43 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
| 44 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Registers a plugin manager. |
||
| 56 | * |
||
| 57 | * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager |
||
| 58 | * The plugin manager to register. |
||
| 59 | * @param $id |
||
| 60 | * The id of the service. |
||
| 61 | */ |
||
| 62 | public function addTypeManager(PluginManagerInterface $pluginManager, $id) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function hasFields($parent) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function hasMutations() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function hasType($name) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public function getFields($parent) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function getMutations() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getTypes() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param $name |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getType($name) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $fields |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function processFields($fields) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param $args |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function processArguments($args) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param $type |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function processType($type) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | protected function getTypeMap() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param $type |
||
| 190 | * |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | View Code Duplication | protected function buildType($type) { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param $field |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | View Code Duplication | protected function buildField($field) { |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param $mutation |
||
| 218 | * |
||
| 219 | * @return mixed |
||
| 220 | */ |
||
| 221 | View Code Duplication | protected function buildMutation($mutation) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | protected function buildTypeMap() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | protected function getTypeReferenceMap() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | protected function buildTypeReferenceMap() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | protected function getFieldAssociationMap() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | protected function buildFieldAssociationMap() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | protected function getFieldMap() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | protected function buildFieldMap() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | protected function getMutationMap() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | protected function buildMutationMap() { |
||
| 439 | } |
||
| 440 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.