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 |
||
18 | class WalletValidator |
||
19 | { |
||
20 | /** @var array */ |
||
21 | protected $wallets = [ |
||
22 | 'GooglePay', |
||
23 | 'ApplePay', |
||
24 | ]; |
||
25 | |||
26 | /** @var array */ |
||
27 | protected $brands = [ |
||
28 | 'Mastercard', |
||
29 | 'Visa', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * @var Wallet |
||
34 | */ |
||
35 | private $wallet; |
||
36 | |||
37 | /** |
||
38 | * WalletValidator constructor. |
||
39 | * @param Wallet $wallet |
||
40 | */ |
||
41 | public function __construct(Wallet $wallet) |
||
45 | |||
46 | /** |
||
47 | * This validate the wallet data |
||
48 | */ |
||
49 | public function validate(): void |
||
55 | |||
56 | /** |
||
57 | * @return void |
||
58 | * @throws \InvalidArgumentException |
||
59 | */ |
||
60 | public function proxy() |
||
67 | |||
68 | /** |
||
69 | * @return void |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | View Code Duplication | public function wallet() |
|
83 | |||
84 | /** |
||
85 | * @return void |
||
86 | * @throws \InvalidArgumentException |
||
87 | */ |
||
88 | View Code Duplication | public function brand() |
|
99 | } |
||
100 |
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.