Total Complexity | 66 |
Total Lines | 489 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ViewsMatviewsTrait 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 ViewsMatviewsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait ViewsMatviewsTrait |
||
15 | { |
||
16 | public $href = ''; |
||
17 | public $misc; |
||
18 | public $view_name; |
||
19 | |||
20 | public function doSubTree() |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Ask for select parameters and perform select. |
||
50 | * |
||
51 | * @param mixed $confirm |
||
52 | * @param mixed $msg |
||
53 | */ |
||
54 | public function doSelectRows($confirm, $msg = '') |
||
55 | { |
||
56 | $data = $this->misc->getDatabaseAccessor(); |
||
57 | |||
58 | if ($confirm) { |
||
59 | $this->printTrail($this->keystring); |
||
60 | $this->printTabs($this->keystring, 'select'); |
||
61 | $this->printMsg($msg); |
||
62 | |||
63 | $attrs = $data->getTableAttributes($_REQUEST[$this->keystring]); |
||
64 | |||
65 | echo '<form action="'.\SUBFOLDER.'/src/views/'.$this->script.'" method="post" id="selectform">'; |
||
66 | echo PHP_EOL; |
||
67 | |||
68 | if ($attrs->recordCount() > 0) { |
||
69 | // JavaScript for select all feature |
||
70 | echo '<script type="text/javascript">'.PHP_EOL; |
||
71 | echo "//<![CDATA[\n"; |
||
72 | echo " function selectAll() {\n"; |
||
73 | echo " for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n"; |
||
74 | echo " var e = document.getElementById('selectform').elements[i];\n"; |
||
75 | echo " if (e.name.indexOf('show') == 0) { \n "; |
||
76 | echo " e.checked = document.getElementById('selectform').selectall.checked;\n"; |
||
77 | echo " }\n"; |
||
78 | echo " }\n"; |
||
79 | echo " }\n"; |
||
80 | echo '//]]>'.PHP_EOL; |
||
81 | echo '</script>'.PHP_EOL; |
||
82 | |||
83 | echo '<table>'.PHP_EOL; |
||
84 | |||
85 | // Output table header |
||
86 | echo "<tr><th class=\"data\">{$this->lang['strshow']}</th><th class=\"data\">{$this->lang['strcolumn']}</th>"; |
||
87 | echo "<th class=\"data\">{$this->lang['strtype']}</th><th class=\"data\">{$this->lang['stroperator']}</th>"; |
||
88 | echo "<th class=\"data\">{$this->lang['strvalue']}</th></tr>"; |
||
89 | |||
90 | $i = 0; |
||
91 | while (!$attrs->EOF) { |
||
92 | $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
||
93 | // Set up default value if there isn't one already |
||
94 | if (!isset($_REQUEST['values'][$attrs->fields['attname']])) { |
||
95 | $_REQUEST['values'][$attrs->fields['attname']] = null; |
||
96 | } |
||
97 | |||
98 | if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) { |
||
99 | $_REQUEST['ops'][$attrs->fields['attname']] = null; |
||
100 | } |
||
101 | |||
102 | // Continue drawing row |
||
103 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
104 | echo "<tr class=\"data{$id}\">".PHP_EOL; |
||
105 | echo '<td style="white-space:nowrap;">'; |
||
106 | echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"', |
||
107 | isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>'; |
||
108 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>'; |
||
109 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>'; |
||
110 | echo '<td style="white-space:nowrap;">'; |
||
111 | echo "<select name=\"ops[{$attrs->fields['attname']}]\">".PHP_EOL; |
||
112 | foreach (array_keys($data->selectOps) as $v) { |
||
113 | echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '', |
||
114 | '>', htmlspecialchars($v), '</option>'.PHP_EOL; |
||
115 | } |
||
116 | echo '</select></td>'.PHP_EOL; |
||
117 | echo '<td style="white-space:nowrap;">', $data->printField( |
||
118 | "values[{$attrs->fields['attname']}]", |
||
119 | $_REQUEST['values'][$attrs->fields['attname']], |
||
120 | $attrs->fields['type'] |
||
121 | ), '</td>'; |
||
122 | echo '</tr>'.PHP_EOL; |
||
123 | ++$i; |
||
124 | $attrs->moveNext(); |
||
125 | } |
||
126 | // Select all checkbox |
||
127 | echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$this->lang['strselectallfields']}</label></td></tr>"; |
||
128 | echo '</table>'.PHP_EOL; |
||
129 | } else { |
||
130 | echo "<p>{$this->lang['strinvalidparam']}</p>".PHP_EOL; |
||
131 | } |
||
132 | |||
133 | echo '<p><input type="hidden" name="action" value="selectrows" />'.PHP_EOL; |
||
134 | echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->keystring]), '" />'.PHP_EOL; |
||
135 | echo '<input type="hidden" name="subject" value="view" />'.PHP_EOL; |
||
136 | echo $this->misc->form; |
||
137 | echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$this->lang['strselect']}\" />".PHP_EOL; |
||
138 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
139 | echo '</form>'.PHP_EOL; |
||
140 | |||
141 | return; |
||
142 | } |
||
143 | $this->coalesceArr($_POST, 'show', []); |
||
144 | |||
145 | $this->coalesceArr($_POST, 'values', []); |
||
146 | |||
147 | $this->coalesceArr($_POST, 'nulls', []); |
||
148 | |||
149 | // Verify that they haven't supplied a value for unary operators |
||
150 | foreach ($_POST['ops'] as $k => $v) { |
||
151 | if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') { |
||
152 | $this->doSelectRows(true, $this->lang['strselectunary']); |
||
153 | |||
154 | return; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | if (0 == sizeof($_POST['show'])) { |
||
159 | return $this->doSelectRows(true, $this->lang['strselectneedscol']); |
||
160 | } |
||
161 | // Generate query SQL |
||
162 | $query = $data->getSelectSQL($_REQUEST[$this->keystring], array_keys($_POST['show']), $_POST['values'], $_POST['ops']); |
||
163 | |||
164 | $_REQUEST['query'] = $query; |
||
165 | $_REQUEST['return'] = 'schema'; |
||
166 | |||
167 | $this->setNoOutput(true); |
||
168 | |||
169 | $display_controller = new \PHPPgAdmin\Controller\DisplayController($this->getContainer()); |
||
170 | |||
171 | return $display_controller->render(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Prints the form wizard to create view or materialized view. |
||
176 | */ |
||
177 | public function printWizardCreateForm() |
||
178 | { |
||
179 | $data = $this->misc->getDatabaseAccessor(); |
||
180 | |||
181 | $tables = $data->getAllTables(); |
||
182 | |||
183 | echo '<form action="'.\SUBFOLDER."/src/views/{$this->script}\" method=\"post\">".PHP_EOL; |
||
184 | echo '<table>'.PHP_EOL; |
||
185 | echo "<tr><th class=\"data\">{$this->lang['strtables']}</th></tr>"; |
||
186 | echo "<tr>\n<td class=\"data1\">".PHP_EOL; |
||
187 | |||
188 | $arrTables = []; |
||
189 | while (!$tables->EOF) { |
||
190 | $arrTmp = []; |
||
191 | $arrTmp['schemaname'] = $tables->fields['nspname']; |
||
192 | $arrTmp['tablename'] = $tables->fields['relname']; |
||
193 | $schema_and_name = $tables->fields['nspname'].'.'.$tables->fields['relname']; |
||
194 | $arrTables[$schema_and_name] = serialize($arrTmp); |
||
195 | $tables->moveNext(); |
||
196 | } |
||
197 | echo \PHPPgAdmin\XHtml\HTMLController::printCombo($arrTables, 'formTables[]', false, '', true); |
||
198 | |||
199 | echo "</td>\n</tr>".PHP_EOL; |
||
200 | echo '</table>'.PHP_EOL; |
||
201 | echo '<p><input type="hidden" name="action" value="set_params_create" />'.PHP_EOL; |
||
202 | echo $this->misc->form; |
||
203 | echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />".PHP_EOL; |
||
204 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>".PHP_EOL; |
||
205 | echo '</form>'.PHP_EOL; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Appends to selected fields. |
||
210 | * |
||
211 | * @param array $arrTmp The arr temporary |
||
212 | * @param string $selFields The selected fields |
||
213 | * @param array $tmpHsh The temporary hsh |
||
214 | */ |
||
215 | private function _appendToSelFields($arrTmp, &$selFields, &$tmpHsh) |
||
216 | { |
||
217 | $field_arr = [$arrTmp['schemaname'], $arrTmp['tablename'], $arrTmp['fieldname']]; |
||
218 | |||
219 | $field_element = '"'.implode('"."', $field_arr).'"'; |
||
220 | if (empty($_POST['dblFldMeth'])) { |
||
221 | // no doublon control |
||
222 | $selFields .= $field_element.', '; |
||
223 | } elseif (empty($tmpHsh[$arrTmp['fieldname']])) { |
||
224 | // field does not exist |
||
225 | $selFields .= $field_element.', '; |
||
226 | $tmpHsh[$arrTmp['fieldname']] = 1; |
||
227 | } elseif ('rename' == $_POST['dblFldMeth']) { |
||
228 | // field exist and must be renamed |
||
229 | ++$tmpHsh[$arrTmp['fieldname']]; |
||
230 | $selFields .= $field_element.' AS "'.implode('_', $field_arr).'_'.$tmpHsh[$arrTmp['fieldname']].'", '; |
||
231 | } |
||
232 | // if field already exist, just ignore this one |
||
233 | } |
||
234 | |||
235 | private function _getArrLinks() |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Actually creates the new wizard view in the database. |
||
257 | * |
||
258 | * @param bool $is_materialized true if it's a materialized view, false by default |
||
259 | * |
||
260 | * @return mixed either a sucess message, a redirection, an error message and who knows |
||
261 | */ |
||
262 | public function doSaveCreateWiz($is_materialized = false) |
||
371 | } |
||
372 | } |
||
373 | |||
374 | public function printParamsCreateForm() |
||
500 | } |
||
501 | |||
502 | abstract public function doSetParamsCreate($msg = ''); |
||
503 | } |
||
504 |