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() { |
||
16 | // Validate, and prepare environment for Drupal bootstrap. |
||
17 | View Code Duplication | if (!defined('DRUPAL_ROOT')) { |
|
|
|||
18 | define('DRUPAL_ROOT', $this->drupalRoot); |
||
19 | require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; |
||
20 | $this->validateDrupalSite(); |
||
21 | } |
||
22 | |||
23 | // Bootstrap Drupal. |
||
24 | chdir(DRUPAL_ROOT); |
||
25 | drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION); |
||
26 | if (empty($GLOBALS['databases'])) { |
||
27 | throw new BootstrapException('Missing database setting, verify the database configuration in settings.php.'); |
||
28 | } |
||
29 | drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | public function clearCache() { |
||
36 | drupal_flush_all_caches(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function nodeCreate($node) { |
||
43 | // Set original if not set. |
||
44 | if (!isset($node->original)) { |
||
45 | $node->original = clone $node; |
||
46 | } |
||
47 | |||
48 | // Assign authorship if none exists and `author` is passed. |
||
49 | View Code Duplication | if (!isset($node->uid) && !empty($node->author) && ($user = user_load_by_name($node->author))) { |
|
50 | $node->uid = $user->uid; |
||
51 | } |
||
52 | |||
53 | // Convert properties to expected structure. |
||
54 | $this->expandEntityProperties($node); |
||
55 | |||
56 | // Attempt to decipher any fields that may be specified. |
||
57 | $this->expandEntityFields('node', $node); |
||
58 | |||
59 | // Set defaults that haven't already been set. |
||
60 | $defaults = clone $node; |
||
61 | node_object_prepare($defaults); |
||
62 | $node = (object) array_merge((array) $defaults, (array) $node); |
||
63 | |||
64 | node_save($node); |
||
65 | return $node; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function nodeDelete($node) { |
||
72 | return node_delete($node->nid); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function nodeDeleteMultiple(array $nids) { |
||
79 | return node_delete_multiple($nids); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | * |
||
85 | * @param object $node |
||
86 | * A drupal node object. |
||
87 | * @param object $values |
||
88 | * An object with field/value parameters. |
||
89 | */ |
||
90 | public function nodeAlter($node, $values) { |
||
91 | View Code Duplication | if (empty($node) || !isset($node->nid)) { |
|
92 | var_dump(array_keys(get_object_vars($node))); |
||
93 | throw new \Exception(sprintf("%s::%s: Node was empty or had no id", get_class($this), __FUNCTION__)); |
||
94 | } |
||
95 | // Assign type (really, bundle) to values so that expansion functions will |
||
96 | // work properly. |
||
97 | $values->type = $node->type; |
||
98 | $this->expandEntityProperties($values); |
||
99 | |||
100 | // Attempt to decipher any fields that may be specified. |
||
101 | $this->expandEntityFields('node', $values); |
||
102 | foreach ($values as $k => $v) { |
||
103 | if (!property_exists($node, $k)) { |
||
104 | throw new \Exception(sprintf("%s::%s line %s: Attempt to modify an invalid field: %s", get_class($this), __LINE__, __FUNCTION__, $k)); |
||
105 | } |
||
106 | $node->{$k} = $v; |
||
107 | } |
||
108 | node_save($node); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Implements CoreInterface::runCron(). |
||
113 | */ |
||
114 | public function runCron() { |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function userCreate(\stdClass $user) { |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | * |
||
145 | * @param object $user |
||
146 | * A drupal user object. |
||
147 | * @param object $values |
||
148 | * An object with field/value parameters. |
||
149 | */ |
||
150 | public function userAlter($user, $values) { |
||
151 | View Code Duplication | if (empty($user) || !isset($user->uid)) { |
|
152 | var_dump(array_keys(get_object_vars($user))); |
||
153 | throw new \Exception(sprintf("%s::%s: User was empty or had no id", get_class($this), __FUNCTION__)); |
||
154 | } |
||
155 | // Attempt to decipher any fields that may be specified. |
||
156 | $this->expandEntityFields('user', $values); |
||
157 | foreach ($values as $k => $v) { |
||
158 | $user->{$k} = $v; |
||
159 | } |
||
160 | user_save($user); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function userDelete(\stdClass $user) { |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function userDeleteMultiple(array $uids) { |
||
174 | return user_delete_multiple($uids); |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function processBatch() { |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function userAddRole(\stdClass $user, $role_name) { |
||
201 | |||
202 | /** |
||
203 | * Check to make sure that the array of permissions are valid. |
||
204 | * |
||
205 | * @param array $permissions |
||
206 | * Permissions to check. |
||
207 | * @param bool $reset |
||
208 | * Reset cached available permissions. |
||
209 | * |
||
210 | * @return bool |
||
211 | * TRUE or FALSE depending on whether the permissions are valid. |
||
212 | */ |
||
213 | protected function checkPermissions(array $permissions, $reset = FALSE) { |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function roleCreate(array $permissions) { |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function roleDelete($role_name) { |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | View Code Duplication | public function validateDrupalSite() { |
|
322 | |||
323 | /** |
||
324 | * Expands properties on the given entity object to the expected structure. |
||
325 | * |
||
326 | * @param \stdClass $entity |
||
327 | * The entity object. |
||
328 | */ |
||
329 | View Code Duplication | protected function expandEntityProperties(\stdClass $entity) { |
|
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | public function termCreate(\stdClass $term) { |
||
392 | |||
393 | /** |
||
394 | * {@inheritdoc} |
||
395 | */ |
||
396 | public function termDelete(\stdClass $term) { |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function languageCreate(\stdClass $language) { |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | public function languageDelete(\stdClass $language) { |
||
467 | |||
468 | /** |
||
469 | * {@inheritdoc} |
||
470 | */ |
||
471 | public function configGet($name, $key = '') { |
||
474 | |||
475 | /** |
||
476 | * {@inheritdoc} |
||
477 | */ |
||
478 | public function configSet($name, $key, $value) { |
||
481 | |||
482 | /** |
||
483 | * Helper function to get all permissions. |
||
484 | * |
||
485 | * @return array |
||
486 | * Array keyed by permission name, with the human-readable title as the |
||
487 | * value. |
||
488 | */ |
||
489 | View Code Duplication | protected function getAllPermissions() { |
|
496 | |||
497 | /** |
||
498 | * {@inheritdoc} |
||
499 | */ |
||
500 | public function getModuleList() { |
||
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | */ |
||
507 | View Code Duplication | public function getExtensionPathList() { |
|
518 | |||
519 | /** |
||
520 | * {@inheritdoc} |
||
521 | */ |
||
522 | public function getEntityFieldTypes($entity_type) { |
||
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | public function isField($entity_type, $field_name) { |
||
540 | |||
541 | /** |
||
542 | * {@inheritdoc} |
||
543 | */ |
||
544 | public function clearStaticCaches() { |
||
547 | |||
548 | } |
||
549 |
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.