Conditions | 11 |
Paths | 33 |
Total Lines | 78 |
Code Lines | 43 |
Lines | 12 |
Ratio | 15.38 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
11 | function df_admin_enable_sub_scenario($module) { |
||
12 | // If the scenario is already enabled, exit. |
||
13 | View Code Duplication | if (module_exists($module)) { |
|
|
|||
14 | drupal_set_message(t('Sub-scenario @module is already installed.', array('@module' => $module)), 'status'); |
||
15 | return FALSE; |
||
16 | } |
||
17 | |||
18 | // Load info file to grab base scenario. |
||
19 | View Code Duplication | if (!$info = drupal_parse_info_file(drupal_get_path('module', $module) . '/' . $module . '.info')) { |
|
20 | drupal_set_message(t('Unable to load Scenario .info file'), 'error'); |
||
21 | return FALSE; |
||
22 | } |
||
23 | |||
24 | // Check to see if this is a valid sub-scenario. |
||
25 | if (!isset($info['base_scenario'])) { |
||
26 | drupal_set_message(t('@module\'s info file is missing the base_scenario key.', array('@module' => $module)), 'error'); |
||
27 | return FALSE; |
||
28 | } |
||
29 | |||
30 | // Check if another sub-scenario is installed. |
||
31 | if ($sub_scenario = variable_get('df_admin_installed_sub_scenario', FALSE)) { |
||
32 | module_disable($sub_scenario); |
||
33 | drupal_uninstall_modules(array($sub_scenario)); |
||
34 | df_admin_revert_all(); |
||
35 | } |
||
36 | |||
37 | // Check if another scenario is installed. |
||
38 | $scenario = variable_get('df_admin_installed_scenario', FALSE); |
||
39 | $base_enabled = $scenario == $info['base_scenario']; |
||
40 | |||
41 | // Roll back existing scenario content if our base scenario is already enabled. |
||
42 | if ($base_enabled) { |
||
43 | $migrations = module_invoke_all('df_import'); |
||
44 | drupal_alter('df_import', $migrations); |
||
45 | $operations = array(); |
||
46 | View Code Duplication | foreach (array_reverse($migrations[$scenario]) as $machine_name) { |
|
47 | migrate_static_registration(array($machine_name)); |
||
48 | $operations[] = array('migrate_ui_batch', array('rollback', $machine_name, array('unit' => 'items', 'value' => ''), FALSE)); |
||
49 | } |
||
50 | $batch = array( |
||
51 | 'operations' => $operations, |
||
52 | 'title' => t('Sub-scenario base scenario rollback.'), |
||
53 | 'init_message' => t('Rolling back base scenario.'), |
||
54 | 'file' => drupal_get_path('module', 'migrate_ui') . '/migrate_ui.pages.inc', |
||
55 | 'progress_message' => t('Rolling back base scenario migrations...'), |
||
56 | 'error_message' => t('An error occurred. Some or all of the rollback processing has failed.') |
||
57 | ); |
||
58 | batch_set($batch); |
||
59 | drush_backend_batch_process(); |
||
60 | } |
||
61 | // Another scenario is installed, uninstall it now. |
||
62 | elseif ($scenario) { |
||
63 | // Uninstall currently installed scenario, if any is installed. |
||
64 | module_load_include('inc', 'df_admin', 'df_admin.uninstall'); |
||
65 | df_admin_uninstall_scenario(); |
||
66 | } |
||
67 | |||
68 | // If the base scenario isn't already enabled, do so now. |
||
69 | if (!$base_enabled) { |
||
70 | if (!df_admin_enable_scenario($info['base_scenario'])) { |
||
71 | return FALSE; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // Enable the sub-scenario. |
||
76 | if (!_df_admin_enable_feature($module)) { |
||
77 | return FALSE; |
||
78 | } |
||
79 | |||
80 | // Revert all features. |
||
81 | df_admin_revert_all(); |
||
82 | |||
83 | // Mark this sub-scenario as enabled. |
||
84 | variable_set('df_admin_installed_sub_scenario', $module); |
||
85 | |||
86 | // Give our caller the base_scenario name, so it knows to revert features or migrate content. |
||
87 | return $info['base_scenario']; |
||
88 | } |
||
89 |
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.