Complex classes like Salariu 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 Salariu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Salariu |
||
32 | { |
||
33 | |||
34 | use \danielgp\bank_holidays\Romanian, |
||
35 | \danielgp\common_lib\CommonCode, |
||
36 | \danielgp\salariu\Bonuses, |
||
37 | \danielgp\salariu\Taxation; |
||
38 | |||
39 | private $appFlags; |
||
40 | private $tApp = null; |
||
41 | |||
42 | public function __construct() |
||
43 | { |
||
44 | $configPath = 'Salariu' . DIRECTORY_SEPARATOR . 'config'; |
||
45 | $interfaceElements = $this->readTypeFromJsonFileUniversal($configPath, 'interfaceElements'); |
||
46 | $this->appFlags = [ |
||
47 | 'FI' => $interfaceElements['Form Input'], |
||
48 | 'TCAS' => $interfaceElements['Table Cell Applied Style'], |
||
49 | 'TCSD' => $interfaceElements['Table Cell Style Definitions'], |
||
50 | ]; |
||
51 | $this->initializeSprGlbAndSession(); |
||
52 | $this->handleLocalizationSalariu($interfaceElements['Application']); |
||
53 | echo $this->setHeaderHtml(); |
||
54 | echo $this->setFormInput(); |
||
55 | $this->refreshExchangeRatesFile($interfaceElements['Application']); |
||
56 | $this->setCurrencyExchangeVariables($interfaceElements['Relevant Currencies']); |
||
57 | $this->getExchangeRates($interfaceElements['Application'], $interfaceElements['Relevant Currencies']); |
||
58 | $aryStngs = $this->readTypeFromJsonFileUniversal($configPath, 'valuesToCompute'); |
||
59 | echo $this->setFormOutput($aryStngs); |
||
60 | echo $this->setFooterHtml($interfaceElements['Application']); |
||
61 | } |
||
62 | |||
63 | private function buildArrayOfFieldsStyled() |
||
71 | |||
72 | private function buildStyleForCellFormat($styleId) |
||
80 | |||
81 | private function getExchangeRates($appSettings, $aryRelevantCurrencies) |
||
106 | |||
107 | private function getOvertimes($aryStngs) |
||
108 | { |
||
109 | $pcToBoolean = [0 => true, 1 => false]; |
||
110 | $pcBoolean = $pcToBoolean[$this->tCmnSuperGlobals->get('pc')]; |
||
111 | $ymVal = $this->tCmnSuperGlobals->get('ym'); |
||
112 | $snVal = $this->tCmnSuperGlobals->get('sn'); |
||
113 | $mnth = $this->setMonthlyAverageWorkingHours($ymVal, $aryStngs, $pcBoolean); |
||
114 | return [ |
||
115 | 'os175' => ceil($this->tCmnSuperGlobals->get('os175') * 1.75 * $snVal / $mnth), |
||
116 | 'os200' => ceil($this->tCmnSuperGlobals->get('os200') * 2 * $snVal / $mnth), |
||
117 | ]; |
||
118 | } |
||
119 | |||
120 | private function getValues($lngBase, $aStngs) |
||
|
|||
121 | { |
||
122 | $inDate = $this->tCmnSuperGlobals->get('ym'); |
||
123 | $inDT = new \DateTime(date('Y/m/d', $inDate)); |
||
124 | $wkDay = $this->setWorkingDaysInMonth($inDT, $_REQUEST['pc']); |
||
125 | $nMealDays = ($wkDay - $_REQUEST['zfb']); |
||
126 | $shLbl = [ |
||
127 | 'HFP' => 'Health Fund Percentage', |
||
128 | 'HFUL' => 'Health Fund Upper Limit', |
||
129 | 'HTP' => 'Health Tax Percentage', |
||
130 | 'IT' => 'Income Tax', |
||
131 | 'MTV' => 'Meal Ticket Value', |
||
132 | ]; |
||
133 | $unemploymentBase = $lngBase; |
||
134 | if ($this->tCmnSuperGlobals->get('ym') < mktime(0, 0, 0, 1, 1, 2008)) { |
||
135 | $unemploymentBase = $_REQUEST['sn']; |
||
136 | } |
||
137 | $aReturn = [ |
||
138 | 'ba' => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]) * $nMealDays, |
||
139 | 'cas' => $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]), |
||
140 | 'sanatate' => $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']]), |
||
141 | 'somaj' => $this->setUnemploymentTax($inDate, $unemploymentBase), |
||
142 | ]; |
||
143 | $pdVal = [ |
||
144 | $inDate, |
||
145 | ($lngBase + $aReturn['ba']), |
||
146 | $_REQUEST['pi'], |
||
147 | $aStngs['Personal Deduction'], |
||
148 | ]; |
||
149 | $aReturn['pd'] = $this->setPersonalDeduction($pdVal[0], $pdVal[1], $pdVal[2], $pdVal[3]); |
||
150 | $restArrayToDeduct = [ |
||
151 | $aReturn['cas'], |
||
152 | $aReturn['sanatate'], |
||
153 | $aReturn['somaj'], |
||
154 | $aReturn['pd'], |
||
155 | ]; |
||
156 | $rest = $lngBase - array_sum($restArrayToDeduct); |
||
157 | if ($inDate >= mktime(0, 0, 0, 7, 1, 2010)) { |
||
158 | $rest += round($aReturn['ba'], -4); |
||
159 | if ($inDate >= mktime(0, 0, 0, 10, 1, 2010)) { |
||
160 | $aReturn['gbns'] = $_REQUEST['gbns'] * pow(10, 4); |
||
161 | $rest += round($aReturn['gbns'], -4); |
||
162 | } |
||
163 | } |
||
164 | $rest += $_REQUEST['afet'] * pow(10, 4); |
||
165 | $aReturn['impozit'] = $this->setIncomeTax($inDate, $rest, $aStngs[$shLbl['IT']]); |
||
166 | $aReturn['zile'] = $wkDay; |
||
167 | return $aReturn; |
||
168 | } |
||
169 | |||
170 | private function handleLocalizationSalariu($appSettings) |
||
171 | { |
||
172 | $this->handleLocalizationSalariuInputsIntoSession($appSettings); |
||
173 | $this->handleLocalizationSalariuSafe($appSettings); |
||
174 | $localizationFile = 'Salariu/locale/' . $this->tCmnSession->get('lang') . '/LC_MESSAGES/salariu.mo'; |
||
175 | $translations = new \Gettext\Translations; |
||
176 | $translations->addFromMoFile($localizationFile); |
||
177 | $this->tApp = new \Gettext\Translator(); |
||
178 | $this->tApp->loadTranslations($translations); |
||
179 | } |
||
180 | |||
181 | private function handleLocalizationSalariuInputsIntoSession($appSettings) |
||
182 | { |
||
183 | if (is_null($this->tCmnSuperGlobals->get('lang')) && is_null($this->tCmnSession->get('lang'))) { |
||
184 | $this->tCmnSession->set('lang', $appSettings['Default Language']); |
||
185 | } elseif (!is_null($this->tCmnSuperGlobals->get('lang'))) { |
||
186 | $this->tCmnSession->set('lang', filter_var($this->tCmnSuperGlobals->get('lang'), FILTER_SANITIZE_STRING)); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * to avoid potential language injections from other applications that do not applies here |
||
192 | */ |
||
193 | private function handleLocalizationSalariuSafe($appSettings) |
||
194 | { |
||
195 | if (!array_key_exists($this->tCmnSession->get('lang'), $appSettings['Available Languages'])) { |
||
196 | $this->tCmnSession->set('lang', $appSettings['Default Language']); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | private function refreshExchangeRatesFile($appSettings) |
||
201 | { |
||
202 | if ((filemtime($appSettings['Exchange Rate Local']) + 90 * 24 * 60 * 60) < time()) { |
||
203 | $fCntnt = file_get_contents($appSettings['Exchange Rate Source']); |
||
204 | if ($fCntnt !== false) { |
||
205 | file_put_contents($appSettings['Exchange Rate Local'], $fCntnt); |
||
206 | chmod($appSettings['Exchange Rate Local'], 0666); |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | private function setCurrencyExchangeVariables($aryRelevantCurrencies) |
||
212 | { |
||
213 | $this->appFlags['currency_exchanges'] = $aryRelevantCurrencies; |
||
214 | $this->appFlags['currency_exchange_rate_date'] = strtotime('now'); |
||
215 | $krncy = array_keys($this->appFlags['currency_exchanges']); |
||
216 | foreach ($krncy as $value) { |
||
217 | $this->appFlags['currency_exchange_rate_value'][$value] = 1; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | private function setFooterHtml($appSettings) |
||
222 | { |
||
223 | $sReturn = $this->setUpperRightBoxLanguages($appSettings['Available Languages']) |
||
224 | . '<div class="resetOnly author">© ' . date('Y') . ' ' |
||
225 | . $appSettings['Copyright Holder'] . '</div>' |
||
226 | . '<hr/>' |
||
227 | . '<div class="disclaimer">' |
||
228 | . $this->tApp->gettext('i18n_Disclaimer') |
||
229 | . '</div>'; |
||
230 | return $this->setFooterCommon($sReturn); |
||
231 | } |
||
232 | |||
233 | private function setFormInput() |
||
234 | { |
||
235 | $sReturn = []; |
||
236 | $sReturn[] = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM(), 1); |
||
237 | $sReturn[] = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1); |
||
238 | $sReturn[] = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 2, '%'), 1); |
||
239 | $sReturn[] = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1); |
||
240 | $sReturn[] = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1); |
||
241 | $sReturn[] = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, ''), 1); |
||
242 | $sReturn[] = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, ''), 1); |
||
243 | $sReturn[] = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1); |
||
244 | $sReturn[] = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1); |
||
245 | $sReturn[] = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 2, ''), 1); |
||
246 | $sReturn[] = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, ''), 1); |
||
247 | $sReturn[] = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 2, ''), 1); |
||
248 | $sReturn[] = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 2, ''), 1); |
||
249 | $fdVal = [ |
||
250 | 'colspan' => 2, |
||
251 | 'style' => 'color: red;' |
||
252 | ]; |
||
253 | $sReturn[] = $this->setStringIntoTag($this->setStringIntoTag($this->setLabel('fd'), 'td', $fdVal), 'tr'); |
||
254 | $submitParams = [ |
||
255 | 'type' => 'submit', |
||
256 | 'id' => 'submit', |
||
257 | 'value' => $this->setLabel('bc') |
||
258 | ]; |
||
259 | $sReturn[] = $this->setFormRow('', $this->setStringIntoShortTag('input', $submitParams), 1); |
||
260 | $frm = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [ |
||
261 | 'method' => 'get', |
||
262 | 'action' => $this->tCmnSuperGlobals->getScriptName() |
||
263 | ]); |
||
264 | $aryFieldSet = [ |
||
265 | $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'), |
||
266 | $frm |
||
267 | ]; |
||
268 | return $this->setStringIntoTag(implode('', $aryFieldSet), 'fieldset', ['style' => 'float: left;']); |
||
269 | } |
||
270 | |||
271 | private function setFormInputSelectPC() |
||
272 | { |
||
273 | $choices = [ |
||
274 | $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceNo'), |
||
275 | $this->tApp->gettext('i18n_Form_Label_CatholicEasterFree_ChoiceYes'), |
||
276 | ]; |
||
277 | return $this->setArrayToSelect($choices, $this->tCmnSuperGlobals->get('pc'), 'pc', ['size' => 1]); |
||
278 | } |
||
279 | |||
280 | private function setFormInputSelectPI() |
||
281 | { |
||
282 | $temp2 = []; |
||
283 | for ($counter = 0; $counter <= 4; $counter++) { |
||
284 | $temp2[$counter] = $counter . ($counter == 4 ? '+' : ''); |
||
285 | } |
||
286 | return $this->setArrayToSelect($temp2, $this->tCmnSuperGlobals->get('pi'), 'pi', ['size' => 1]); |
||
287 | } |
||
288 | |||
289 | private function setFormInputSelectYM() |
||
290 | { |
||
291 | $temp = []; |
||
292 | for ($counter = date('Y'); $counter >= 2001; $counter--) { |
||
293 | for ($counter2 = 12; $counter2 >= 1; $counter2--) { |
||
294 | $crtDate = mktime(0, 0, 0, $counter2, 1, $counter); |
||
295 | if ($crtDate <= mktime(0, 0, 0, date('m'), 1, date('Y'))) { |
||
296 | $temp[$crtDate] = strftime('%Y, %m (%B)', $crtDate); |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | return $this->setArrayToSelect($temp, $this->tCmnSuperGlobals->get('ym'), 'ym', ['size' => 1]); |
||
301 | } |
||
302 | |||
303 | private function setFormInputText($inName, $inSize, $inAfterLabel) |
||
304 | { |
||
305 | $inputParameters = [ |
||
306 | 'type' => 'text', |
||
307 | 'name' => $inName, |
||
308 | 'value' => $this->tCmnSuperGlobals->get($inName), |
||
309 | 'size' => $inSize, |
||
310 | 'maxlength' => $inSize, |
||
311 | ]; |
||
312 | return $this->setStringIntoShortTag('input', $inputParameters) . ' ' . $inAfterLabel; |
||
313 | } |
||
314 | |||
315 | private function setFormOutput($aryStngs) |
||
316 | { |
||
317 | $sReturn = []; |
||
318 | $overtime = $this->getOvertimes($aryStngs['Monthly Average Working Hours']); |
||
319 | $additions = $_REQUEST['pb'] + $overtime['os175'] + $overtime['os200']; |
||
320 | $brut = ($_REQUEST['sn'] * (1 + $_REQUEST['sc'] / 100) + $additions) * pow(10, 4); |
||
321 | $text = $this->tApp->gettext('i18n_Form_Label_ExchangeRateAtDate'); |
||
322 | $xRate = str_replace('%1', date('d.m.Y', $this->appFlags['currency_exchange_rate_date']), $text); |
||
323 | $sReturn[] = $this->setFormRow($xRate, 1000000); |
||
324 | $text = $this->tApp->gettext('i18n_Form_Label_NegotiatedSalary'); |
||
325 | $sReturn[] = $this->setFormRow($text, $_REQUEST['sn'] * 10000); |
||
326 | $prima = $_REQUEST['sn'] * $_REQUEST['sc'] * 100; |
||
327 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_CumulatedAddedValue'), $prima); |
||
328 | $text = $this->tApp->gettext('i18n_Form_Label_AdditionalBruttoAmount'); |
||
329 | $sReturn[] = $this->setFormRow($text, $_REQUEST['pb'] * 10000); |
||
330 | $ovTime = [ |
||
331 | 'main' => $this->tApp->gettext('i18n_Form_Label_OvertimeAmount'), |
||
332 | 1 => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice1'), |
||
333 | 2 => $this->tApp->gettext('i18n_Form_Label_OvertimeChoice2'), |
||
334 | ]; |
||
335 | $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[1], '175%'), ($overtime['os175'] * pow(10, 4))); |
||
336 | $sReturn[] = $this->setFormRow(sprintf($ovTime['main'], $ovTime[2], '200%'), ($overtime['os200'] * pow(10, 4))); |
||
337 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_BruttoSalary'), $brut); |
||
338 | $brut += $_REQUEST['afet'] * pow(10, 4); |
||
339 | $amount = $this->getValues($brut, $aryStngs); |
||
340 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PensionFund'), $amount['cas']); |
||
341 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_UnemploymentTax'), $amount['somaj']); |
||
342 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_HealthTax'), $amount['sanatate']); |
||
343 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_PersonalDeduction'), $amount['pd']); |
||
344 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_ExciseTax'), $amount['impozit']); |
||
345 | $retineri = $amount['cas'] + $amount['somaj'] + $amount['sanatate'] + $amount['impozit']; |
||
346 | $net = $brut - $retineri + $_REQUEST['pn'] * 10000; |
||
347 | $text = $this->tApp->gettext('i18n_Form_Label_AdditionalNettoAmount'); |
||
348 | $sReturn[] = $this->setFormRow($text, $_REQUEST['pn'] * 10000); |
||
349 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_NettoSalary'), $net); |
||
350 | $text = $this->tApp->gettext('i18n_Form_Label_SeisureAmout'); |
||
351 | $sReturn[] = $this->setFormRow($text, $_REQUEST['szamnt'] * 10000); |
||
352 | $text = $this->tApp->gettext('i18n_Form_Label_NettoSalaryCash'); |
||
353 | $sReturn[] = $this->setFormRow($text, ($net - $_REQUEST['szamnt'] * 10000)); |
||
354 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_WorkingDays'), $amount['zile'], 'value'); |
||
355 | $fBonus = [ |
||
356 | 'main' => $this->tApp->gettext('i18n_Form_Label_FoodBonuses'), |
||
357 | 'no' => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceNo'), |
||
358 | 'value' => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue') |
||
359 | ]; |
||
360 | $fBonusTxt = sprintf($fBonus['main'], $fBonus['value'], $fBonus['no'], ($amount['zile'] - $_REQUEST['zfb'])); |
||
361 | $sReturn[] = $this->setFormRow($fBonusTxt, $amount['ba']); |
||
362 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_FoodBonusesValue'), $amount['gbns']); |
||
363 | $total = ($net + $amount['ba'] + $amount['gbns'] - $_REQUEST['szamnt'] * 10000); |
||
364 | $sReturn[] = $this->setFormRow($this->tApp->gettext('i18n_Form_Label_Total'), $total); |
||
365 | setlocale(LC_TIME, explode('_', $this->tCmnSession->get('lang'))[0]); |
||
366 | $crtMonth = strftime('%B', $this->tCmnSuperGlobals->get('ym')); |
||
367 | $legend = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results') |
||
368 | . '', $crtMonth, date('Y', $this->tCmnSuperGlobals->get('ym'))); |
||
369 | return $this->setStringIntoTag(implode('', [ |
||
370 | $this->setStringIntoTag($legend, 'legend'), |
||
371 | $this->setStringIntoTag(implode('', $sReturn), 'table') |
||
372 | ]), 'fieldset', ['style' => 'float: left;']); |
||
373 | } |
||
374 | |||
375 | private function setFormRow($text, $value, $type = 'amount') |
||
376 | { |
||
377 | $a = ''; |
||
378 | $defaultCellStyle = $this->setFormatRow($text, $value); |
||
379 | $defaultCellStyle2 = []; |
||
380 | switch ($type) { |
||
381 | case 'amount': |
||
382 | $value = $value / pow(10, 4); |
||
383 | $defaultCellStyle2['style'] = $defaultCellStyle['style'] . 'text-align:right;'; |
||
384 | $cellValue = []; |
||
385 | foreach ($this->appFlags['currency_exchanges'] as $key2 => $value2) { |
||
386 | $fmt = new \NumberFormatter($value2['locale'], \NumberFormatter::CURRENCY); |
||
387 | $fmt->setAttribute(\NumberFormatter::FRACTION_DIGITS, $value2['decimals']); |
||
388 | $x = $this->appFlags['currency_exchange_rate_value'][$key2]; |
||
389 | $finalValue = $fmt->formatCurrency($value / $x, $key2); |
||
390 | $cellValue[] = $this->setStringIntoTag($finalValue, 'td', $defaultCellStyle2); |
||
391 | } |
||
392 | $value2show = implode('', $cellValue); |
||
393 | break; |
||
394 | case 'value': |
||
395 | $defaultCellStyle2 = array_merge($defaultCellStyle, [ |
||
396 | 'colspan' => count($this->appFlags['currency_exchanges']) |
||
397 | ]); |
||
398 | $value2show = $this->setStringIntoTag($value . $a, 'td', $defaultCellStyle2); |
||
399 | break; |
||
400 | default: |
||
401 | $value2show = $this->setStringIntoTag($value, 'td'); |
||
402 | break; |
||
403 | } |
||
404 | if (!in_array($text, ['', ' ']) && (strpos($text, '<input') === false)) { |
||
405 | $text .= ':'; |
||
406 | } |
||
407 | return $this->setStringIntoTag($this->setStringIntoTag($text, 'td', $defaultCellStyle) . $value2show, 'tr'); |
||
408 | } |
||
409 | |||
410 | private function setFormatRow($text, $value) |
||
424 | |||
425 | private function setHeaderHtml() |
||
438 | |||
439 | private function setLabel($labelId) |
||
456 | } |
||
457 |
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: