Total Complexity | 62 |
Total Lines | 510 |
Duplicated Lines | 1.37 % |
Changes | 0 |
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 IndexesController 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.
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 IndexesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class IndexesController extends BaseController |
||
11 | { |
||
12 | public $_name = 'IndexesController'; |
||
13 | |||
14 | public function render() |
||
76 | } |
||
77 | |||
78 | public function doDefault($msg = '') |
||
79 | { |
||
80 | $conf = $this->conf; |
||
81 | $misc = $this->misc; |
||
82 | $lang = $this->lang; |
||
83 | $data = $misc->getDatabaseAccessor(); |
||
84 | |||
85 | $indPre = function (&$rowdata, $actions) use ($data, $lang) { |
||
86 | if ($data->phpBool($rowdata->fields['indisprimary'])) { |
||
87 | $rowdata->fields['+constraints'] = $lang['strprimarykey']; |
||
88 | $actions['drop']['disable'] = true; |
||
89 | } elseif ($data->phpBool($rowdata->fields['indisunique'])) { |
||
90 | $rowdata->fields['+constraints'] = $lang['struniquekey']; |
||
91 | $actions['drop']['disable'] = true; |
||
92 | } else { |
||
93 | $rowdata->fields['+constraints'] = ''; |
||
94 | } |
||
95 | |||
96 | return $actions; |
||
97 | }; |
||
98 | if (!isset($_REQUEST['subject'])) { |
||
99 | $_REQUEST['subject'] = 'table'; |
||
100 | } |
||
101 | |||
102 | $subject = urlencode($_REQUEST['subject']); |
||
103 | $object = urlencode($_REQUEST[$_REQUEST['subject']]); |
||
104 | |||
105 | $this->printTrail($subject); |
||
106 | $this->printTabs($subject, 'indexes'); |
||
107 | $this->printMsg($msg); |
||
108 | |||
109 | $indexes = $data->getIndexes($_REQUEST[$_REQUEST['subject']]); |
||
110 | |||
111 | $columns = [ |
||
112 | 'index' => [ |
||
113 | 'title' => $lang['strname'], |
||
114 | 'field' => Decorator::field('indname'), |
||
115 | ], |
||
116 | 'definition' => [ |
||
117 | 'title' => $lang['strdefinition'], |
||
118 | 'field' => Decorator::field('inddef'), |
||
119 | ], |
||
120 | 'constraints' => [ |
||
121 | 'title' => $lang['strconstraints'], |
||
122 | 'field' => Decorator::field('+constraints'), |
||
123 | 'type' => 'verbatim', |
||
124 | 'params' => ['align' => 'center'], |
||
125 | ], |
||
126 | 'clustered' => [ |
||
127 | 'title' => $lang['strclustered'], |
||
128 | 'field' => Decorator::field('indisclustered'), |
||
129 | 'type' => 'yesno', |
||
130 | ], |
||
131 | 'actions' => [ |
||
132 | 'title' => $lang['stractions'], |
||
133 | ], |
||
134 | 'comment' => [ |
||
135 | 'title' => $lang['strcomment'], |
||
136 | 'field' => Decorator::field('idxcomment'), |
||
137 | ], |
||
138 | ]; |
||
139 | |||
140 | $actions = [ |
||
141 | 'cluster' => [ |
||
142 | 'content' => $lang['strclusterindex'], |
||
143 | 'attr' => [ |
||
144 | 'href' => [ |
||
145 | 'url' => 'indexes.php', |
||
146 | 'urlvars' => [ |
||
147 | 'action' => 'confirm_cluster_index', |
||
148 | $subject => $object, |
||
149 | 'index' => Decorator::field('indname'), |
||
150 | ], |
||
151 | ], |
||
152 | ], |
||
153 | ], |
||
154 | 'reindex' => [ |
||
155 | 'content' => $lang['strreindex'], |
||
156 | 'attr' => [ |
||
157 | 'href' => [ |
||
158 | 'url' => 'indexes.php', |
||
159 | 'urlvars' => [ |
||
160 | 'action' => 'reindex', |
||
161 | $subject => $object, |
||
162 | 'index' => Decorator::field('indname'), |
||
163 | ], |
||
164 | ], |
||
165 | ], |
||
166 | ], |
||
167 | 'drop' => [ |
||
168 | 'content' => $lang['strdrop'], |
||
169 | 'attr' => [ |
||
170 | 'href' => [ |
||
171 | 'url' => 'indexes.php', |
||
172 | 'urlvars' => [ |
||
173 | 'action' => 'confirm_drop_index', |
||
174 | $subject => $object, |
||
175 | 'index' => Decorator::field('indname'), |
||
176 | ], |
||
177 | ], |
||
178 | ], |
||
179 | ], |
||
180 | ]; |
||
181 | |||
182 | echo $this->printTable($indexes, $columns, $actions, 'indexes-indexes', $lang['strnoindexes'], $indPre); |
||
183 | |||
184 | $this->printNavLinks([ |
||
185 | 'create' => [ |
||
186 | 'attr' => [ |
||
187 | 'href' => [ |
||
188 | 'url' => 'indexes.php', |
||
189 | 'urlvars' => [ |
||
190 | 'action' => 'create_index', |
||
191 | 'server' => $_REQUEST['server'], |
||
192 | 'database' => $_REQUEST['database'], |
||
193 | 'schema' => $_REQUEST['schema'], |
||
194 | $subject => $object, |
||
195 | 'subject' => $subject, |
||
196 | ], |
||
197 | ], |
||
198 | ], |
||
199 | 'content' => $lang['strcreateindex'], |
||
200 | ], |
||
201 | ], 'indexes-indexes', get_defined_vars()); |
||
202 | } |
||
203 | |||
204 | public function doTree() |
||
205 | { |
||
206 | $conf = $this->conf; |
||
207 | $misc = $this->misc; |
||
208 | $lang = $this->lang; |
||
209 | $data = $misc->getDatabaseAccessor(); |
||
210 | if (!isset($_REQUEST['subject'])) { |
||
211 | $_REQUEST['subject'] = 'table'; |
||
212 | } |
||
213 | |||
214 | $subject = urlencode($_REQUEST['subject']); |
||
215 | $object = urlencode($_REQUEST[$_REQUEST['subject']]); |
||
216 | |||
217 | $indexes = $data->getIndexes($object); |
||
218 | |||
219 | $reqvars = $misc->getRequestVars($subject); |
||
220 | |||
221 | $getIcon = function ($f) { |
||
222 | if ($f['indisprimary'] == 't') { |
||
223 | return 'PrimaryKey'; |
||
224 | } |
||
225 | |||
226 | if ($f['indisunique'] == 't') { |
||
227 | return 'UniqueConstraint'; |
||
228 | } |
||
229 | |||
230 | return 'Index'; |
||
231 | }; |
||
232 | |||
233 | $attrs = [ |
||
234 | 'text' => Decorator::field('indname'), |
||
235 | 'icon' => Decorator::callback($getIcon), |
||
236 | ]; |
||
237 | |||
238 | return $this->printTree($indexes, $attrs, 'indexes'); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Show confirmation of cluster index and perform actual cluster |
||
243 | */ |
||
244 | public function doClusterIndex($confirm) |
||
245 | { |
||
246 | $conf = $this->conf; |
||
247 | $misc = $this->misc; |
||
248 | $lang = $this->lang; |
||
249 | $data = $misc->getDatabaseAccessor(); |
||
250 | |||
251 | if ($confirm) { |
||
252 | // Default analyze to on |
||
253 | $_REQUEST['analyze'] = true; |
||
254 | |||
255 | $this->printTrail('index'); |
||
256 | $this->printTitle($lang['strclusterindex'], 'pg.index.cluster'); |
||
257 | |||
258 | echo '<p>', sprintf($lang['strconfcluster'], $misc->printVal($_REQUEST['index'])), '</p>' . "\n"; |
||
259 | |||
260 | echo '<form action="' . SUBFOLDER . '/src/views/indexes.php" method="post">' . "\n"; |
||
261 | echo '<p><input type="checkbox" id="analyze" name="analyze"', (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), ' />'; |
||
262 | echo "<label for=\"analyze\">{$lang['stranalyze']}</label></p>" . "\n"; |
||
263 | echo '<input type="hidden" name="action" value="cluster_index" />' . "\n"; |
||
264 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), '" />' . "\n"; |
||
265 | echo '<input type="hidden" name="index" value="', htmlspecialchars($_REQUEST['index']), '" />' . "\n"; |
||
266 | echo $misc->form; |
||
267 | echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strclusterindex']}\" />" . "\n"; |
||
268 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />" . "\n"; |
||
269 | echo '</form>' . "\n"; |
||
270 | } else { |
||
271 | $status = $data->clusterIndex($_POST['table'], $_POST['index']); |
||
272 | if ($status == 0) { |
||
273 | if (isset($_POST['analyze'])) { |
||
274 | $status = $data->analyzeDB($_POST['table']); |
||
275 | if ($status == 0) { |
||
276 | $this->doDefault($lang['strclusteredgood'] . ' ' . $lang['stranalyzegood']); |
||
277 | } else { |
||
278 | $this->doDefault($lang['stranalyzebad']); |
||
279 | } |
||
280 | } else { |
||
281 | $this->doDefault($lang['strclusteredgood']); |
||
282 | } |
||
283 | } else { |
||
284 | $this->doDefault($lang['strclusteredbad']); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | public function doReindex() |
||
290 | { |
||
291 | $conf = $this->conf; |
||
292 | $misc = $this->misc; |
||
293 | $lang = $this->lang; |
||
294 | $data = $misc->getDatabaseAccessor(); |
||
295 | $status = $data->reindex('INDEX', $_REQUEST['index']); |
||
296 | if ($status == 0) { |
||
297 | $this->doDefault($lang['strreindexgood']); |
||
298 | } else { |
||
299 | $this->doDefault($lang['strreindexbad']); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Displays a screen where they can enter a new index |
||
305 | */ |
||
306 | public function doCreateIndex($msg = '') |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Actually creates the new index in the database |
||
441 | * @@ Note: this function can't handle columns with commas in them |
||
442 | */ |
||
443 | public function doSaveCreateIndex() |
||
444 | { |
||
445 | $conf = $this->conf; |
||
446 | $misc = $this->misc; |
||
447 | $lang = $this->lang; |
||
448 | $data = $misc->getDatabaseAccessor(); |
||
449 | |||
450 | if (!isset($_POST['subject'])) { |
||
451 | $_POST['subject'] = 'table'; |
||
452 | } |
||
453 | $subject = urlencode($_POST['subject']); |
||
454 | $object = urlencode($_POST[$subject]); |
||
455 | |||
456 | // Handle databases that don't have partial indexes |
||
457 | if (!isset($_POST['formWhere'])) { |
||
458 | $_POST['formWhere'] = ''; |
||
459 | } |
||
460 | |||
461 | // Default tablespace to null if it isn't set |
||
462 | if (!isset($_POST['formSpc'])) { |
||
463 | $_POST['formSpc'] = null; |
||
464 | } |
||
465 | |||
466 | // Check that they've given a name and at least one column |
||
467 | if ($_POST['formIndexName'] == '') { |
||
468 | $this->doCreateIndex($lang['strindexneedsname']); |
||
469 | } elseif (!isset($_POST['IndexColumnList']) || $_POST['IndexColumnList'] == '') { |
||
470 | $this->doCreateIndex($lang['strindexneedscols']); |
||
471 | } else { |
||
472 | $status = $data->createIndex($_POST['formIndexName'], $object, $_POST['IndexColumnList'], |
||
473 | $_POST['formIndexType'], isset($_POST['formUnique']), $_POST['formWhere'], $_POST['formSpc'], |
||
474 | isset($_POST['formConcur'])); |
||
475 | if ($status == 0) { |
||
476 | $this->doDefault($lang['strindexcreated']); |
||
477 | } else { |
||
478 | $this->doCreateIndex($lang['strindexcreatedbad']); |
||
479 | } |
||
480 | } |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Show confirmation of drop index and perform actual drop |
||
485 | */ |
||
486 | public function doDropIndex($confirm) |
||
520 | } |
||
521 | } |
||
524 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: