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 Drupal6 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 Drupal6, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Drupal6 extends AbstractCore { |
||
11 | |||
12 | /** |
||
13 | * The available permissions. |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $availablePermissons; |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public function bootstrap() { |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function clearCache() { |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function nodeCreate($node) { |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function nodeDelete($node) { |
||
94 | |||
95 | /** |
||
96 | * Implements CoreInterface::runCron(). |
||
97 | */ |
||
98 | public function runCron() { |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function userCreate(\stdClass $user) { |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function userDelete(\stdClass $user) { |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function processBatch() { |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function userAddRole(\stdClass $user, $role_name) { |
||
155 | |||
156 | /** |
||
157 | * Fetches a user role by role name. |
||
158 | * |
||
159 | * @param string $role_name |
||
160 | * A string representing the role name. |
||
161 | * |
||
162 | * @return object |
||
163 | * A fully-loaded role object if a role with the given name exists, or FALSE |
||
164 | * otherwise. |
||
165 | * |
||
166 | * @see user_role_load() |
||
167 | */ |
||
168 | protected function userRoleLoadByName($role_name) { |
||
172 | |||
173 | /** |
||
174 | * Check to make sure that the array of permissions are valid. |
||
175 | * |
||
176 | * @param array $permissions |
||
177 | * Permissions to check. |
||
178 | * @param bool $reset |
||
179 | * Reset cached available permissions. |
||
180 | * |
||
181 | * @return bool |
||
182 | * TRUE or FALSE depending on whether the permissions are valid. |
||
183 | */ |
||
184 | protected function checkPermissions(array $permissions, $reset = FALSE) { |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function roleCreate(array $permissions) { |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function roleDelete($role_name) { |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | View Code Duplication | public function validateDrupalSite() { |
|
286 | |||
287 | /** |
||
288 | * Expands properties on the given entity object to the expected structure. |
||
289 | * |
||
290 | * @param \stdClass $entity |
||
291 | * The entity object. |
||
292 | */ |
||
293 | View Code Duplication | protected function expandEntityProperties(\stdClass $entity) { |
|
309 | |||
310 | /** |
||
311 | * Load vocabularies, optional by VIDs. |
||
312 | * |
||
313 | * @param array $vids |
||
314 | * The vids to load. |
||
315 | * |
||
316 | * @return array |
||
317 | * An array of vocabulary objects |
||
318 | */ |
||
319 | protected function taxonomyVocabularyLoadMultiple(array $vids = array()) { |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function termCreate(\stdClass $term) { |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | */ |
||
386 | public function termDelete(\stdClass $term) { |
||
394 | |||
395 | /** |
||
396 | * Helper function to get all permissions. |
||
397 | * |
||
398 | * @return array |
||
399 | * Array keyed by permission name, with the human-readable title as the |
||
400 | * value. |
||
401 | */ |
||
402 | View Code Duplication | protected function getAllPermissions() { |
|
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function getModuleList() { |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | View Code Duplication | public function getExtensionPathList() { |
|
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | protected function expandEntityFields($entity_type, \stdClass $entity, array $base_fields = array()) { |
||
438 | |||
439 | /** |
||
440 | * {@inheritdoc} |
||
441 | */ |
||
442 | public function getEntityFieldTypes($entity_type, array $base_fields = array()) { |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | public function isField($entity_type, $field_name) { |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | public function languageCreate(\stdClass $language) { |
||
480 | |||
481 | /** |
||
482 | * {@inheritdoc} |
||
483 | */ |
||
484 | public function languageDelete(\stdClass $language) { |
||
487 | |||
488 | /** |
||
489 | * {@inheritdoc} |
||
490 | */ |
||
491 | public function configGet($name, $key = '') { |
||
494 | |||
495 | /** |
||
496 | * {@inheritdoc} |
||
497 | */ |
||
498 | public function configGetOriginal($name, $key = '') { |
||
501 | |||
502 | /** |
||
503 | * {@inheritdoc} |
||
504 | */ |
||
505 | public function configSet($name, $key, $value) { |
||
508 | |||
509 | /** |
||
510 | * {@inheritdoc} |
||
511 | */ |
||
512 | public function clearStaticCaches() { |
||
515 | |||
516 | /** |
||
517 | * {@inheritdoc} |
||
518 | */ |
||
519 | public function entityCreate($entity_type, $entity) { |
||
522 | |||
523 | /** |
||
524 | * {@inheritdoc} |
||
525 | */ |
||
526 | public function entityDelete($entity_type, $entity) { |
||
529 | |||
530 | /** |
||
531 | * {@inheritdoc} |
||
532 | */ |
||
533 | public function startCollectingMail() { |
||
537 | |||
538 | /** |
||
539 | * {@inheritdoc} |
||
540 | */ |
||
541 | public function stopCollectingMail() { |
||
545 | |||
546 | /** |
||
547 | * {@inheritdoc} |
||
548 | */ |
||
549 | public function getMail() { |
||
553 | |||
554 | /** |
||
555 | * {@inheritdoc} |
||
556 | */ |
||
557 | public function clearMail() { |
||
561 | |||
562 | /** |
||
563 | * {@inheritdoc} |
||
564 | */ |
||
565 | public function sendMail($body, $subject = '', $to = '', $langcode = '') { |
||
569 | |||
570 | } |
||
571 |
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.