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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
7 | class CheckoutWizard extends ZervWizard |
||
8 | { |
||
9 | /** |
||
10 | * CheckoutWizard constructor. |
||
11 | */ |
||
12 | public function __construct() |
||
13 | { |
||
14 | global $field; |
||
15 | // start the session and initialize the wizard |
||
16 | if (!isset($_SESSION)) { |
||
17 | session_start(); |
||
18 | } |
||
19 | parent::__construct($_SESSION, __CLASS__); |
||
20 | |||
21 | $this->addStep('fieldname', _MA_PEDIGREE_ENTER_FIELD); |
||
22 | if (0 == $this->getValue('field')) { //only for a new field |
||
|
|||
23 | $this->addStep('fieldtype', _MA_PEDIGREE_FIELD_TYP_SEL); |
||
24 | if (('selectbox' === $this->getValue('fieldtype')) || ('radiobutton' === $this->getValue('fieldtype'))) { |
||
25 | $this->addStep('lookup', _MA_PEDIGREE_FIELD_ADD_VALUE); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | $this->addStep('Settings', _MA_PEDIGREE_FIELD_PARAM); |
||
30 | if ('hassearch' === $this->getValue('hassearch')) { |
||
31 | $this->addStep('search', _MA_PEDIGREE_SEARCH_PARAMFIELD); |
||
32 | } |
||
33 | if ('picture' !== $this->getValue('fieldtype')) { |
||
34 | $this->addStep('defaultvalue', _MA_PEDIGREE_FIELD_DEFAUT); |
||
35 | } |
||
36 | $this->addStep('confirm', _MA_PEDIGREE_FIELDCONFIRM); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @todo change access to fields using Pedigree\Fields |
||
41 | * @return void |
||
42 | */ |
||
43 | public function prepareFieldname() |
||
44 | { |
||
45 | global $field; |
||
46 | if (0 == !$field) { |
||
47 | // field already exists (editing mode) |
||
48 | |||
49 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $field; |
||
50 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
51 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
52 | $name = $row['fieldname']; |
||
53 | $fieldexplanation = $row['fieldexplanation']; |
||
54 | $fieldtype = $row['fieldtype']; |
||
55 | } |
||
56 | $this->setValue('name', $name); |
||
57 | $this->setValue('explain', $fieldexplanation); |
||
58 | //set the fieldtype because we wont allow it to be edited |
||
59 | $this->setValue('fieldtype', $fieldtype); |
||
60 | } |
||
61 | $this->setValue('field', $field); //is it a new field or are we editing a field |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param $form |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function processFieldname(&$form) |
||
70 | { |
||
71 | $name = $this->coalesce($form['name']); |
||
72 | if (strlen($name) > 0) { |
||
73 | $this->setValue('name', $name); |
||
74 | } else { |
||
75 | $this->addError('name', _MA_PEDIGREE_FIELD_NAM); |
||
76 | } |
||
77 | |||
78 | $fieldexplanation = $this->coalesce($form['explain']); |
||
79 | if (strlen($fieldexplanation) > 0) { |
||
80 | $this->setValue('explain', $fieldexplanation); |
||
81 | } else { |
||
82 | $this->addError('explain', _MA_PEDIGREE_FIELD_EXPLAN1); |
||
83 | } |
||
84 | |||
85 | return !$this->isError(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Setup this class' fieldtype array |
||
90 | * @return void |
||
91 | */ |
||
92 | public function prepareFieldtype() |
||
93 | { |
||
94 | $this->fieldtype[] = ['value' => 'radiobutton', 'description' => _MA_PEDIGREE_RADIOBUTTONFIELD]; |
||
95 | $this->fieldtype[] = ['value' => 'selectbox', 'description' => _MA_PEDIGREE_DROPDOWNFIELD]; |
||
96 | $this->fieldtype[] = ['value' => 'textbox', 'description' => _MA_PEDIGREE_TEXTBOXFIELD]; |
||
97 | $this->fieldtype[] = ['value' => 'textarea', 'description' => _MA_PEDIGREE_TEXTAREAFIELD]; |
||
98 | $this->fieldtype[] = ['value' => 'DateSelect', 'description' => _MA_PEDIGREE_DATEFIELD]; |
||
99 | $this->fieldtype[] = ['value' => 'urlfield', 'description' => _MA_PEDIGREE_URLFIELD]; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param $form |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function processFieldtype($form) |
||
108 | { |
||
109 | $this->prepareFieldtype(); |
||
110 | $fieldtype = $this->coalesce($form['fieldtype']); |
||
111 | $this->setValue('fieldtype', $fieldtype); |
||
112 | |||
113 | return !$this->isError(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $form |
||
118 | * |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function processLookup($form) |
||
122 | { |
||
123 | $fc = $this->coalesce($form['fc']); |
||
124 | $this->setValue('fc', $fc); |
||
125 | $lookup = $this->coalesce($form['lookup' . $fc]); |
||
126 | $lookupid = $this->coalesce($form['id' . $fc]); |
||
127 | if (strlen($lookup) > 0) { |
||
128 | $this->setValue('lookup' . $fc, $lookup); |
||
129 | $this->setValue('id' . $fc, $lookupid); |
||
130 | } |
||
131 | $lastlookup = $this->getValue('lookup' . $fc); |
||
132 | if ('' == $lastlookup) { |
||
133 | $this->setValue('fc', $fc - 1); |
||
134 | } |
||
135 | |||
136 | for ($i = 0; $i < $fc; ++$i) { |
||
137 | $radioarray[] = [ |
||
138 | 'id' => $this->getValue('id' . ($i + 1)), |
||
139 | 'value' => $this->getValue('lookup' . ($i + 1)) |
||
140 | ]; |
||
141 | } |
||
142 | //print_r($radioarray); die(); |
||
143 | $this->setValue('radioarray', $radioarray); |
||
144 | |||
145 | return !$this->isError(); |
||
146 | // |
||
147 | } |
||
148 | |||
149 | public function prepareSettings() |
||
150 | { |
||
151 | if (0 == !$this->getValue('field')) { |
||
152 | // field allready exists (editing mode) |
||
153 | |||
154 | { |
||
155 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . " WHERE id='" . $this->getValue('field') . "'"; |
||
156 | } |
||
157 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
158 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
159 | $hs = $row['hassearch']; |
||
160 | if ('1' == $hs) { |
||
161 | $this->setValue('hassearch', 'hassearch'); |
||
162 | } |
||
163 | $vip = $row['viewinpedigree']; |
||
164 | if ('1' == $vip) { |
||
165 | $this->setValue('viewinpedigree', 'viewinpedigree'); |
||
166 | } |
||
167 | $via = $row['viewinadvanced']; |
||
168 | if ('1' == $via) { |
||
169 | $this->setValue('viewinadvanced', 'viewinadvanced'); |
||
170 | } |
||
171 | $vipie = $row['viewinpie']; |
||
172 | if ('1' == $vipie) { |
||
173 | $this->setValue('viewinpie', 'viewinpie'); |
||
174 | } |
||
175 | $vil = $row['viewinlist']; |
||
176 | if ('1' == $vil) { |
||
177 | $this->setValue('viewinlist', 'viewinlist'); |
||
178 | } |
||
179 | $lit = $row['litter']; |
||
180 | if ('1' == $lit) { |
||
181 | $this->setValue('litter', 'litter'); |
||
182 | } |
||
183 | $glit = $row['generallitter']; |
||
184 | if ('1' == $glit) { |
||
185 | $this->setValue('generallitter', 'generallitter'); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param $form |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function processSettings($form) |
||
197 | { |
||
198 | $this->setValue('hassearch', $this->coalesce($form['hasSearch'])); |
||
199 | $this->setValue('viewinpedigree', $this->coalesce($form['viewinpedigree'])); |
||
200 | $this->setValue('viewinadvanced', $this->coalesce($form['viewinadvanced'])); |
||
201 | $this->setValue('viewinpie', $this->coalesce($form['viewinpie'])); |
||
202 | $this->setValue('viewinlist', $this->coalesce($form['viewinlist'])); |
||
203 | $this->setValue('litter', $this->coalesce($form['litter'])); |
||
204 | $this->setValue('generallitter', $this->coalesce($form['generallitter'])); |
||
205 | |||
206 | //if both litter and general litter are set; unset generallitter |
||
207 | if (('litter' === $this->getValue('litter')) && ('generallitter' === $this->getValue('generallitter'))) { |
||
208 | $this->setValue('generallitter', 0); |
||
209 | } |
||
210 | |||
211 | return !$this->isError(); |
||
212 | } |
||
213 | |||
214 | public function prepareSearch() |
||
215 | { |
||
216 | if (0 == !$this->getValue('field')) { |
||
217 | // field allready exists (editing mode) |
||
218 | |||
219 | $sql = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' WHERE id=' . $this->getValue('field'); |
||
220 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
221 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
222 | if ('hasearch' === $this->getValue('hassearch')) { |
||
223 | $searchname = $row['searchname']; |
||
224 | $this->setValue('searchname', $searchname); |
||
225 | $searchexplain = $row['searchexplanation']; |
||
226 | $this->setValue('searchexplain', $searchexplain); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param $form |
||
234 | * |
||
235 | * @return bool |
||
236 | * @todo move language strings to language files |
||
237 | */ |
||
238 | public function processSearch($form) |
||
239 | { |
||
240 | $searchname = $this->coalesce($form['searchname']); |
||
241 | if (strlen($searchname) > 0) { |
||
242 | $this->setValue('searchname', $searchname); |
||
243 | } else { |
||
244 | $this->addError('searchname', 'Please enter the searchname'); |
||
245 | } |
||
246 | |||
247 | $fieldexplanation = $this->coalesce($form['searchexplain']); |
||
248 | if (strlen($fieldexplanation) > 0) { |
||
249 | $this->setValue('searchexplain', $fieldexplanation); |
||
250 | } else { |
||
251 | $this->addError('searchexplain', 'Please enter the search explanation for this field'); |
||
252 | } |
||
253 | |||
254 | return !$this->isError(); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return void |
||
259 | */ |
||
260 | public function prepareDefaultvalue() |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param $form |
||
287 | * |
||
288 | * @return bool |
||
289 | * @todo move language string to language file |
||
290 | */ |
||
291 | public function processDefaultValue($form) |
||
292 | { |
||
293 | $defaultvalue = $this->coalesce($form['defaultvalue']); |
||
294 | if (strlen($defaultvalue) >= 0) { |
||
295 | $this->setValue('defaultvalue', $defaultvalue); |
||
296 | } else { |
||
297 | $this->addError('defaultvalue', 'Please enter a defaultvalue'); |
||
298 | } |
||
299 | |||
300 | return !$this->isError(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param $form |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | public function processConfirm($form) |
||
311 | } |
||
312 | |||
313 | public function completeCallback() |
||
314 | { |
||
315 | |||
316 | //can this field be searched |
||
317 | $search = $this->getValue('hassearch'); |
||
318 | if ('hassearch' === $search) { |
||
319 | $search = '1'; |
||
320 | $searchname = $this->getValue('searchname'); |
||
321 | $searchexplain = $this->getValue('searchexplain'); |
||
322 | } else { |
||
323 | $search = '0'; |
||
324 | $searchname = ''; |
||
325 | $searchexplain = ''; |
||
326 | } |
||
327 | //show in pedigree |
||
328 | $viewinpedigree = $this->getValue('viewinpedigree'); |
||
329 | if ('viewinpedigree' === $viewinpedigree) { |
||
330 | $viewinpedigree = '1'; |
||
331 | } else { |
||
332 | $viewinpedigree = '0'; |
||
333 | } |
||
334 | //show in advanced |
||
335 | $viewinadvanced = $this->getValue('viewinadvanced'); |
||
336 | if ('viewinadvanced' === $viewinadvanced) { |
||
337 | $viewinadvanced = '1'; |
||
338 | } else { |
||
339 | $viewinadvanced = '0'; |
||
340 | } |
||
341 | //show in pie |
||
342 | $viewinpie = $this->getValue('viewinpie'); |
||
343 | if ('viewinpie' === $viewinpie) { |
||
344 | $viewinpie = '1'; |
||
345 | } else { |
||
346 | $viewinpie = '0'; |
||
347 | } |
||
348 | //view in list |
||
349 | $viewinlist = $this->getValue('viewinlist'); |
||
350 | if ('viewinlist' === $viewinlist) { |
||
351 | $viewinlist = '1'; |
||
352 | } else { |
||
353 | $viewinlist = '0'; |
||
354 | } |
||
355 | //add a litter? |
||
356 | $litter = ('litter' === $this->getValue('litter')) ? '1' : '0'; |
||
357 | |||
358 | //general litter |
||
359 | $generallitter = ('generallitter' === $this->getValue('generalLitter')) ? '1' : '0'; |
||
360 | |||
361 | if (0 == !$this->getValue('field')) { |
||
362 | // field allready exists (editing mode) |
||
363 | |||
364 | //@todo refactor using class methods |
||
365 | $sql = 'UPDATE ' |
||
366 | . $GLOBALS['xoopsDB']->prefix('pedigree_fields') |
||
367 | . " SET fieldname = '" |
||
368 | . htmlspecialchars($this->getValue('name'), ENT_QUOTES | ENT_HTML5) |
||
369 | . "', fieldtype = '" |
||
370 | . $this->getValue('fieldtype') |
||
371 | . "', defaultvalue = '" |
||
372 | . $this->getValue('defaultvalue') |
||
373 | . "', fieldexplanation = '" |
||
374 | . $this->getValue('explain') |
||
375 | . "', hassearch = '" |
||
376 | . $search |
||
377 | . "', litter = '" |
||
378 | . $litter |
||
379 | . "', generallitter = '" |
||
380 | . $generallitter |
||
381 | . "', searchname = '" |
||
382 | . $searchname |
||
383 | . "', searchexplanation = '" |
||
384 | . $searchexplain |
||
385 | . "', viewinpedigree = '" |
||
386 | . $viewinpedigree |
||
387 | . "', viewinadvanced = '" |
||
388 | . $viewinadvanced |
||
389 | . "', viewinpie = '" |
||
390 | . $viewinpie |
||
391 | . "', viewinlist = '" |
||
392 | . $viewinlist |
||
393 | . "' WHERE id ='" |
||
394 | . $this->getValue('field') |
||
395 | . "'"; |
||
396 | $GLOBALS['xoopsDB']->queryF($sql); |
||
397 | //possible change defaultvalue for userfield |
||
398 | $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_tree') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
399 | $GLOBALS['xoopsDB']->queryF($sql); |
||
400 | $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_temp') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 1024 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
401 | $GLOBALS['xoopsDB']->queryF($sql); |
||
402 | $sql = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_trash') . ' CHANGE `user' . $this->getValue('field') . '` `user' . $this->getValue('field') . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
403 | $GLOBALS['xoopsDB']->queryF($sql); |
||
404 | } else { //this is a new field |
||
405 | $sql = 'SELECT MAX(id) AS lid FROM ' . $GLOBALS['xoopsDB']->prefix('pedigree_fields') . ' LIMIT 1'; |
||
406 | $result = $GLOBALS['xoopsDB']->query($sql); |
||
407 | while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) { |
||
408 | $nextfieldnum = $row['lid'] + 1; |
||
409 | } |
||
410 | //add userfield to various tables as a new field. |
||
411 | //always add at the end of the table |
||
412 | $tables = ['pedigree_tree', 'pedigree_temp', 'pedigree_trash']; |
||
413 | foreach ($tables as $table) { |
||
414 | $SQL = 'ALTER TABLE ' . $GLOBALS['xoopsDB']->prefix($table) . ' ADD `user' . $nextfieldnum . "` VARCHAR( 255 ) NOT NULL DEFAULT '" . $this->getValue('defaultvalue') . "'"; |
||
415 | $GLOBALS['xoopsDB']->queryF($SQL); |
||
416 | } |
||
417 | //is a lookup table present |
||
418 | $lookup = $this->getValue('lookup1'); |
||
419 | if ('' == $lookup) { |
||
420 | $lookup = '0'; |
||
421 | } else { |
||
422 | $lookup = '1'; |
||
423 | //create table for lookupfield |
||
424 | $createtable = 'CREATE TABLE ' . $GLOBALS['xoopsDB']->prefix('pedigree_lookup' . $nextfieldnum) . ' (`id` INT( 10 ) NOT NULL ,`value` VARCHAR( 255 ) NOT NULL, `order` INT( 10 )) ENGINE = MyISAM'; |
||
489 |