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 |
||
20 | class TransactionResponse { |
||
21 | /** |
||
22 | * Unique transaction ID given to each transaction. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | public $id; |
||
27 | |||
28 | /** |
||
29 | * Action. |
||
30 | * |
||
31 | * Actual action performed. In case a separate capture is NOT possible, the transaction will be interpreted and performed as a payment transaction and cannot be cancelled, but can be refunded and with result=0 considered as a “successful payment". |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $action; |
||
36 | |||
37 | /** |
||
38 | * Your unique transaction reference. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $tracking_code; |
||
43 | |||
44 | /** |
||
45 | * Amount of the transaction. |
||
46 | * |
||
47 | * @var string|float|int |
||
48 | */ |
||
49 | private $amount; |
||
50 | |||
51 | /** |
||
52 | * Currency code of the amount of the transaction. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $currency_code; |
||
57 | |||
58 | /** |
||
59 | * Construct and initialize transaction response. |
||
60 | * |
||
61 | * @param string $action Action. |
||
62 | * @param string $id ID. |
||
63 | * @param string $tracking_code Tracking code. |
||
64 | * @param string|float|int $amount Amount. |
||
65 | * @param string $currency_code Currency code. |
||
66 | */ |
||
67 | 4 | View Code Duplication | public function __construct( $action, $id, $tracking_code, $amount, $currency_code ) { |
74 | |||
75 | /** |
||
76 | * From JSON. |
||
77 | * |
||
78 | * @link https://github.com/WordPress/wp-notify/blob/develop/includes/JsonUnserializable.php |
||
79 | * @param object $object Object. |
||
80 | * @return self |
||
81 | * @throws \JsonSchema\Exception\ValidationException Throws exception when JSON is not valid. |
||
82 | */ |
||
83 | 3 | View Code Duplication | public static function from_json( $object ) { |
97 | |||
98 | /** |
||
99 | * Get action. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | public function get_action() { |
|
106 | |||
107 | /** |
||
108 | * Get id. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 1 | public function get_id() { |
|
115 | |||
116 | /** |
||
117 | * Get tracking code. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 1 | public function get_tracking_code() { |
|
124 | |||
125 | /** |
||
126 | * Get amount. |
||
127 | * |
||
128 | * @return string|float|int |
||
129 | */ |
||
130 | 1 | public function get_amount() { |
|
133 | |||
134 | /** |
||
135 | * Get currency code. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 1 | public function get_currency_code() { |
|
142 | } |
||
143 |
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.