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() { |
||
36 | drupal_flush_all_caches(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function nodeCreate($node) { |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function nodeDelete($node) { |
||
74 | |||
75 | /** |
||
76 | * Implements CoreInterface::runCron(). |
||
77 | */ |
||
78 | public function runCron() { |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function userCreate(\stdClass $user) { |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public function userDelete(\stdClass $user) { |
||
108 | user_cancel(array(), $user->uid, 'user_cancel_delete'); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | public function processBatch() { |
||
115 | $batch =& batch_get(); |
||
116 | $batch['progressive'] = FALSE; |
||
117 | batch_process(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function userAddRole(\stdClass $user, $role_name) { |
||
124 | $role = user_role_load_by_name($role_name); |
||
125 | |||
126 | if (!$role) { |
||
127 | throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name)); |
||
128 | } |
||
129 | |||
130 | user_multiple_role_edit(array($user->uid), 'add_role', $role->rid); |
||
131 | $account = user_load($user->uid); |
||
132 | $user->roles = $account->roles; |
||
133 | |||
134 | } |
||
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) { |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function roleCreate(array $permissions) { |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function roleDelete($role_name) { |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | View Code Duplication | public function validateDrupalSite() { |
|
206 | if ('default' !== $this->uri) { |
||
207 | // Fake the necessary HTTP headers that Drupal needs: |
||
208 | $drupal_base_url = parse_url($this->uri); |
||
209 | // If there's no url scheme set, add http:// and re-parse the url |
||
210 | // so the host and path values are set accurately. |
||
211 | if (!array_key_exists('scheme', $drupal_base_url)) { |
||
212 | $drupal_base_url = parse_url($this->uri); |
||
213 | } |
||
214 | // Fill in defaults. |
||
215 | $drupal_base_url += array( |
||
216 | 'path' => NULL, |
||
217 | 'host' => NULL, |
||
218 | 'port' => NULL, |
||
219 | ); |
||
220 | $_SERVER['HTTP_HOST'] = $drupal_base_url['host']; |
||
221 | |||
222 | if ($drupal_base_url['port']) { |
||
223 | $_SERVER['HTTP_HOST'] .= ':' . $drupal_base_url['port']; |
||
224 | } |
||
225 | $_SERVER['SERVER_PORT'] = $drupal_base_url['port']; |
||
226 | |||
227 | if (array_key_exists('path', $drupal_base_url)) { |
||
228 | $_SERVER['PHP_SELF'] = $drupal_base_url['path'] . '/index.php'; |
||
229 | } |
||
230 | else { |
||
231 | $_SERVER['PHP_SELF'] = '/index.php'; |
||
232 | } |
||
233 | } |
||
234 | else { |
||
235 | $_SERVER['HTTP_HOST'] = 'default'; |
||
236 | $_SERVER['PHP_SELF'] = '/index.php'; |
||
237 | } |
||
238 | |||
239 | $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; |
||
240 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
||
241 | $_SERVER['REQUEST_METHOD'] = NULL; |
||
242 | |||
243 | $_SERVER['SERVER_SOFTWARE'] = NULL; |
||
244 | $_SERVER['HTTP_USER_AGENT'] = NULL; |
||
245 | |||
246 | $conf_path = conf_path(TRUE, TRUE); |
||
247 | $conf_file = $this->drupalRoot . "/$conf_path/settings.php"; |
||
248 | if (!file_exists($conf_file)) { |
||
249 | throw new BootstrapException(sprintf('Could not find a Drupal settings.php file at "%s"', $conf_file)); |
||
250 | } |
||
251 | $drushrc_file = $this->drupalRoot . "/$conf_path/drushrc.php"; |
||
252 | if (file_exists($drushrc_file)) { |
||
253 | require_once $drushrc_file; |
||
254 | } |
||
255 | } |
||
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) { |
|
264 | // The created field may come in as a readable date, rather than a |
||
265 | // timestamp. |
||
266 | if (isset($entity->created) && !is_numeric($entity->created)) { |
||
267 | $entity->created = strtotime($entity->created); |
||
268 | } |
||
269 | |||
270 | // Map human-readable node types to machine node types. |
||
271 | $types = \node_type_get_types(); |
||
272 | foreach ($types as $type) { |
||
273 | if ($entity->type == $type->name) { |
||
274 | $entity->type = $type->type; |
||
275 | continue; |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function termCreate(\stdClass $term) { |
||
284 | // Map vocabulary names to vid, these take precedence over machine names. |
||
285 | View Code Duplication | if (!isset($term->vid)) { |
|
286 | $vocabularies = \taxonomy_get_vocabularies(); |
||
287 | foreach ($vocabularies as $vid => $vocabulary) { |
||
288 | if ($vocabulary->name == $term->vocabulary_machine_name) { |
||
289 | $term->vid = $vocabulary->vid; |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | if (!isset($term->vid)) { |
||
295 | |||
296 | // Try to load vocabulary by machine name. |
||
297 | $vocabularies = \taxonomy_vocabulary_load_multiple(FALSE, array( |
||
298 | 'machine_name' => $term->vocabulary_machine_name, |
||
299 | )); |
||
300 | if (!empty($vocabularies)) { |
||
301 | $vids = array_keys($vocabularies); |
||
302 | $term->vid = reset($vids); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | // If `parent` is set, look up a term in this vocab with that name. |
||
307 | View Code Duplication | if (isset($term->parent)) { |
|
308 | $parent = \taxonomy_get_term_by_name($term->parent, $term->vocabulary_machine_name); |
||
309 | if (!empty($parent)) { |
||
310 | $parent = reset($parent); |
||
311 | $term->parent = $parent->tid; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | if (empty($term->vid)) { |
||
316 | throw new \Exception(sprintf('No "%s" vocabulary found.')); |
||
317 | } |
||
318 | |||
319 | // Attempt to decipher any fields that may be specified. |
||
320 | $this->expandEntityFields('taxonomy_term', $term); |
||
321 | |||
322 | \taxonomy_term_save($term); |
||
323 | |||
324 | return $term; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function termDelete(\stdClass $term) { |
||
331 | $status = 0; |
||
332 | if (isset($term->tid)) { |
||
333 | $status = \taxonomy_term_delete($term->tid); |
||
334 | } |
||
335 | // Will be SAVED_DELETED (3) on success. |
||
336 | return $status; |
||
337 | } |
||
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'; |
||
347 | include_once DRUPAL_ROOT . '/includes/locale.inc'; |
||
348 | |||
349 | // Get all predefined languages, regardless if they are enabled or not. |
||
350 | $predefined_languages = _locale_get_predefined_list(); |
||
351 | |||
352 | // If the language code is not valid then throw an InvalidArgumentException. |
||
353 | if (!isset($predefined_languages[$language->langcode])) { |
||
354 | throw new InvalidArgumentException("There is no predefined language with langcode '{$language->langcode}'."); |
||
355 | } |
||
356 | |||
357 | // Enable a language only if it has not been enabled already. |
||
358 | $enabled_languages = locale_language_list(); |
||
359 | if (!isset($enabled_languages[$language->langcode])) { |
||
360 | locale_add_language($language->langcode); |
||
361 | return $language; |
||
362 | } |
||
363 | |||
364 | return FALSE; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * {@inheritdoc} |
||
369 | */ |
||
370 | public function languageDelete(\stdClass $language) { |
||
371 | $langcode = $language->langcode; |
||
372 | // Do not remove English or the default language. |
||
373 | if (!in_array($langcode, array(language_default('language'), 'en'))) { |
||
374 | // @see locale_languages_delete_form_submit(). |
||
375 | $languages = language_list(); |
||
376 | if (isset($languages[$langcode])) { |
||
377 | // Remove translations first. |
||
378 | db_delete('locales_target') |
||
379 | ->condition('language', $langcode) |
||
380 | ->execute(); |
||
381 | cache_clear_all('locale:' . $langcode, 'cache'); |
||
382 | // With no translations, this removes existing JavaScript translations |
||
383 | // file. |
||
384 | _locale_rebuild_js($langcode); |
||
385 | // Remove the language. |
||
386 | db_delete('languages') |
||
387 | ->condition('language', $langcode) |
||
388 | ->execute(); |
||
389 | db_update('node') |
||
390 | ->fields(array('language' => '')) |
||
391 | ->condition('language', $langcode) |
||
392 | ->execute(); |
||
393 | if ($languages[$langcode]->enabled) { |
||
394 | variable_set('language_count', variable_get('language_count', 1) - 1); |
||
395 | } |
||
396 | module_invoke_all('multilingual_settings_changed'); |
||
397 | drupal_static_reset('language_list'); |
||
398 | } |
||
399 | |||
400 | // Changing the language settings impacts the interface: |
||
401 | cache_clear_all('*', 'cache_page', TRUE); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function configGet($name, $key = '') { |
||
409 | throw new \Exception('Getting config is not yet implemented for Drupal 7.'); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | */ |
||
415 | public function configSet($name, $key, $value) { |
||
416 | throw new \Exception('Setting config is not yet implemented for Drupal 7.'); |
||
417 | } |
||
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() { |
|
445 | $paths = array(); |
||
446 | |||
447 | // Get enabled modules. |
||
448 | $modules = $this->getModuleList(); |
||
449 | foreach ($modules as $module) { |
||
450 | $paths[] = $this->drupalRoot . DIRECTORY_SEPARATOR . \drupal_get_path('module', $module); |
||
451 | } |
||
452 | |||
453 | return $paths; |
||
454 | } |
||
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() { |
||
482 | drupal_static_reset(); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * {@inheritdoc} |
||
487 | */ |
||
488 | public function entityCreate($entity_type, $entity) { |
||
489 | // @todo: create a D7 version of this function |
||
490 | throw new \Exception('Creation of entities via the generic Entity API is not yet implemented for Drupal 7.'); |
||
492 | |||
493 | /** |
||
494 | * {@inheritdoc} |
||
495 | */ |
||
496 | public function entityDelete($entity_type, $entity) { |
||
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | public function startCollectingMail() { |
||
505 | // @todo: create a D7 version of this function |
||
506 | throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * {@inheritdoc} |
||
511 | */ |
||
512 | public function stopCollectingMail() { |
||
513 | // @todo: create a D7 version of this function |
||
514 | throw new \Exception('Mail testing is not yet implemented for Drupal 7.'); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * {@inheritdoc} |
||
519 | */ |
||
520 | public function getMail() { |
||
524 | |||
525 | /** |
||
526 | * {@inheritdoc} |
||
527 | */ |
||
528 | public function clearMail() { |
||
532 | |||
533 | /** |
||
534 | * {@inheritdoc} |
||
535 | */ |
||
536 | public function sendMail($body, $subject = '', $to = '', $langcode = '') { |
||
540 | |||
541 | } |
||
542 |
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.