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 |
||
9 | class Wallet extends Model |
||
10 | { |
||
11 | protected $fillable = [ |
||
12 | 'user_id', |
||
13 | 'user_type', |
||
14 | 'wallet_type_id', |
||
15 | 'raw_balance', |
||
16 | ]; |
||
17 | |||
18 | public function user() |
||
22 | |||
23 | public function walletType() |
||
27 | |||
28 | public function walletLedgers() |
||
32 | |||
33 | public function getBalanceAttribute() |
||
41 | |||
42 | /** |
||
43 | * @param $transaction WalletTransaction|integer|float|double |
||
44 | * @return Wallet |
||
45 | * @throws Exception |
||
46 | */ |
||
47 | View Code Duplication | public function incrementBalance($transaction) |
|
68 | |||
69 | /** |
||
70 | * @param $transaction WalletTransaction|integer|float|double |
||
71 | * @return Wallet |
||
72 | * @throws Exception |
||
73 | */ |
||
74 | View Code Duplication | public function decrementBalance($transaction) |
|
95 | |||
96 | /** |
||
97 | * @param $transaction |
||
98 | * @param $newRunningRawBalance |
||
99 | * @param string $type |
||
100 | * @return mixed |
||
101 | * @throws Exception |
||
102 | */ |
||
103 | private function createWalletLedgerEntry($transaction, $newRunningRawBalance, $type = 'increment') |
||
136 | |||
137 | /** |
||
138 | * Converts the given value to an integer that is compatible with this wallet's type. |
||
139 | * |
||
140 | * @param int $value |
||
141 | * @return float|int |
||
142 | */ |
||
143 | private function convertToWalletTypeInteger($value) |
||
151 | } |
||
152 |
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.