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 Drupal7 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 Drupal7, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Drupal7 extends AbstractCore { |
||
11 | |||
12 | /** |
||
13 | * {@inheritdoc} |
||
14 | */ |
||
15 | public function bootstrap() { |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function clearCache() { |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function nodeCreate($node) { |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function nodeDelete($node) { |
||
74 | |||
75 | /** |
||
76 | * Implements CoreInterface::runCron(). |
||
77 | */ |
||
78 | public function runCron() { |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function userCreate(\stdClass $user) { |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function userDelete(\stdClass $user) { |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function processBatch() { |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function userAddRole(\stdClass $user, $role_name) { |
||
135 | |||
136 | /** |
||
137 | * Check to make sure that the array of permissions are valid. |
||
138 | * |
||
139 | * @param array $permissions |
||
140 | * Permissions to check. |
||
141 | * @param bool $reset |
||
142 | * Reset cached available permissions. |
||
143 | * |
||
144 | * @return bool |
||
145 | * TRUE or FALSE depending on whether the permissions are valid. |
||
146 | */ |
||
147 | protected function checkPermissions(array $permissions, $reset = FALSE) { |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function roleCreate(array $permissions) { |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function roleDelete($role_name) { |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | View Code Duplication | public function validateDrupalSite() { |
|
256 | |||
257 | /** |
||
258 | * Expands properties on the given entity object to the expected structure. |
||
259 | * |
||
260 | * @param \stdClass $entity |
||
261 | * The entity object. |
||
262 | */ |
||
263 | View Code Duplication | protected function expandEntityProperties(\stdClass $entity) { |
|
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function termCreate(\stdClass $term) { |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function termDelete(\stdClass $term) { |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | public function languageCreate(\stdClass $language) { |
||
366 | |||
367 | /** |
||
368 | * {@inheritdoc} |
||
369 | */ |
||
370 | public function languageDelete(\stdClass $language) { |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function configGet($name, $key = '') { |
||
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | */ |
||
415 | public function configGetOriginal($name, $key = '') { |
||
418 | |||
419 | /** |
||
420 | * {@inheritdoc} |
||
421 | */ |
||
422 | public function configSet($name, $key, $value) { |
||
425 | |||
426 | /** |
||
427 | * Helper function to get all permissions. |
||
428 | * |
||
429 | * @return array |
||
430 | * Array keyed by permission name, with the human-readable title as the |
||
431 | * value. |
||
432 | */ |
||
433 | View Code Duplication | protected function getAllPermissions() { |
|
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | public function getModuleList() { |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | View Code Duplication | public function getExtensionPathList() { |
|
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | public function getEntityFieldTypes($entity_type, array $base_fields = array()) { |
||
476 | |||
477 | /** |
||
478 | * {@inheritdoc} |
||
479 | */ |
||
480 | public function isField($entity_type, $field_name) { |
||
484 | |||
485 | /** |
||
486 | * {@inheritdoc} |
||
487 | */ |
||
488 | public function clearStaticCaches() { |
||
491 | |||
492 | /** |
||
493 | * {@inheritdoc} |
||
494 | */ |
||
495 | public function entityCreate($entity_type, $entity) { |
||
496 | $info = entity_get_info($entity_type); |
||
497 | // If the bundle field is empty, put the inferred bundle value in it. |
||
498 | $bundle_key = $info['entity keys']['bundle']; |
||
499 | View Code Duplication | if (!isset($entity->$bundle_key) && isset($entity->step_bundle)) { |
|
500 | $entity->$bundle_key = $entity->step_bundle; |
||
501 | } |
||
502 | |||
503 | // Throw an exception if a bundle is specified but does not exist. |
||
504 | if (isset($entity->$bundle_key) && ($entity->$bundle_key !== NULL)) { |
||
505 | $bundles = $info['bundles']; |
||
506 | View Code Duplication | if (!in_array($entity->$bundle_key, array_keys($bundles))) { |
|
507 | throw new \Exception("Cannot create entity because provided bundle {$entity->$bundle_key} does not exist."); |
||
508 | } |
||
509 | } |
||
510 | if (empty($entity_type)) { |
||
511 | throw new \Exception("You must specify an entity type to create an entity."); |
||
512 | } |
||
513 | |||
514 | $this->expandEntityFields($entity_type, $entity); |
||
515 | $createdEntity = entity_create($entity_type, (array) $entity); |
||
516 | |||
517 | // In D7 it's possible that $createdEntity is not of class Entity, so we |
||
518 | // must use entity_save(). |
||
519 | entity_save($entity_type, $createdEntity); |
||
520 | |||
521 | list($id) = entity_extract_ids($entity_type, $createdEntity); |
||
522 | $createdEntity->id = $id; |
||
523 | |||
524 | return $createdEntity; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * {@inheritdoc} |
||
529 | */ |
||
530 | public function entityDelete($entity_type, $entity) { |
||
536 | |||
537 | /** |
||
538 | * {@inheritdoc} |
||
539 | */ |
||
540 | public function startCollectingMail() { |
||
544 | |||
545 | /** |
||
546 | * {@inheritdoc} |
||
547 | */ |
||
548 | public function stopCollectingMail() { |
||
552 | |||
553 | /** |
||
554 | * {@inheritdoc} |
||
555 | */ |
||
556 | public function getMail() { |
||
560 | |||
561 | /** |
||
562 | * {@inheritdoc} |
||
563 | */ |
||
564 | public function clearMail() { |
||
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | public function sendMail($body, $subject = '', $to = '', $langcode = '') { |
||
576 | |||
577 | } |
||
578 |
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.