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 |
||
16 | class DebtAmortizator extends CalculatorAbstract |
||
17 | { |
||
18 | /** @var RepaymentInstance[] */ |
||
19 | // list of individual debt's repayments as an array of RepaymentInstance objects |
||
20 | protected $debtRepayments; |
||
21 | |||
22 | // principal of the debt = 'PV' |
||
23 | protected $debtPrincipal; |
||
24 | // number of periods pertaining to the interest compounding = 'n' |
||
25 | protected $debtNoOfCompoundingPeriods; |
||
26 | // length of a single period in days |
||
27 | /** @var TimeSpan */ |
||
28 | protected $debtPeriodLength; |
||
29 | // the interest rate by which the unpaid balance is multiplied (i.e., a decimal number) = 'i' |
||
30 | protected $debtInterest; |
||
31 | |||
32 | // props returned by the getResultAsArray method by default |
||
33 | protected $propResultArray = [ |
||
34 | "debtPrincipal", |
||
35 | "debtNoOfCompoundingPeriods", |
||
36 | "debtPeriodLength" => |
||
37 | [ |
||
38 | "years" => "debtPeriodLengthInYears", |
||
39 | "months" => "debtPeriodLengthInMonths", |
||
40 | "days" => "debtPeriodLengthInDays" |
||
41 | ], |
||
42 | "debtInterest", |
||
43 | "debtDiscountFactor", |
||
44 | "debtLength" => |
||
45 | [ |
||
46 | "years" => "debtLengthInYears", |
||
47 | "months" => "debtLengthInMonths", |
||
48 | "days" => "debtLengthInDays" |
||
49 | ], |
||
50 | "debtSingleRepayment", |
||
51 | "debtSingleRepaymentRounded", |
||
52 | "debtRepayments" => "debtRepaymentsAsArrays", |
||
53 | "debtRepaymentsRounded" => "debtRepaymentsAsArraysRounded" |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @param $debtPrincipal |
||
58 | * @param $debtNoOfCompoundingPeriods |
||
59 | * @param TimeSpan $debtPeriodLength |
||
60 | * @param $debtInterest |
||
61 | */ |
||
62 | public function __construct( |
||
74 | |||
75 | /** |
||
76 | * @param $debtPrincipal |
||
77 | */ |
||
78 | private function setDebtPrincipalWithoutRecalculation($debtPrincipal) |
||
82 | |||
83 | /** |
||
84 | * @param $debtNoOfCompoundingPeriods |
||
85 | */ |
||
86 | private function setDebtNoOfCompoundingPeriodsWithoutRecalculation($debtNoOfCompoundingPeriods) |
||
90 | |||
91 | /** |
||
92 | * @param $debtInterest |
||
93 | */ |
||
94 | private function setDebtInterestWithoutRecalculation($debtInterest) |
||
98 | |||
99 | /** |
||
100 | * @param $debtPrincipal |
||
101 | */ |
||
102 | public function setDebtPrincipal($debtPrincipal) |
||
107 | |||
108 | /** |
||
109 | * @param $debtNoOfCompoundingPeriods |
||
110 | */ |
||
111 | public function setDebtNoOfCompoundingPeriods($debtNoOfCompoundingPeriods) |
||
116 | |||
117 | /** |
||
118 | * @param $debtPeriodLength |
||
119 | */ |
||
120 | public function setDebtPeriodLength(TimeSpan $debtPeriodLength) |
||
124 | |||
125 | /** |
||
126 | * @param $debtInterest |
||
127 | */ |
||
128 | public function setDebtInterest($debtInterest) |
||
133 | |||
134 | /** |
||
135 | * Private function populating the $debtRepayments array which represents the amortization schedule |
||
136 | * constructed on basis of the initial parameters passed to the constructor |
||
137 | */ |
||
138 | private function calculateDebtRepayments() |
||
156 | |||
157 | /** |
||
158 | * @return string [Value of the debt principal as a string] |
||
159 | */ |
||
160 | public function getDebtPrincipal() |
||
164 | |||
165 | /** |
||
166 | * @return string [Number of the debt's compounding periods as a string] |
||
167 | */ |
||
168 | public function getDebtNoOfCompoundingPeriods() |
||
172 | |||
173 | /** |
||
174 | * @return TimeSpan |
||
175 | */ |
||
176 | public function getDebtPeriodLength() |
||
180 | |||
181 | /** |
||
182 | * @return string [Length of each of the debt's compounding periods in years as a string] |
||
183 | */ |
||
184 | public function getDebtPeriodLengthInYears() |
||
188 | |||
189 | /** |
||
190 | * @return string [Length of each of the debt's compounding periods in months as a string] |
||
191 | */ |
||
192 | public function getDebtPeriodLengthInMonths() |
||
196 | |||
197 | /** |
||
198 | * @return string [Length of each of the debt's compounding periods in days as a string] |
||
199 | */ |
||
200 | public function getDebtPeriodLengthInDays() |
||
204 | |||
205 | /** |
||
206 | * @return string [Value of the debt's interest in a decimal number 'multiplier' form as a string] |
||
207 | */ |
||
208 | public function getDebtInterest() |
||
212 | |||
213 | /** |
||
214 | * @return string [Value of the debt's discount factor as a string] |
||
215 | */ |
||
216 | public function getDebtDiscountFactor() |
||
228 | |||
229 | /** |
||
230 | * @return string [Length of the debt in years as a string] |
||
231 | */ |
||
232 | public function getDebtLengthInYears() |
||
239 | |||
240 | /** |
||
241 | * @return string [Length of the debt in months as a string] |
||
242 | */ |
||
243 | public function getDebtLengthInMonths() |
||
250 | |||
251 | /** |
||
252 | * @return string [Length of the debt in years as a string] |
||
253 | */ |
||
254 | public function getDebtLengthInDays() |
||
261 | |||
262 | /** |
||
263 | * @param DateTime $startDate [The start date of the debt] |
||
264 | * @return DateTime [The end date of the debt] |
||
265 | */ |
||
266 | public function getDebtEndDate(DateTime $startDate) |
||
272 | |||
273 | /** |
||
274 | * @return string [Value of a single debt repayment instance as a string] |
||
275 | */ |
||
276 | View Code Duplication | public function getDebtSingleRepayment() |
|
293 | |||
294 | /** |
||
295 | * @return string [Value of a single debt repayment instance as a string] |
||
296 | */ |
||
297 | View Code Duplication | public function getDebtSingleRepaymentRounded() { |
|
298 | // single repayment 'K = PV/((1-v^n)/i)' |
||
299 | return MathFuncs::roundUp( |
||
300 | MathFuncs::div( |
||
301 | $this->debtPrincipal, |
||
302 | MathFuncs::div( |
||
303 | MathFuncs::sub( |
||
304 | 1, |
||
305 | MathFuncs::pow( |
||
306 | $this->getDebtDiscountFactor(), |
||
307 | $this->debtNoOfCompoundingPeriods |
||
308 | ) |
||
309 | ), |
||
310 | $this->debtInterest |
||
311 | ) |
||
312 | ), 2); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @return RepaymentInstance[] [Array of individual debt repayments (RepaymentInstances)] |
||
317 | */ |
||
318 | public function getDebtRepayments() |
||
322 | |||
323 | /** |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getDebtRepaymentsAsArrays() |
||
340 | |||
341 | /** |
||
342 | * @return array |
||
343 | */ |
||
344 | public function getDebtRepaymentsAsArraysRounded() |
||
376 | |||
377 | } |
||
378 | |||
425 |
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.