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 |
||
28 | View Code Duplication | class LuckyMoney |
|
2 ignored issues
–
show
|
|||
29 | { |
||
30 | /** |
||
31 | * @var API |
||
32 | */ |
||
33 | protected $api; |
||
34 | |||
35 | /** |
||
36 | * Merchant instance. |
||
37 | * |
||
38 | * @var \EasyWeChat\Payment\Merchant |
||
39 | */ |
||
40 | protected $merchant; |
||
41 | |||
42 | /** |
||
43 | * Constructor. |
||
44 | * |
||
45 | * @param Merchant $merchant |
||
46 | */ |
||
47 | public function __construct(Merchant $merchant) |
||
51 | |||
52 | /** |
||
53 | * Merchant setter. |
||
54 | * |
||
55 | * @param Merchant $merchant |
||
56 | */ |
||
57 | public function setMerchant(Merchant $merchant) |
||
61 | |||
62 | /** |
||
63 | * Merchant getter. |
||
64 | * |
||
65 | * @return Merchant |
||
66 | */ |
||
67 | public function getMerchant() |
||
71 | |||
72 | /** |
||
73 | * API setter. |
||
74 | * |
||
75 | * @param API $api |
||
76 | */ |
||
77 | public function setAPI(API $api) |
||
81 | |||
82 | /** |
||
83 | * Return API instance. |
||
84 | * |
||
85 | * @return API |
||
86 | */ |
||
87 | public function getAPI() |
||
91 | |||
92 | /** |
||
93 | * Magic call. |
||
94 | * |
||
95 | * @param string $method |
||
96 | * @param array $args |
||
97 | * |
||
98 | * @return mixed |
||
99 | * |
||
100 | * @codeCoverageIgnore |
||
101 | */ |
||
102 | public function __call($method, $args) |
||
108 | } |
||
109 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.