Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 31 | class Salariu |
||
| 32 | { |
||
| 33 | |||
| 34 | use \danielgp\bank_holidays\Romanian, |
||
| 35 | \danielgp\salariu\FormattingSalariu, |
||
| 36 | \danielgp\salariu\Bonuses, |
||
| 37 | \danielgp\salariu\ForeignCurrency, |
||
| 38 | \danielgp\salariu\Taxation; |
||
| 39 | |||
| 40 | public function __construct() |
||
| 41 | { |
||
| 42 | $configPath = 'Salariu' . DIRECTORY_SEPARATOR . 'config'; |
||
| 43 | $inElmnts = $this->readTypeFromJsonFileUniversal($configPath, 'interfaceElements'); |
||
| 44 | $this->appFlags = [ |
||
| 45 | 'FI' => $inElmnts['Form Input'], |
||
| 46 | 'TCAS' => $inElmnts['Table Cell Applied Style'], |
||
| 47 | 'TCSD' => $inElmnts['Table Cell Style Definitions'], |
||
| 48 | ]; |
||
| 49 | $this->initializeSprGlbAndSession(); |
||
| 50 | $this->handleLocalizationSalariu($inElmnts['Application']); |
||
| 51 | echo $this->setHeaderHtml(); |
||
| 52 | $this->establishLocalizationToDisplay(); |
||
| 53 | $dtR = $this->dateRangesInScope(); |
||
| 54 | $ymValues = $this->buildYMvalues($dtR); |
||
| 55 | $arySts = $this->readTypeFromJsonFileUniversal($configPath, 'valuesToCompute'); |
||
| 56 | echo $this->setFormInput($dtR, $ymValues, $arySts['Minimum Wage'], $inElmnts['Values Filter Rules']); |
||
| 57 | echo $this->setFormOutput($dtR, $arySts, $inElmnts); |
||
| 58 | echo $this->setFooterHtml($inElmnts['Application']); |
||
| 59 | } |
||
| 60 | |||
| 61 | private function getOvertimes($aryStngs) |
||
| 62 | { |
||
| 63 | $pcToBoolean = [0 => true, 1 => false]; |
||
| 64 | $pcBoolean = $pcToBoolean[$this->tCmnSuperGlobals->request->get('pc')]; |
||
| 65 | $ymVal = (string) $this->tCmnSuperGlobals->request->get('ym'); |
||
| 66 | $snVal = $this->tCmnSuperGlobals->request->get('sn'); |
||
| 67 | $mnth = $this->setMonthlyAverageWorkingHours($ymVal, $aryStngs, $pcBoolean); |
||
| 68 | return [ |
||
| 69 | 'os175' => ceil($this->tCmnSuperGlobals->request->get('os175') * 1.75 * $snVal / $mnth), |
||
| 70 | 'os200' => ceil($this->tCmnSuperGlobals->request->get('os200') * 2 * $snVal / $mnth), |
||
| 71 | ]; |
||
| 72 | } |
||
| 73 | |||
| 74 | private function getValues($dtR, $lngBase, $aStngs, $shLabels) |
||
| 75 | { |
||
| 76 | $inDate = $this->tCmnSuperGlobals->request->get('ym'); |
||
| 77 | $aReturn = $this->getValuesPrimary($dtR, $inDate, $lngBase, $aStngs, $shLabels); |
||
| 78 | $pdV = [ |
||
| 79 | ($lngBase + $aReturn['ba']), |
||
| 80 | $this->tCmnSuperGlobals->request->get('pi'), |
||
| 81 | ]; |
||
| 82 | $aReturn['pd'] = $this->setPersonalDeduction($inDate, $pdV[0], $pdV[1], $aStngs['Personal Deduction']); |
||
| 83 | $aryDeductions = [ |
||
| 84 | $this->txLvl['cas'], |
||
| 85 | $this->txLvl['snt'], |
||
| 86 | $this->txLvl['smj'], |
||
| 87 | $aReturn['pd'], |
||
| 88 | ]; |
||
| 89 | $aReturn['impozit'] = $this->getIncomeTaxValue($inDate, $lngBase, $aReturn['ba'], $aryDeductions, $aStngs); |
||
| 90 | return $aReturn; |
||
| 91 | } |
||
| 92 | |||
| 93 | private function getValuesPrimary($dtR, $inDate, $lngBase, $aStngs, $shLbl) |
||
| 94 | { |
||
| 95 | $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]); |
||
| 96 | $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']], $aStngs[$shLbl['HFUL']]); |
||
| 97 | $nMealDays = $this->tCmnSuperGlobals->request->get('nDays'); |
||
| 98 | $unemploymentBase = $lngBase; |
||
| 99 | if ($this->tCmnSuperGlobals->request->get('ym') < 20080101) { |
||
| 100 | $unemploymentBase = $this->tCmnSuperGlobals->request->get('sn'); |
||
| 101 | } |
||
| 102 | $this->setUnemploymentTax($inDate, $unemploymentBase); |
||
| 103 | return [ |
||
| 104 | 'b1' => $this->setFoodTicketsValue($dtR, $inDate, $aStngs[$shLbl['MTV']]), |
||
| 105 | 'ba' => $this->setFoodTicketsValue($dtR, $inDate, $aStngs[$shLbl['MTV']]) * $nMealDays, |
||
| 106 | 'gbns' => $this->tCmnSuperGlobals->request->get('gbns') * pow(10, 4), |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | |||
| 110 | private function getWorkingDays() |
||
| 111 | { |
||
| 112 | $components = [ |
||
| 113 | \DateTime::createFromFormat('Ymd', $this->tCmnSuperGlobals->request->get('ym')), |
||
| 114 | $this->tCmnSuperGlobals->request->get('pc'), |
||
| 115 | ]; |
||
| 116 | $this->tCmnSuperGlobals->request->set('wkDays', $this->setWorkingDaysInMonth($components[0], $components[1])); |
||
|
|
|||
| 117 | $vDays = $this->tCmnSuperGlobals->request->get('wkDays') - $this->tCmnSuperGlobals->request->get('zfb'); |
||
| 118 | $this->tCmnSuperGlobals->request->set('nDays', max($vDays, 0)); |
||
| 119 | } |
||
| 120 | |||
| 121 | private function setFormInput($dtR, $ymValues, $inMW, $inVFR) |
||
| 122 | { |
||
| 123 | $this->applyYMvalidations($this->tCmnSuperGlobals, $ymValues, $dtR); |
||
| 124 | $minWage = $this->determineCrtMinWage($this->tCmnSuperGlobals, [ |
||
| 125 | 'EMW' => $inMW, |
||
| 126 | 'YM range' => $dtR |
||
| 127 | ]); |
||
| 128 | $this->processFormInputDefaults($this->tCmnSuperGlobals, [ |
||
| 129 | 'VFR' => $inVFR, |
||
| 130 | 'Year Month Values' => $ymValues, |
||
| 131 | 'MW' => $minWage, |
||
| 132 | 'YM range' => $dtR, |
||
| 133 | ]); |
||
| 134 | $sReturn = $this->setFormInputElements($ymValues, $minWage); |
||
| 135 | $sReturn[] = $this->setFormInputBottom(); |
||
| 136 | $frm = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [ |
||
| 137 | 'method' => 'get', |
||
| 138 | 'action' => $this->tCmnSuperGlobals->getScriptName() |
||
| 139 | ]); |
||
| 140 | return $this->setFormInputIntoFieldSet($frm); |
||
| 141 | } |
||
| 142 | |||
| 143 | private function setFormInputBottom() |
||
| 144 | { |
||
| 145 | $btn = $this->setStringIntoShortTag('input', [ |
||
| 146 | 'type' => 'submit', |
||
| 147 | 'id' => 'submit', |
||
| 148 | 'value' => $this->tApp->gettext('i18n_Form_Button_Recalculate') |
||
| 149 | ]); |
||
| 150 | return $this->setFormRow($this->setLabel('fd'), $btn, 1) . '</tbody>'; |
||
| 151 | } |
||
| 152 | |||
| 153 | private function setFormInputElements($ymValues, $crtMinWage) |
||
| 154 | { |
||
| 155 | $sReturn = []; |
||
| 156 | $sReturn[] = '<thead><tr><th>' . $this->tApp->gettext('i18n_Form_Label_InputElements') |
||
| 157 | . '</th><th>' . $this->tApp->gettext('i18n_Form_Label_InputValues') . '</th></tr></thead><tbody>'; |
||
| 158 | $sReturn[] = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM($ymValues), 1); |
||
| 159 | $sReturn[] = $this->setFormRow($this->setLabel('sm'), $this->setFormInputText('sm', 10, 'RON', $crtMinWage), 1); |
||
| 160 | $sReturn[] = $this->setFormRow($this->setLabel('sn'), $this->setFormInputText('sn', 10, 'RON'), 1); |
||
| 161 | $sReturn[] = $this->setFormRow($this->setLabel('sc'), $this->setFormInputText('sc', 7, '%'), 1); |
||
| 162 | $sReturn[] = $this->setFormRow($this->setLabel('pb'), $this->setFormInputText('pb', 10, 'RON'), 1); |
||
| 163 | $sReturn[] = $this->setFormRow($this->setLabel('pn'), $this->setFormInputText('pn', 10, 'RON'), 1); |
||
| 164 | $sReturn[] = $this->setFormRow($this->setLabel('os175'), $this->setFormInputText('os175', 2, 'ore'), 1); |
||
| 165 | $sReturn[] = $this->setFormRow($this->setLabel('os200'), $this->setFormInputText('os200', 2, 'ore'), 1); |
||
| 166 | $sReturn[] = $this->setFormRow($this->setLabel('pi'), $this->setFormInputSelectPI(), 1); |
||
| 167 | $sReturn[] = $this->setFormRow($this->setLabel('pc'), $this->setFormInputSelectPC(), 1); |
||
| 168 | $sReturn[] = $this->setFormRow($this->setLabel('szamnt'), $this->setFormInputText('szamnt', 10, 'RON'), 1); |
||
| 169 | $sReturn[] = $this->setFormRow($this->setLabel('zfb'), $this->setFormInputText('zfb', 2, 'zile'), 1); |
||
| 170 | $sReturn[] = $this->setFormRow($this->setLabel('zfs'), $this->setFormInputText('zfs', 2, 'zile'), 1); |
||
| 171 | $sReturn[] = $this->setFormRow($this->setLabel('gbns'), $this->setFormInputText('gbns', 10, 'RON'), 1); |
||
| 172 | $sReturn[] = $this->setFormRow($this->setLabel('afet'), $this->setFormInputText('afet', 10, 'RON'), 1); |
||
| 173 | return $sReturn; |
||
| 174 | } |
||
| 175 | |||
| 176 | private function setFormInputIntoFieldSet($frm) |
||
| 177 | { |
||
| 178 | $aryFieldSet = [ |
||
| 179 | $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'), |
||
| 180 | $frm |
||
| 181 | ]; |
||
| 182 | return $this->setStringIntoTag(implode('', $aryFieldSet), 'fieldset', ['style' => 'float: left;']); |
||
| 183 | } |
||
| 184 | |||
| 185 | private function setFormInputText($inName, $inSize, $inAfterLabel, $crtMinWage = 0) |
||
| 201 | |||
| 202 | private function setFormOutput($dtR, $aryStngs, $inElements) |
||
| 203 | { |
||
| 204 | $sReturn = []; |
||
| 205 | $this->setExchangeRateValues($inElements['Application'], $inElements['Relevant Currencies']); |
||
| 206 | $sReturn[] = $this->setFormOutputHeader(); |
||
| 207 | $ovTimeVal = $this->getOvertimes($aryStngs['Monthly Average Working Hours']); |
||
| 208 | $additions = $this->tCmnSuperGlobals->request->get('pb') + $ovTimeVal['os175'] + $ovTimeVal['os200']; |
||
| 209 | $this->getWorkingDays(); |
||
| 210 | $bComponents = [ |
||
| 211 | 'sc' => $this->tCmnSuperGlobals->request->get('sc'), |
||
| 212 | 'sn' => $this->tCmnSuperGlobals->request->get('sn'), |
||
| 213 | 'zile' => $this->tCmnSuperGlobals->request->get('wkDays'), |
||
| 214 | ]; |
||
| 215 | $xDate = '<span style="font-size:smaller;">' . date('d.m.Y', $this->currencyDetails['CXD']) . '</span>'; |
||
| 216 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('xrate@Date'), $xDate, pow(10, 7)); |
||
| 217 | $snValue = $this->tCmnSuperGlobals->request->get('sn') * pow(10, 4); |
||
| 218 | $amntLAA = round(($this->tCmnSuperGlobals->request->get('zfs') / $bComponents['zile']) * $snValue, -4); |
||
| 219 | $sReturn[] = $this->setFormOutputBonuses($snValue, $bComponents['zile'], $amntLAA, $ovTimeVal); |
||
| 220 | $brut = ($bComponents['sn'] * (1 + $bComponents['sc'] / 100) + $additions) * pow(10, 4) - $amntLAA; |
||
| 221 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('sb'), ' ', $brut); |
||
| 222 | $brut2 = $brut + $this->tCmnSuperGlobals->request->get('afet') * pow(10, 4); |
||
| 223 | $amnt = $this->getValues($dtR, $brut2, $aryStngs, $inElements['Short Labels']); |
||
| 224 | $sReturn[] = $this->setFormOutputTaxations($brut2, $amnt); |
||
| 225 | $pnValue = $this->tCmnSuperGlobals->request->get('pn') * pow(10, 4); |
||
| 226 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('pn'), ' ', $pnValue); |
||
| 227 | $retineri = $this->txLvl['cas'] + $this->txLvl['smj'] + $this->txLvl['snt'] + $amnt['impozit']; |
||
| 228 | $net = $brut2 - $retineri + $this->tCmnSuperGlobals->request->get('pn') * pow(10, 4); |
||
| 229 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('ns'), ' ', $net); |
||
| 230 | $szamntValue = $this->tCmnSuperGlobals->request->get('szamnt') * pow(10, 4); |
||
| 231 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('szamnt'), ' ', $szamntValue); |
||
| 232 | $nsc = $net - $this->tCmnSuperGlobals->request->get('szamnt') * pow(10, 4); |
||
| 233 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('nsc'), ' ', $nsc); |
||
| 234 | $fBonus = [ |
||
| 235 | 'main' => $this->setLabel('gb'), |
||
| 236 | 'value' => $this->tApp->gettext('i18n_Form_Label_FoodBonusesChoiceValue'), |
||
| 237 | 'mtDays' => $this->tCmnSuperGlobals->request->get('nDays') . ' / ' . $bComponents['zile'] |
||
| 238 | ]; |
||
| 239 | $fBonusTxt = sprintf($fBonus['main'], $fBonus['value']); |
||
| 240 | $sReturn[] = $this->setFrmRowTwoLbls($fBonusTxt, $fBonus['mtDays'], $amnt['ba']); |
||
| 241 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('gbns'), ' ', $amnt['gbns']); |
||
| 242 | $total = ($net + $amnt['ba'] + $amnt['gbns'] - $this->tCmnSuperGlobals->request->get('szamnt') * 10000); |
||
| 243 | $sReturn[] = $this->setFrmRowTwoLbls($this->setLabel('total'), ' ', $total); |
||
| 244 | $sReturn[] = '</tbody>'; |
||
| 245 | $tDate = \DateTime::createFromFormat('Ymd', $this->tCmnSuperGlobals->request->get('ym')); |
||
| 246 | $legentText = sprintf($this->tApp->gettext('i18n_FieldsetLabel_Results') |
||
| 247 | . '', strftime('%B', mktime(0, 0, 0, $tDate->format('n'), 1, $tDate->format('Y'))), $tDate->format('Y')); |
||
| 248 | $fieldsetC = $this->setStringIntoTag($legentText, 'legend') |
||
| 249 | . $this->setStringIntoTag(implode('', $sReturn), 'table'); |
||
| 250 | return $this->setStringIntoTag($fieldsetC, 'fieldset', [ |
||
| 251 | 'style' => 'float: left;' |
||
| 252 | ]); |
||
| 253 | } |
||
| 254 | |||
| 255 | private function setFormOutputBonuses($snValue, $wkDays, $amntLAA, $ovTimeVal) |
||
| 278 | |||
| 279 | private function setFormOutputHeader() |
||
| 291 | |||
| 292 | private function setFormOutputTaxations($brut, $amnt) |
||
| 293 | { |
||
| 294 | $sRn = []; |
||
| 295 | $limitDisplayBase = false; |
||
| 296 | View Code Duplication | if ($brut > $this->txLvl['casP_base']) { |
|
| 297 | $limitDisplayBase = true; |
||
| 298 | $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('cas_base'), '', $this->txLvl['casP_base']); |
||
| 299 | } |
||
| 300 | $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('cas'), $this->txLvl['casP'] . '%', $this->txLvl['cas']); |
||
| 301 | $sRn[] = $this->setFrmRowTwoLbls($this->setLabel('somaj'), $this->txLvl['smjP'] . '%', $this->txLvl['smj']); |
||
| 311 | |||
| 312 | private function setFormRow($text, $value, $type = 'amount') |
||
| 330 | |||
| 331 | private function setFormRowAmount($value, $defaultCellStyle) |
||
| 343 | |||
| 344 | private function setFrmRowTwoLbls($text1, $text2, $value) |
||
| 349 | } |
||
| 350 |