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\InputValidation, |
||
36 | \danielgp\salariu\FormattingSalariu, |
||
37 | \danielgp\salariu\Bonuses, |
||
38 | \danielgp\salariu\ForeignCurrency, |
||
39 | \danielgp\salariu\Taxation; |
||
40 | |||
41 | public function __construct() |
||
42 | { |
||
43 | $configPath = 'Salariu' . DIRECTORY_SEPARATOR . 'config'; |
||
44 | $interfaceElements = $this->readTypeFromJsonFileUniversal($configPath, 'interfaceElements'); |
||
45 | $this->appFlags = [ |
||
46 | 'FI' => $interfaceElements['Form Input'], |
||
47 | 'TCAS' => $interfaceElements['Table Cell Applied Style'], |
||
48 | 'TCSD' => $interfaceElements['Table Cell Style Definitions'], |
||
49 | ]; |
||
50 | $this->initializeSprGlbAndSession(); |
||
51 | $this->handleLocalizationSalariu($interfaceElements['Application']); |
||
52 | echo $this->setHeaderHtml(); |
||
53 | $ymValues = $this->buildYMvalues(); |
||
54 | $this->processFormInputDefaults($this->tCmnSuperGlobals, $interfaceElements['Values Filter Rules'], $ymValues); |
||
55 | echo $this->setFormInput($ymValues); |
||
56 | $this->setExchangeRateValues($interfaceElements['Application'], $interfaceElements['Relevant Currencies']); |
||
57 | echo $this->setFormOutput($configPath, $interfaceElements['Short Labels']); |
||
58 | echo $this->setFooterHtml($interfaceElements['Application']); |
||
59 | } |
||
60 | |||
61 | private function getIncomeTaxValue($inDate, $lngBase, $vBA, $aryDeductions, $arySettings) |
||
62 | { |
||
63 | $rest = $lngBase - array_sum($aryDeductions); |
||
64 | if ($inDate >= mktime(0, 0, 0, 7, 1, 2010)) { |
||
65 | $rest += round($vBA, -4); |
||
66 | if ($inDate >= mktime(0, 0, 0, 10, 1, 2010)) { |
||
67 | $rest += round($this->tCmnSuperGlobals->request->get('gbns') * pow(10, 4), -4); |
||
68 | } |
||
69 | } |
||
70 | $rest += $this->tCmnSuperGlobals->request->get('afet') * pow(10, 4); |
||
71 | return $this->setIncomeTax($inDate, $rest, $arySettings['Income Tax']); |
||
72 | } |
||
73 | |||
74 | private function getOvertimes($aryStngs) |
||
75 | { |
||
76 | $pcToBoolean = [0 => true, 1 => false]; |
||
77 | $pcBoolean = $pcToBoolean[$this->tCmnSuperGlobals->request->get('pc')]; |
||
78 | $ymVal = $this->tCmnSuperGlobals->request->get('ym'); |
||
79 | $snVal = $this->tCmnSuperGlobals->request->get('sn'); |
||
80 | $mnth = $this->setMonthlyAverageWorkingHours($ymVal, $aryStngs, $pcBoolean); |
||
81 | return [ |
||
82 | 'os175' => ceil($this->tCmnSuperGlobals->request->get('os175') * 1.75 * $snVal / $mnth), |
||
83 | 'os200' => ceil($this->tCmnSuperGlobals->request->get('os200') * 2 * $snVal / $mnth), |
||
84 | ]; |
||
85 | } |
||
86 | |||
87 | private function getValues($lngBase, $aStngs, $shLabels) |
||
88 | { |
||
89 | $inDate = $this->tCmnSuperGlobals->request->get('ym'); |
||
90 | $aReturn = $this->getValuesPrimary($inDate, $lngBase, $aStngs, $shLabels); |
||
91 | $pdV = [ |
||
92 | ($lngBase + $aReturn['ba']), |
||
93 | $this->tCmnSuperGlobals->request->get('pi'), |
||
94 | ]; |
||
95 | $aReturn['pd'] = $this->setPersonalDeduction($inDate, $pdV[0], $pdV[1], $aStngs['Personal Deduction']); |
||
96 | $aryDeductions = [ |
||
97 | $this->txLvl['cas'], |
||
98 | $this->txLvl['snt'], |
||
99 | $this->txLvl['smj'], |
||
100 | $aReturn['pd'], |
||
101 | ]; |
||
102 | $aReturn['impozit'] = $this->getIncomeTaxValue($inDate, $lngBase, $aReturn['ba'], $aryDeductions, $aStngs); |
||
103 | return $aReturn; |
||
104 | } |
||
105 | |||
106 | private function getValuesPrimary($inDate, $lngBase, $aStngs, $shLbl) |
||
107 | { |
||
108 | $this->setHealthFundTax($inDate, $lngBase, $aStngs[$shLbl['HFP']], $aStngs[$shLbl['HFUL']]); |
||
109 | $this->setHealthTax($inDate, $lngBase, $aStngs[$shLbl['HTP']], $aStngs[$shLbl['HFUL']]); |
||
110 | $nMealDays = $this->tCmnSuperGlobals->request->get('nDays'); |
||
111 | $unemploymentBase = $lngBase; |
||
112 | if ($this->tCmnSuperGlobals->request->get('ym') < mktime(0, 0, 0, 1, 1, 2008)) { |
||
113 | $unemploymentBase = $this->tCmnSuperGlobals->request->get('sn'); |
||
114 | } |
||
115 | $this->setUnemploymentTax($inDate, $unemploymentBase); |
||
116 | return [ |
||
117 | 'b1' => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]), |
||
118 | 'ba' => $this->setFoodTicketsValue($inDate, $aStngs[$shLbl['MTV']]) * $nMealDays, |
||
119 | 'gbns' => $this->tCmnSuperGlobals->request->get('gbns') * pow(10, 4), |
||
120 | ]; |
||
121 | } |
||
122 | |||
123 | private function getWorkingDays() |
||
124 | { |
||
125 | $components = [ |
||
126 | new \DateTime(date('Y/m/d', $this->tCmnSuperGlobals->request->get('ym'))), |
||
127 | $this->tCmnSuperGlobals->request->get('pc'), |
||
128 | ]; |
||
129 | $this->tCmnSuperGlobals->request->set('wkDays', $this->setWorkingDaysInMonth($components[0], $components[1])); |
||
130 | $vDays = $this->tCmnSuperGlobals->request->get('wkDays') - $this->tCmnSuperGlobals->request->get('zfb'); |
||
131 | $this->tCmnSuperGlobals->request->set('nDays', max($vDays, 0)); |
||
132 | } |
||
133 | |||
134 | private function setFormInput($ymValues) |
||
135 | { |
||
136 | $sReturn = $this->setFormInputElements($ymValues); |
||
137 | $btn = $this->setStringIntoShortTag('input', [ |
||
138 | 'type' => 'submit', |
||
139 | 'id' => 'submit', |
||
140 | 'value' => $this->tApp->gettext('i18n_Form_Button_Recalculate') |
||
141 | ]); |
||
142 | $sReturn[] = $this->setFormRow($this->setLabel('fd'), $btn, 1) . '</tbody>'; |
||
143 | $frm = $this->setStringIntoTag($this->setStringIntoTag(implode('', $sReturn), 'table'), 'form', [ |
||
144 | 'method' => 'get', |
||
145 | 'action' => $this->tCmnSuperGlobals->getScriptName() |
||
146 | ]); |
||
147 | $aryFieldSet = [ |
||
148 | $this->setStringIntoTag($this->tApp->gettext('i18n_FieldsetLabel_Inputs'), 'legend'), |
||
149 | $frm |
||
150 | ]; |
||
151 | return $this->setStringIntoTag(implode('', $aryFieldSet), 'fieldset', ['style' => 'float: left;']); |
||
152 | } |
||
153 | |||
154 | private function setFormInputElements($ymValues) |
||
155 | { |
||
156 | $sReturn = []; |
||
157 | $sReturn[] = '<thead><tr><th>' . $this->tApp->gettext('i18n_Form_Label_InputElements') |
||
158 | . '</th><th>' . $this->tApp->gettext('i18n_Form_Label_InputValues') . '</th></tr></thead><tbody>'; |
||
159 | $sReturn[] = $this->setFormRow($this->setLabel('ym'), $this->setFormInputSelectYM($ymValues), 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 setFormInputText($inName, $inSize, $inAfterLabel) |
||
187 | |||
188 | private function setFormOutput($configPath, $shLabels) |
||
189 | { |
||
190 | $aryStngs = $this->readTypeFromJsonFileUniversal($configPath, 'valuesToCompute'); |
||
191 | $sReturn = []; |
||
192 | $sReturn[] = $this->setFormOutputHeader(); |
||
193 | $ovTimeVal = $this->getOvertimes($aryStngs['Monthly Average Working Hours']); |
||
194 | $additions = $this->tCmnSuperGlobals->request->get('pb') + $ovTimeVal['os175'] + $ovTimeVal['os200']; |
||
195 | $this->getWorkingDays(); |
||
241 | |||
242 | private function setFormOutputBonuses($snValue, $wkDays, $amntLAA, $ovTimeVal) |
||
263 | |||
264 | private function setFormOutputHeader() |
||
276 | |||
277 | private function setFormOutputTaxations($brut, $amnt) |
||
295 | |||
296 | private function setFormRow($text, $value, $type = 'amount') |
||
314 | |||
315 | private function setFormRowAmount($value, $defaultCellStyle) |
||
327 | |||
328 | private function setFrmRowTwoLbls($text1, $text2, $value) |
||
333 | } |
||
334 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.