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) { |
||
132 | $current_path = getcwd(); |
||
133 | chdir(DRUPAL_ROOT); |
||
134 | user_delete((array) $user, $user->uid); |
||
135 | chdir($current_path); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function processBatch() { |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function userAddRole(\stdClass $user, $role_name) { |
||
148 | $roles = array_flip(user_roles()); |
||
149 | $role = $roles[$role_name]; |
||
150 | if (!$role) { |
||
151 | throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name)); |
||
152 | } |
||
153 | user_multiple_role_edit(array($user->uid), 'add_role', $role); |
||
154 | } |
||
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) { |
||
224 | $roles = array_flip(user_roles()); |
||
225 | $rid = $roles[$role_name]; |
||
226 | db_query('DELETE FROM {role} WHERE rid = %d', $rid); |
||
227 | if (!db_affected_rows()) { |
||
228 | throw new \RuntimeException(sprintf('No role "%s" exists.', $rid)); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | View Code Duplication | public function validateDrupalSite() { |
|
236 | if ('default' !== $this->uri) { |
||
237 | // Fake the necessary HTTP headers that Drupal needs: |
||
238 | $drupal_base_url = parse_url($this->uri); |
||
239 | // If there's no url scheme set, add http:// and re-parse the url |
||
240 | // so the host and path values are set accurately. |
||
241 | if (!array_key_exists('scheme', $drupal_base_url)) { |
||
242 | $drupal_base_url = parse_url($this->uri); |
||
243 | } |
||
244 | // Fill in defaults. |
||
245 | $drupal_base_url += array( |
||
246 | 'path' => NULL, |
||
247 | 'host' => NULL, |
||
248 | 'port' => NULL, |
||
249 | ); |
||
250 | $_SERVER['HTTP_HOST'] = $drupal_base_url['host']; |
||
251 | |||
252 | if ($drupal_base_url['port']) { |
||
253 | $_SERVER['HTTP_HOST'] .= ':' . $drupal_base_url['port']; |
||
254 | } |
||
255 | $_SERVER['SERVER_PORT'] = $drupal_base_url['port']; |
||
256 | |||
257 | if (array_key_exists('path', $drupal_base_url)) { |
||
258 | $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php'; |
||
259 | } |
||
260 | else { |
||
261 | $_SERVER['PHP_SELF'] = '/index.php'; |
||
262 | } |
||
263 | } |
||
264 | else { |
||
265 | $_SERVER['HTTP_HOST'] = 'default'; |
||
266 | $_SERVER['PHP_SELF'] = '/index.php'; |
||
267 | } |
||
268 | |||
269 | $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; |
||
270 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
||
271 | $_SERVER['REQUEST_METHOD'] = NULL; |
||
272 | |||
273 | $_SERVER['SERVER_SOFTWARE'] = NULL; |
||
274 | $_SERVER['HTTP_USER_AGENT'] = NULL; |
||
275 | |||
276 | $conf_path = conf_path(TRUE, TRUE); |
||
277 | $conf_file = $this->drupalRoot . "/$conf_path/settings.php"; |
||
278 | if (!file_exists($conf_file)) { |
||
279 | throw new BootstrapException(sprintf('Could not find a Drupal settings.php file at "%s"', $conf_file)); |
||
280 | } |
||
281 | $drushrc_file = $this->drupalRoot . "/$conf_path/drushrc.php"; |
||
282 | if (file_exists($drushrc_file)) { |
||
283 | require_once $drushrc_file; |
||
284 | } |
||
285 | } |
||
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) { |
|
294 | // The created field may come in as a readable date, rather than a |
||
295 | // timestamp. |
||
296 | if (isset($entity->created) && !is_numeric($entity->created)) { |
||
297 | $entity->created = strtotime($entity->created); |
||
298 | } |
||
299 | |||
300 | // Map human-readable node types to machine node types. |
||
301 | $types = node_get_types(); |
||
302 | foreach ($types as $type) { |
||
303 | if ($entity->type == $type->name) { |
||
304 | $entity->type = $type->type; |
||
305 | continue; |
||
306 | } |
||
307 | } |
||
308 | } |
||
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() { |
|
421 | $paths = array(); |
||
422 | |||
423 | // Get enabled modules. |
||
424 | $modules = $this->getModuleList(); |
||
425 | foreach ($modules as $module) { |
||
426 | $paths[] = $this->drupalRoot . DIRECTORY_SEPARATOR . \drupal_get_path('module', $module); |
||
427 | } |
||
428 | |||
429 | return $paths; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | protected function expandEntityFields($entity_type, \stdClass $entity, array $base_fields = array()) { |
||
436 | return parent::expandEntityFields($entity_type, $entity); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * {@inheritdoc} |
||
441 | */ |
||
442 | public function getEntityFieldTypes($entity_type, array $base_fields = array()) { |
||
443 | $taxonomy_fields = array('taxonomy' => 'taxonomy'); |
||
444 | if (!module_exists('content')) { |
||
445 | return $taxonomy_fields; |
||
446 | } |
||
447 | $return = array(); |
||
448 | $fields = content_fields(); |
||
449 | foreach ($fields as $field_name => $field) { |
||
450 | if ($this->isField($entity_type, $field_name)) { |
||
451 | $return[$field_name] = $field['type']; |
||
452 | } |
||
453 | } |
||
454 | |||
455 | $return += $taxonomy_fields; |
||
456 | |||
457 | return $return; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | public function isField($entity_type, $field_name) { |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | public function languageCreate(\stdClass $language) { |
||
478 | throw new \Exception('Creating languages is not yet implemented for Drupal 6.'); |
||
479 | } |
||
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 configSet($name, $key, $value) { |
||
501 | |||
502 | /** |
||
503 | * {@inheritdoc} |
||
504 | */ |
||
505 | public function clearStaticCaches() { |
||
508 | |||
509 | /** |
||
510 | * {@inheritdoc} |
||
511 | */ |
||
512 | public function entityCreate($entity_type, $entity) { |
||
515 | |||
516 | /** |
||
517 | * {@inheritdoc} |
||
518 | */ |
||
519 | public function entityDelete($entity_type, $entity) { |
||
522 | |||
523 | /** |
||
524 | * {@inheritdoc} |
||
525 | */ |
||
526 | public function startCollectingMail() { |
||
527 | // @todo: create a D6 version of this function |
||
528 | throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * {@inheritdoc} |
||
533 | */ |
||
534 | public function stopCollectingMail() { |
||
535 | // @todo: create a D6 version of this function |
||
536 | throw new \Exception('Mail testing is not yet implemented for Drupal 6.'); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * {@inheritdoc} |
||
541 | */ |
||
542 | public function getMail() { |
||
546 | |||
547 | /** |
||
548 | * {@inheritdoc} |
||
549 | */ |
||
550 | public function clearMail() { |
||
554 | |||
555 | /** |
||
556 | * {@inheritdoc} |
||
557 | */ |
||
558 | public function sendMail($body, $subject = '', $to = '', $langcode = '') { |
||
562 | |||
563 | } |
||
564 |
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.