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 |
||
8 | class Calculator |
||
9 | { |
||
10 | /** |
||
11 | * @var NewtonRaphson |
||
12 | */ |
||
13 | private $newton; |
||
14 | |||
15 | /** |
||
16 | * |
||
17 | * @param NewtonRaphson $newton |
||
18 | */ |
||
19 | public function __construct(NewtonRaphson $newton = null) |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Get the interest when you know all the payments and their dates. Use this function when you have |
||
27 | * administration fees at the first payment and/or when payments are irregular. |
||
28 | * |
||
29 | * @param int $principal |
||
30 | * @param string $startDate in format 'YYYY-mm-dd' |
||
31 | * @param array $payments array with payment dates and values ['YYYY-mm-dd'=>int] |
||
32 | * @param float $guess A guess what the interest may be. Between zero and one. Example 0.045 |
||
33 | * |
||
34 | * @return float |
||
35 | */ |
||
36 | public function withSpecifiedPayments(int $principal, string $startDate, array $payments, float $guess): float |
||
40 | |||
41 | /** |
||
42 | * Get the effective interest when the monthly payments are exactly the same. |
||
43 | * |
||
44 | * @param int $a The total loan amount (Principal) |
||
45 | * @param int $p The monthly payment |
||
46 | * @param int $n The number of months |
||
47 | * @param float $i A guess of what the interest might be. Interest as a number between zero and one. Example 0.045 |
||
48 | * |
||
49 | * @return float |
||
50 | */ |
||
51 | public function withEqualPayments(int $a, int $p, int $n, float $i): float |
||
63 | } |
||
64 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.