Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Helper |
||
29 | { |
||
30 | public static function showOrderStatusText(\HaaseIT\Toolbox\Textcat $textcats, $sStatusShort) |
||
31 | { |
||
32 | $mapping = [ |
||
33 | 'y' => 'order_status_completed', |
||
34 | 'n' => 'order_status_open', |
||
35 | 'i' => 'order_status_inwork', |
||
36 | 's' => 'order_status_canceled', |
||
37 | 'd' => 'order_status_deleted', |
||
38 | ]; |
||
39 | |||
40 | if (!empty($mapping[$sStatusShort])) { |
||
41 | return $textcats->T($mapping[$sStatusShort]); |
||
42 | } |
||
43 | |||
44 | return ''; |
||
45 | } |
||
46 | |||
47 | public static function calculateTotalFromDB($aOrder) |
||
48 | { |
||
49 | $fGesamtnetto = $aOrder['o_sumnettoall']; |
||
50 | $fVoll = $aOrder['o_sumvoll']; |
||
51 | $fSteuererm = $aOrder['o_taxerm']; |
||
52 | |||
53 | if ($aOrder['o_mindermenge'] > 0) { |
||
54 | $fVoll += $aOrder['o_mindermenge']; |
||
55 | $fGesamtnetto += $aOrder['o_mindermenge']; |
||
56 | } |
||
57 | if ($aOrder['o_shippingcost'] > 0) { |
||
58 | $fVoll += $aOrder['o_shippingcost']; |
||
59 | $fGesamtnetto += $aOrder['o_shippingcost']; |
||
60 | } |
||
61 | |||
62 | $fSteuervoll = ($fVoll * $aOrder['o_vatfull'] / 100); |
||
63 | |||
64 | return $fGesamtnetto + $fSteuervoll + $fSteuererm; |
||
65 | } |
||
66 | |||
67 | public static function addAdditionalCostsToItems($aSumme, $iVATfull, $iVATreduced) |
||
68 | { |
||
69 | $fGesamtnetto = $aSumme['sumvoll'] + $aSumme['sumerm']; |
||
70 | $fSteuervoll = $aSumme['sumvoll'] * $iVATfull / 100; |
||
71 | $fSteuererm = $aSumme['sumerm'] * $iVATreduced / 100; |
||
72 | $fGesamtbrutto = $fGesamtnetto + $fSteuervoll + $fSteuererm; |
||
73 | |||
74 | $aOrder = [ |
||
75 | 'sumvoll' => $aSumme['sumvoll'], |
||
76 | 'sumerm' => $aSumme['sumerm'], |
||
77 | 'sumnettoall' => $fGesamtnetto, |
||
78 | 'taxvoll' => $fSteuervoll, |
||
79 | 'taxerm' => $fSteuererm, |
||
80 | 'sumbruttoall' => $fGesamtbrutto, |
||
81 | ]; |
||
82 | |||
83 | $fGesamtnettoitems = $aOrder['sumnettoall']; |
||
84 | $aOrder['fVoll'] = $aOrder['sumvoll']; |
||
85 | $aOrder['fErm'] = $aOrder['sumerm']; |
||
86 | $aOrder['fGesamtnetto'] = $aOrder['sumnettoall']; |
||
87 | $aOrder['fSteuervoll'] = $aOrder['taxvoll']; |
||
88 | $aOrder['fSteuererm'] = $aOrder['taxerm']; |
||
89 | $aOrder['fGesamtbrutto'] = $aOrder['sumbruttoall']; |
||
90 | |||
91 | $aOrder['bMindesterreicht'] = true; |
||
92 | $aOrder['fMindergebuehr'] = 0; |
||
93 | $aOrder['iMindergebuehr_id'] = 0; |
||
94 | if ($fGesamtnettoitems < HelperConfig::$shop['minimumorderamountnet']) { |
||
95 | $aOrder['bMindesterreicht'] = false; |
||
96 | $aOrder['iMindergebuehr_id'] = 0; |
||
97 | } elseif ($fGesamtnettoitems < HelperConfig::$shop['reducedorderamountnet1']) { |
||
98 | $aOrder['iMindergebuehr_id'] = 1; |
||
99 | |||
100 | } elseif ($fGesamtnettoitems < HelperConfig::$shop['reducedorderamountnet2']) { |
||
101 | $aOrder['iMindergebuehr_id'] = 2; |
||
102 | } |
||
103 | |||
104 | if ($aOrder['iMindergebuehr_id'] > 0) { |
||
105 | $aOrder['fVoll'] += HelperConfig::$shop['reducedorderamountfee' . $aOrder['iMindergebuehr_id']]; |
||
106 | $aOrder['fGesamtnetto'] += HelperConfig::$shop['reducedorderamountfee' . $aOrder['iMindergebuehr_id']]; |
||
107 | $aOrder['fSteuervoll'] = $aOrder['fVoll'] * $iVATfull / 100; |
||
108 | $aOrder['fGesamtbrutto'] = $aOrder['fGesamtnetto'] + $aOrder['fSteuervoll'] + $aOrder['fSteuererm']; |
||
109 | $aOrder['fMindergebuehr'] = HelperConfig::$shop['reducedorderamountfee' . $aOrder['iMindergebuehr_id']]; |
||
110 | } |
||
111 | |||
112 | $aOrder['fVersandkosten'] = 0; |
||
113 | if ( |
||
114 | isset(HelperConfig::$shop['shippingcoststandardrate']) |
||
115 | && HelperConfig::$shop['shippingcoststandardrate'] != 0 |
||
116 | && |
||
117 | ( |
||
118 | ( |
||
119 | !isset(HelperConfig::$shop['mindestbetragversandfrei']) |
||
120 | || !HelperConfig::$shop['mindestbetragversandfrei'] |
||
121 | ) |
||
122 | || $fGesamtnettoitems < HelperConfig::$shop['mindestbetragversandfrei'] |
||
123 | ) |
||
124 | ) { |
||
125 | $aOrder['fVersandkostennetto'] = self::getShippingcost(); |
||
126 | $aOrder['fVersandkostenvat'] = $aOrder['fVersandkostennetto'] * $iVATfull / 100; |
||
127 | $aOrder['fVersandkostenbrutto'] = $aOrder['fVersandkostennetto'] + $aOrder['fVersandkostenvat']; |
||
128 | |||
129 | $aOrder['fSteuervoll'] = ($aOrder['fVoll'] * $iVATfull / 100) + $aOrder['fVersandkostenvat']; |
||
130 | $aOrder['fVoll'] += $aOrder['fVersandkostennetto']; |
||
131 | $aOrder['fGesamtnetto'] += $aOrder['fVersandkostennetto']; |
||
132 | $aOrder['fGesamtbrutto'] = $aOrder['fGesamtnetto'] + $aOrder['fSteuervoll'] + $aOrder['fSteuererm']; |
||
133 | } |
||
134 | |||
135 | return $aOrder; |
||
136 | } |
||
137 | |||
138 | public static function getShippingcost() |
||
139 | { |
||
140 | $fShippingcost = HelperConfig::$shop['shippingcoststandardrate']; |
||
141 | |||
142 | $sCountry = CHelper::getDefaultCountryByConfig(HelperConfig::$lang); |
||
143 | if (isset($_SESSION['user']['cust_country'])) { |
||
144 | $sCountry = $_SESSION['user']['cust_country']; |
||
145 | } elseif (isset($_POST['doCheckout'], $_POST['country']) && $_POST['doCheckout'] === 'yes') { |
||
146 | $sCountry = trim(\HaaseIT\Toolbox\Tools::getFormfield('country')); |
||
147 | } elseif (isset($_SESSION['formsave_addrform']['country'])) { |
||
148 | $sCountry = $_SESSION['formsave_addrform']['country']; |
||
149 | } |
||
150 | |||
151 | foreach (HelperConfig::$shop['shippingcosts'] as $aValue) { |
||
152 | if (isset($aValue['countries'][$sCountry])) { |
||
153 | $fShippingcost = $aValue['cost']; |
||
154 | break; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | return $fShippingcost; |
||
159 | } |
||
160 | |||
161 | public static function calculateCartItems($aCart) |
||
188 | |||
189 | public static function refreshCartItems(ServiceManager $serviceManager) // bei login/logout ändern sich ggf die preise, shoppingcart neu berechnen |
||
204 | |||
205 | public static function buildShoppingCartTable($aCart, $bReadonly = false, $sCustomergroup = '', $aErr = '', $iVATfull = '', $iVATreduced = '') |
||
237 | |||
238 | public static function getShoppingcartData() |
||
268 | |||
269 | /** |
||
270 | * @param Items $oItem |
||
271 | * @param array $aPossibleSuggestions |
||
272 | * @param string $sSetSuggestions |
||
273 | * @param string $sCurrentitem |
||
274 | * @param string|array $mItemindex |
||
275 | * @param array $itemindexpathtree |
||
276 | * @return array |
||
277 | */ |
||
278 | public static function getItemSuggestions( |
||
328 | |||
329 | /** |
||
330 | * @param string $sSetSuggestions - Items defined as Suggestions in Item config |
||
331 | * @param array $aPossibleSuggestions - Items from the same category |
||
332 | * @param Items $oItem |
||
333 | * @return array |
||
334 | */ |
||
335 | public static function prepareSuggestions($sSetSuggestions, array $aPossibleSuggestions, Items $oItem) |
||
390 | |||
391 | /** |
||
392 | * @param array $suggestions |
||
393 | * @return array |
||
394 | */ |
||
395 | public static function fillSuggestions($suggestions) |
||
443 | |||
444 | public static function handleItemPage(ServiceManager $serviceManager, \HaaseIT\HCSF\Page $P, $aP) |
||
445 | { |
||
446 | $mItemIndex = ''; |
||
447 | if (isset($P->cb_pageconfig->itemindex)) { |
||
448 | $mItemIndex = $P->cb_pageconfig->itemindex; |
||
449 | } |
||
450 | |||
451 | $oItem = $serviceManager->get('oItem'); |
||
452 | |||
453 | $aP['items'] = $oItem->sortItems($mItemIndex, '', ($aP['pagetype'] === 'itemoverviewgrpd')); |
||
454 | if ($aP['pagetype'] === 'itemdetail') { |
||
455 | |||
456 | $aP['itemindexpathtreeforsuggestions'] = $oItem->getItemPathTree(); |
||
457 | |||
458 | if (isset($aP['pageconfig']->itemindex)) { |
||
459 | $aP['itemindexpathtreeforsuggestions'][$aP['pageconfig']->itemindex] = ''; |
||
460 | if (is_array($aP['pageconfig']->itemindex)) { |
||
461 | foreach ($aP['pageconfig']->itemindex as $sItemIndexValue) { |
||
462 | $aP['itemindexpathtreeforsuggestions'][$sItemIndexValue] = ''; |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 | |||
467 | $aP = static::seekItem($P, $aP, $oItem); |
||
468 | } |
||
469 | |||
470 | return $aP; |
||
471 | } |
||
472 | |||
473 | public static function seekItem(\HaaseIT\HCSF\Page $P, $aP, Items $oItem) |
||
526 | |||
527 | public static function renderItemStatusIcon($itemindex) |
||
536 | |||
537 | // todo: when we use twig 2.x, move this to macro |
||
538 | public static function shopadminMakeCheckbox($id) |
||
542 | } |
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: