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 nodeLoad($nid) { |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function nodeCreate($node) { |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function nodeDelete($node) { |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function nodeDeleteMultiple(array $nids) { |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | * |
||
92 | * @param object $node |
||
93 | * A drupal node object. |
||
94 | * @param object $values |
||
95 | * An object with field/value parameters. |
||
96 | */ |
||
97 | public function nodeAlter($node, $values) { |
||
117 | |||
118 | /** |
||
119 | * Implements CoreInterface::runCron(). |
||
120 | */ |
||
121 | public function runCron() { |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function userLoad($uid) { |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function userCreate(\stdClass $user) { |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | * |
||
159 | * @param object $user |
||
160 | * A drupal user object. |
||
161 | * @param object $values |
||
162 | * An object with field/value parameters. |
||
163 | */ |
||
164 | public function userAlter($user, $values) { |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function userDelete(\stdClass $user) { |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function userDeleteMultiple(array $uids) { |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function processBatch() { |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function userAddRole(\stdClass $user, $role_name) { |
||
215 | |||
216 | /** |
||
217 | * Check to make sure that the array of permissions are valid. |
||
218 | * |
||
219 | * @param array $permissions |
||
220 | * Permissions to check. |
||
221 | * @param bool $reset |
||
222 | * Reset cached available permissions. |
||
223 | * |
||
224 | * @return bool |
||
225 | * TRUE or FALSE depending on whether the permissions are valid. |
||
226 | */ |
||
227 | protected function checkPermissions(array $permissions, $reset = FALSE) { |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function roleCreate(array $permissions) { |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function roleDelete($role_name) { |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | */ |
||
285 | View Code Duplication | public function validateDrupalSite() { |
|
336 | |||
337 | /** |
||
338 | * Expands properties on the given entity object to the expected structure. |
||
339 | * |
||
340 | * @param \stdClass $entity |
||
341 | * The entity object. |
||
342 | */ |
||
343 | protected function expandEntityProperties(\stdClass $entity) { |
||
362 | |||
363 | /** |
||
364 | * {@inheritdoc} |
||
365 | */ |
||
366 | public function termLoad($tid, $vocabulary = NULL) { |
||
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | */ |
||
376 | public function termCreate(\stdClass $term) { |
||
419 | |||
420 | /** |
||
421 | * {@inheritdoc} |
||
422 | */ |
||
423 | public function termDelete(\stdClass $term) { |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function languageCreate(\stdClass $language) { |
||
436 | if (!module_exists('locale')) { |
||
437 | throw new \Exception(sprintf("%s::%s line %s: This driver requires the 'locale' module be enabled in order to create languages", get_class($this), __FUNCTION__, __LINE__)); |
||
438 | } |
||
439 | include_once DRUPAL_ROOT . '/includes/iso.inc'; |
||
440 | include_once DRUPAL_ROOT . '/includes/locale.inc'; |
||
441 | // Get all predefined languages, regardless if they are enabled or not. |
||
442 | $predefined_languages = _locale_get_predefined_list(); |
||
443 | |||
444 | // If the language code is not valid then throw an InvalidArgumentException. |
||
445 | if (!isset($predefined_languages[$language->langcode])) { |
||
446 | throw new InvalidArgumentException("There is no predefined language with langcode '{$language->langcode}'."); |
||
447 | } |
||
448 | |||
449 | // Enable a language only if it has not been enabled already. |
||
450 | $enabled_languages = locale_language_list(); |
||
451 | if (!isset($enabled_languages[$language->langcode])) { |
||
452 | locale_add_language($language->langcode); |
||
453 | return $language; |
||
454 | } |
||
455 | |||
456 | return FALSE; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * {@inheritdoc} |
||
461 | */ |
||
462 | public function languageDelete(\stdClass $language) { |
||
496 | |||
497 | /** |
||
498 | * {@inheritdoc} |
||
499 | */ |
||
500 | public function configGet($name, $key = '') { |
||
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | */ |
||
507 | public function configSet($name, $key, $value) { |
||
510 | |||
511 | /** |
||
512 | * Helper function to get all permissions. |
||
513 | * |
||
514 | * @return array |
||
515 | * Array keyed by permission name, with the human-readable title as the |
||
516 | * value. |
||
517 | */ |
||
518 | View Code Duplication | protected function getAllPermissions() { |
|
525 | |||
526 | /** |
||
527 | * {@inheritdoc} |
||
528 | */ |
||
529 | public function getModuleList() { |
||
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | View Code Duplication | public function getExtensionPathList() { |
|
547 | |||
548 | /** |
||
549 | * {@inheritdoc} |
||
550 | */ |
||
551 | public function getEntityFieldTypes($entity_type) { |
||
561 | |||
562 | /** |
||
563 | * {@inheritdoc} |
||
564 | */ |
||
565 | public function isField($entity_type, $field_name) { |
||
569 | |||
570 | /** |
||
571 | * {@inheritdoc} |
||
572 | */ |
||
573 | public function clearStaticCaches() { |
||
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.