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 |
||
21 | class IncomeExpense extends AccountingData |
||
22 | { |
||
23 | |||
24 | |||
25 | /** |
||
26 | * @param int $categoryid |
||
27 | * @param string $income_title |
||
28 | * @param int $amount |
||
29 | * @param string $notes |
||
30 | * @param string $transaction_id |
||
31 | * @return Income |
||
32 | * |
||
33 | * function to add an income and ledger at the same time |
||
34 | */ |
||
35 | public function add_income(int $categoryid, string $income_title, int $amount, string $notes = "", $transaction_id = "0"): Income |
||
48 | |||
49 | /** |
||
50 | * @param int $categoryid |
||
51 | * @param string $income_title |
||
52 | * @param int $amount |
||
53 | * @param string $notes |
||
54 | * @param string $transaction_id |
||
55 | * @return Expense |
||
56 | * |
||
57 | * function to add expense and to a ledger at the sametime |
||
58 | */ |
||
59 | |||
60 | public function add_expense(int $categoryid, string $expense_title, int $amount, string $notes = "", $transaction_id = "0"): Expense |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @param $income |
||
77 | * @return string |
||
78 | * @throws \Exception |
||
79 | * |
||
80 | * function that sets a transaction id |
||
81 | */ |
||
82 | |||
83 | public function set_transaction_id($income) |
||
100 | |||
101 | /** |
||
102 | * @param int $categoryid |
||
103 | * @param string $income_title |
||
104 | * @param int $amount |
||
105 | * @param string $notes |
||
106 | * @return Income |
||
107 | * |
||
108 | * Unit function that adds income |
||
109 | */ |
||
110 | private function newIncome(int $categoryid, string $income_title, int $amount, string $notes = ""): Income |
||
120 | |||
121 | /** |
||
122 | * @param $transactionObj |
||
123 | * @param $type |
||
124 | * @param $transaction_id |
||
125 | * @return Ledger |
||
126 | * |
||
127 | * unit function that adds a ledger transaction |
||
128 | */ |
||
129 | public function add_transaction_on_ledger($transactionObj, $type, $transaction_id): Ledger |
||
140 | |||
141 | /** |
||
142 | * @param int $categoryid |
||
143 | * @param string $expense_title |
||
144 | * @param int $amount |
||
145 | * @param string $notes |
||
146 | * @return Expense |
||
147 | * |
||
148 | * A unit function that adds new expense |
||
149 | */ |
||
150 | |||
151 | private function newExpense(int $categoryid, string $expense_title, int $amount, string $notes = ""): Expense |
||
161 | |||
162 | |||
163 | /** |
||
164 | * @param $categoryname |
||
165 | * @param $description |
||
166 | * @return ExpenseCategory |
||
167 | * |
||
168 | * A unit function that adds expense category |
||
169 | */ |
||
170 | View Code Duplication | public function addExpenseCategory($categoryname, $description): ExpenseCategory |
|
179 | |||
180 | /** |
||
181 | * @param $categoryname |
||
182 | * @param $description |
||
183 | * @return IncomeCategory |
||
184 | * |
||
185 | * A unit function that adds income category |
||
186 | */ |
||
187 | |||
188 | |||
189 | View Code Duplication | public function addIncomeCategory($categoryname, $description): IncomeCategory |
|
199 | |||
200 | |||
201 | public function getExpenseCategoryById($expensecategoryid) |
||
205 | |||
206 | |||
207 | |||
208 | public function getIncomeCategoryById($incomecategoryid) |
||
212 | |||
213 | public function deleteExpenseCategory($expensecategoryid) |
||
216 | |||
217 | |||
218 | public function deleteIncomeCategory($incomecategoryid) |
||
221 | |||
222 | |||
223 | public function updateExpenseCategory($expensecategoryid) |
||
226 | |||
227 | |||
228 | public function updateIncomeCategory($incomecategoryid) |
||
231 | } |
||
232 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.