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() { |
||
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 | node_delete($node->nid); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Implements CoreInterface::runCron(). |
||
77 | */ |
||
78 | public function runCron() { |
||
79 | return drupal_cron_run(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function userCreate(\stdClass $user) { |
||
86 | // Default status to TRUE if not explicitly creating a blocked user. |
||
87 | if (!isset($user->status)) { |
||
88 | $user->status = 1; |
||
89 | } |
||
90 | |||
91 | // Clone user object, otherwise user_save() changes the password to the |
||
92 | // hashed password. |
||
93 | $account = clone $user; |
||
94 | |||
95 | // Attempt to decipher any fields that may be specified. |
||
96 | $this->expandEntityFields('user', $account); |
||
97 | |||
98 | user_save($account, (array) $account); |
||
99 | |||
100 | // Store UID. |
||
101 | $user->uid = $account->uid; |
||
102 | } |
||
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) { |
||
148 | $available = &drupal_static(__FUNCTION__); |
||
149 | |||
150 | if (!isset($available) || $reset) { |
||
151 | $available = array_keys(module_invoke_all('permission')); |
||
152 | } |
||
153 | |||
154 | $valid = TRUE; |
||
155 | foreach ($permissions as $permission) { |
||
156 | if (!in_array($permission, $available)) { |
||
157 | $valid = FALSE; |
||
158 | } |
||
159 | } |
||
160 | return $valid; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function roleCreate(array $permissions) { |
||
167 | |||
168 | // Both machine name and permission title are allowed. |
||
169 | $all_permissions = $this->getAllPermissions(); |
||
170 | |||
171 | foreach ($permissions as $key => $name) { |
||
172 | if (!isset($all_permissions[$name])) { |
||
173 | $search = array_search($name, $all_permissions); |
||
174 | if (!$search) { |
||
175 | throw new \RuntimeException(sprintf("No permission '%s' exists.", $name)); |
||
176 | } |
||
177 | $permissions[$key] = $search; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | // Create new role. |
||
182 | $role = new \stdClass(); |
||
183 | $role->name = $this->random->name(8); |
||
184 | user_role_save($role); |
||
185 | user_role_grant_permissions($role->rid, $permissions); |
||
186 | |||
187 | if ($role && !empty($role->rid)) { |
||
188 | return $role->name; |
||
189 | } |
||
190 | |||
191 | throw new \RuntimeException(sprintf('Failed to create a role with "" permission(s).', implode(', ', $permissions))); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function roleDelete($role_name) { |
||
198 | $role = user_role_load_by_name($role_name); |
||
199 | user_role_delete((int) $role->rid); |
||
200 | } |
||
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) { |
||
343 | if (!module_exists('locale')) { |
||
344 | 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__)); |
||
345 | } |
||
346 | include_once DRUPAL_ROOT . '/includes/iso.inc'; |
||
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 configSet($name, $key, $value) { |
||
418 | |||
419 | /** |
||
420 | * Helper function to get all permissions. |
||
421 | * |
||
422 | * @return array |
||
423 | * Array keyed by permission name, with the human-readable title as the |
||
424 | * value. |
||
425 | */ |
||
426 | View Code Duplication | protected function getAllPermissions() { |
|
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | */ |
||
437 | public function getModuleList() { |
||
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | View Code Duplication | public function getExtensionPathList() { |
|
455 | |||
456 | /** |
||
457 | * {@inheritdoc} |
||
458 | */ |
||
459 | public function getEntityFieldTypes($entity_type) { |
||
469 | |||
470 | /** |
||
471 | * {@inheritdoc} |
||
472 | */ |
||
473 | public function isField($entity_type, $field_name) { |
||
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | */ |
||
481 | public function clearStaticCaches() { |
||
484 | |||
485 | } |
||
486 |
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.