Total Complexity | 62 |
Total Lines | 474 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CheckoutWizard 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 CheckoutWizard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class CheckoutWizard extends ZervWizard |
||
16 | { |
||
17 | /** |
||
18 | * CheckoutWizard constructor. |
||
19 | */ |
||
20 | public function __construct() |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @todo change access to fields using Pedigree\Fields |
||
51 | */ |
||
52 | public function prepareFieldname() |
||
53 | { |
||
54 | global $field; |
||
55 | if (0 == !$field) { |
||
56 | // field already exists (editing mode) |
||
57 | |||
58 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $field; |
||
59 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
60 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
61 | $name = $row['fieldname']; |
||
62 | $fieldexplanation = $row['fieldexplanation']; |
||
63 | $fieldtype = $row['fieldtype']; |
||
64 | } |
||
65 | $this->setValue('name', $name); |
||
66 | $this->setValue('explain', $fieldexplanation); |
||
67 | //set the fieldtype because we wont allow it to be edited |
||
68 | $this->setValue('fieldtype', $fieldtype); |
||
69 | } |
||
70 | $this->setValue('field', $field); //is it a new field or are we editing a field |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param $form |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function processFieldname($form) |
||
79 | { |
||
80 | $name = $this->coalesce($form['name']); |
||
81 | if (\mb_strlen($name) > 0) { |
||
82 | $this->setValue('name', $name); |
||
83 | } else { |
||
84 | $this->addError('name', \_MA_PEDIGREE_FIELD_NAM); |
||
85 | } |
||
86 | |||
87 | $fieldexplanation = $this->coalesce($form['explain']); |
||
88 | if (\mb_strlen($fieldexplanation) > 0) { |
||
89 | $this->setValue('explain', $fieldexplanation); |
||
90 | } else { |
||
91 | $this->addError('explain', _MA_PEDIGREE_FIELD_EXPLAN1); |
||
92 | } |
||
93 | |||
94 | return !$this->isError(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Setup this class' fieldtype array |
||
99 | */ |
||
100 | public function prepareFieldtype() |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param $form |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function processFieldtype($form) |
||
116 | { |
||
117 | $this->prepareFieldtype(); |
||
118 | $fieldtype = $this->coalesce($form['fieldtype']); |
||
119 | $this->setValue('fieldtype', $fieldtype); |
||
120 | |||
121 | return !$this->isError(); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param $form |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function processLookup($form) |
||
130 | { |
||
131 | $fc = $this->coalesce($form['fc']); |
||
132 | $this->setValue('fc', $fc); |
||
133 | $lookup = $this->coalesce($form['lookup' . $fc]); |
||
134 | $lookupid = $this->coalesce($form['id' . $fc]); |
||
135 | if (\mb_strlen($lookup) > 0) { |
||
136 | $this->setValue('lookup' . $fc, $lookup); |
||
137 | $this->setValue('id' . $fc, $lookupid); |
||
138 | } |
||
139 | $lastlookup = $this->getValue('lookup' . $fc); |
||
140 | if ('' == $lastlookup) { |
||
141 | $this->setValue('fc', $fc - 1); |
||
142 | } |
||
143 | |||
144 | for ($i = 0; $i < $fc; ++$i) { |
||
145 | $radioarray[] = [ |
||
146 | 'id' => $this->getValue('id' . ($i + 1)), |
||
147 | 'value' => $this->getValue('lookup' . ($i + 1)), |
||
148 | ]; |
||
149 | } |
||
150 | //print_r($radioarray); exit(); |
||
151 | $this->setValue('radioarray', $radioarray); |
||
152 | |||
153 | return !$this->isError(); |
||
154 | } |
||
155 | |||
156 | public function prepareSettings() |
||
157 | { |
||
158 | if (0 == !$this->getValue('field')) { |
||
159 | // field already exists (editing mode) |
||
160 | |||
161 | { |
||
162 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " WHERE id='" . $this->getValue('field') . "'"; |
||
163 | } |
||
164 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
165 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
166 | $hs = $row['hassearch']; |
||
167 | if ('1' == $hs) { |
||
168 | $this->setValue('hassearch', 'hassearch'); |
||
169 | } |
||
170 | $vip = $row['viewinpedigree']; |
||
171 | if ('1' == $vip) { |
||
172 | $this->setValue('viewinpedigree', 'viewinpedigree'); |
||
173 | } |
||
174 | $via = $row['viewinadvanced']; |
||
175 | if ('1' == $via) { |
||
176 | $this->setValue('viewinadvanced', 'viewinadvanced'); |
||
177 | } |
||
178 | $vipie = $row['viewinpie']; |
||
179 | if ('1' == $vipie) { |
||
180 | $this->setValue('viewinpie', 'viewinpie'); |
||
181 | } |
||
182 | $vil = $row['viewinlist']; |
||
183 | if ('1' == $vil) { |
||
184 | $this->setValue('viewinlist', 'viewinlist'); |
||
185 | } |
||
186 | $lit = $row['litter']; |
||
187 | if ('1' == $lit) { |
||
188 | $this->setValue('litter', 'litter'); |
||
189 | } |
||
190 | $glit = $row['generallitter']; |
||
191 | if ('1' == $glit) { |
||
192 | $this->setValue('generallitter', 'generallitter'); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param $form |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | public function processSettings($form) |
||
204 | { |
||
205 | $this->setValue('hassearch', $this->coalesce($form['hasSearch'])); |
||
206 | $this->setValue('viewinpedigree', $this->coalesce($form['viewinpedigree'])); |
||
207 | $this->setValue('viewinadvanced', $this->coalesce($form['viewinadvanced'])); |
||
208 | $this->setValue('viewinpie', $this->coalesce($form['viewinpie'])); |
||
209 | $this->setValue('viewinlist', $this->coalesce($form['viewinlist'])); |
||
210 | $this->setValue('litter', $this->coalesce($form['litter'])); |
||
211 | $this->setValue('generallitter', $this->coalesce($form['generallitter'])); |
||
212 | |||
213 | //if both litter and general litter are set; unset generallitter |
||
214 | if (('litter' === $this->getValue('litter')) && ('generallitter' === $this->getValue('generallitter'))) { |
||
215 | $this->setValue('generallitter', 0); |
||
216 | } |
||
217 | |||
218 | return !$this->isError(); |
||
219 | } |
||
220 | |||
221 | public function prepareSearch() |
||
222 | { |
||
223 | if (0 == !$this->getValue('field')) { |
||
224 | // field already exists (editing mode) |
||
225 | |||
226 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $this->getValue('field'); |
||
227 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
228 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
229 | if ('hasearch' === $this->getValue('hassearch')) { |
||
230 | $searchname = $row['searchname']; |
||
231 | $this->setValue('searchname', $searchname); |
||
232 | $searchexplain = $row['searchexplanation']; |
||
233 | $this->setValue('searchexplain', $searchexplain); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param $form |
||
241 | * |
||
242 | * @return bool |
||
243 | * @todo move language strings to language files |
||
244 | */ |
||
245 | public function processSearch($form) |
||
246 | { |
||
247 | $searchname = $this->coalesce($form['searchname']); |
||
248 | if (\mb_strlen($searchname) > 0) { |
||
249 | $this->setValue('searchname', $searchname); |
||
250 | } else { |
||
251 | $this->addError('searchname', 'Please enter the searchname'); |
||
252 | } |
||
253 | |||
254 | $fieldexplanation = $this->coalesce($form['searchexplain']); |
||
255 | if (\mb_strlen($fieldexplanation) > 0) { |
||
256 | $this->setValue('searchexplain', $fieldexplanation); |
||
257 | } else { |
||
258 | $this->addError('searchexplain', 'Please enter the search explanation for this field'); |
||
259 | } |
||
260 | |||
261 | return !$this->isError(); |
||
262 | } |
||
263 | |||
264 | public function prepareDefaultvalue() |
||
265 | { |
||
266 | if (0 == !$this->getValue('field')) { |
||
267 | // field already exists (editing mode) |
||
268 | |||
269 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $this->getValue('field'); |
||
270 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
271 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
272 | $def = $row['DefaultValue']; |
||
273 | $this->setValue('defaultvalue', $def); |
||
274 | if ('1' == $row['LookupTable']) { //we have a lookup table; load values |
||
275 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $this->getValue('field')) . " ORDER BY 'order'"; |
||
276 | $fc = 0; |
||
277 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
278 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
279 | $radioarray[] = ['id' => $row['id'], 'value' => $row['value']]; |
||
280 | ++$fc; |
||
281 | } |
||
282 | $this->setValue('radioarray', $radioarray); |
||
283 | $this->setValue('fc', $fc); |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param $form |
||
291 | * |
||
292 | * @return bool |
||
293 | * @todo move language string to language file |
||
294 | */ |
||
295 | public function processDefaultValue($form) |
||
296 | { |
||
297 | $defaultvalue = $this->coalesce($form['defaultvalue']); |
||
298 | if (\mb_strlen($defaultvalue) >= 0) { |
||
299 | $this->setValue('defaultvalue', $defaultvalue); |
||
300 | } else { |
||
301 | $this->addError('defaultvalue', 'Please enter a defaultvalue'); |
||
302 | } |
||
303 | |||
304 | return !$this->isError(); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param $form |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function processConfirm($form) |
||
313 | { |
||
314 | return !$this->isError(); |
||
315 | } |
||
316 | |||
317 | public function completeCallback() |
||
476 | } |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Miscellaneous utility functions |
||
481 | * |
||
482 | * @param $email |
||
483 | * |
||
484 | * @return int |
||
485 | */ |
||
486 | public function isValidEmail($email) |
||
489 | } |
||
490 | } |
||
491 |