Conditions | 19 |
Paths | 721 |
Total Lines | 131 |
Code Lines | 72 |
Lines | 10 |
Ratio | 7.63 % |
Changes | 10 | ||
Bugs | 3 | Features | 4 |
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 |
||
22 | function df_admin_reset_scenario($scenario, $rollback = TRUE, $seed = TRUE) { |
||
23 | // Check to see if scenario is not enabled. |
||
24 | if (!module_exists($scenario)) { |
||
25 | drupal_set_message(t('@scenario must be enabled before a reset can occur.', array('@scenario' => $scenario)), 'error'); |
||
26 | return FALSE; |
||
27 | } |
||
28 | |||
29 | // Get list of modules implementing Migrate API. |
||
30 | migrate_get_module_apis(); |
||
31 | |||
32 | // Set up operations array. |
||
33 | $operations = array(); |
||
34 | |||
35 | // Revert Features to ensure proper DFS reset. |
||
36 | $operations[] = array('df_admin_revert_all', array('')); |
||
37 | $operations[] = array('drupal_set_message', array(t('Reverted Features after scenario enablement.'), 'status')); |
||
38 | $operations[] = array('_df_admin_watchdog_revert_features', array($scenario)); |
||
39 | |||
40 | // Rebuild defaultconfig from modules. |
||
41 | $operations[] = array('defaultconfig_rebuild_all', array('')); |
||
42 | $operations[] = array('drupal_set_message', array(t('Defaultconfig rebuilt after scenario enablement.'), 'status')); |
||
43 | $operations[] = array('_df_admin_watchdog_defaultconfig_rebuild', array($scenario)); |
||
44 | |||
45 | // Set up pre-import routines via hook_df_pre_import(). |
||
46 | $routines = array(); |
||
47 | $routines = module_invoke_all('df_pre_import'); |
||
48 | drupal_alter('df_pre_import', $routines); |
||
49 | |||
50 | // Add pre-import functions to the batch process. |
||
51 | foreach ($routines as $routine) { |
||
52 | $operations[] = $routine; |
||
53 | } |
||
54 | |||
55 | // Set up migrations array via hook_df_import(). |
||
56 | $migrations = array(); |
||
57 | $migrations = module_invoke_all('df_import'); |
||
58 | drupal_alter('df_import', $migrations); |
||
59 | |||
60 | // Check for available migrations based on the scenario argument. |
||
61 | if (isset($migrations[$scenario]) && !empty($migrations[$scenario])) { |
||
62 | |||
63 | if ($rollback == TRUE) { |
||
64 | // Queue migrations to rollback in the opposite order provided. |
||
65 | View Code Duplication | foreach (array_reverse($migrations[$scenario]) as $machine_name) { |
|
66 | migrate_static_registration(array($machine_name)); |
||
67 | $migration = Migration::getInstance($machine_name); |
||
68 | $operations[] = array('migrate_ui_batch', array('rollback', $machine_name, array('unit' => 'items', 'value' => ''), FALSE)); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // Queue migrations to import in the provided order. |
||
73 | View Code Duplication | foreach ($migrations[$scenario] as $machine_name) { |
|
74 | migrate_static_registration(array($machine_name)); |
||
75 | $migration = Migration::getInstance($machine_name); |
||
76 | $operations[] = array('migrate_ui_batch', array('import', $machine_name, array('unit' => 'items', 'value' => ''), FALSE)); |
||
77 | } |
||
78 | |||
79 | // Confirm if there was an attempt to run scenario migrations. |
||
80 | $migrations_count = count($migrations[$scenario]); |
||
81 | if ($migrations_count > 0 && drupal_is_cli()) { |
||
82 | $message = $migrations_count . ' demo migrations attempted.'; |
||
83 | $operations[] = array('drush_print', array($message, 0, NULL, TRUE)); |
||
84 | } |
||
85 | |||
86 | } |
||
87 | |||
88 | // Set up post-import routines via hook_df_post_import(). |
||
89 | $routines = array(); |
||
90 | $routines = module_invoke_all('df_post_import'); |
||
91 | drupal_alter('df_post_import', $routines); |
||
92 | |||
93 | // Add post-import functions to the batch process. |
||
94 | foreach ($routines as $routine) { |
||
95 | $operations[] = $routine; |
||
96 | } |
||
97 | |||
98 | // Clear caches post-operations. |
||
99 | $operations[] = array('node_access_rebuild', array('')); |
||
100 | $operations[] = array('drupal_flush_all_caches', array('')); |
||
101 | $operations[] = array('variable_set', array('menu_rebuild_needed', TRUE)); |
||
102 | $operations[] = array('drupal_set_message', array(t('All caches have been cleared.'), 'status')); |
||
103 | $operations[] = array('_df_admin_watchdog_cache_clear', array($scenario)); |
||
104 | |||
105 | // Clear messages for non-Drush users providing a cleaner scenario reset. |
||
106 | if (!drupal_is_cli()) { |
||
107 | $messages = array('completed', 'status', 'warning'); |
||
108 | foreach ($messages as $message) { |
||
109 | $operations[] = array('drupal_get_messages', array($message, TRUE)); |
||
110 | } |
||
111 | } |
||
112 | else { |
||
113 | // Determine the current alias |
||
114 | $alias_context = drush_get_context('alias'); |
||
115 | $alias = !empty($alias_context) ? $alias_context : '@self'; |
||
116 | |||
117 | // Seed derivatives |
||
118 | if ($seed == TRUE) { |
||
119 | $operations[] = array('drush_invoke_process', array($alias, 'df-sd', array(), array(), TRUE)); |
||
120 | } |
||
121 | // Clear caches before we start the batch, to prevent conflicts with new module additions |
||
122 | // This fixes a specific bug with the --pre option, but there's no harm in calling it every time df-rs is called |
||
123 | drush_invoke_process($alias, 'cc', array('all')); |
||
124 | } |
||
125 | |||
126 | // Rollback and Import migration operations via Batch API. |
||
127 | if (count($operations) > 0) { |
||
128 | $batch = array( |
||
129 | 'operations' => $operations, |
||
130 | 'title' => t('Import processing'), |
||
131 | 'init_message' => t('Starting import process'), |
||
132 | 'file' => drupal_get_path('module', 'migrate_ui') . '/migrate_ui.pages.inc', |
||
133 | 'progress_message' => t('Importing Demo Framework Scenario...'), |
||
134 | 'error_message' => t('An error occurred. Some or all of the import processing has failed.'), |
||
135 | 'finished' => 'df_admin_batch_finish', |
||
136 | ); |
||
137 | batch_set($batch); |
||
138 | if (!drupal_is_cli()) { |
||
139 | $options = array('query' => array('token' => drupal_get_token(DRUPAL_ROOT . '/df/imagestyles'))); |
||
140 | // The ability to send an array for $redirect is undocumented on Drupal.org, yay! |
||
141 | batch_process(array('admin/df/imagestyles', $options)); |
||
142 | } |
||
143 | else { |
||
144 | drush_backend_batch_process(); |
||
145 | } |
||
146 | } |
||
147 | // No operations present, non-drush users get redirected. |
||
148 | if (!drupal_is_cli() && empty($operations)) { |
||
149 | drupal_goto(''); |
||
150 | } |
||
151 | return TRUE; |
||
152 | } |
||
153 |
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.