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 |
||
14 | trait Request |
||
15 | { |
||
16 | protected $data; |
||
17 | protected $options; |
||
18 | |||
19 | /** |
||
20 | * Request a create to API |
||
21 | * |
||
22 | * @param array $payload |
||
23 | * @return Model |
||
24 | */ |
||
25 | public function create($payload) |
||
42 | |||
43 | /** |
||
44 | * Request a create to API |
||
45 | * |
||
46 | * @param array $payload |
||
47 | * @return Model |
||
48 | */ |
||
49 | View Code Duplication | public function find($payload) |
|
65 | |||
66 | /** |
||
67 | * Request a get all to API |
||
68 | * |
||
69 | * @return Model |
||
70 | */ |
||
71 | public function all() |
||
85 | |||
86 | /** |
||
87 | * Enables the webhook |
||
88 | * |
||
89 | * @param Webhook $webhook |
||
90 | * @return Model |
||
91 | */ |
||
92 | View Code Duplication | public function enable(Webhook $webhook) |
|
106 | |||
107 | /** |
||
108 | * Disables the webhook |
||
109 | * |
||
110 | * @param Webhook $webhook |
||
111 | * @return Model |
||
112 | */ |
||
113 | View Code Duplication | public function disable(Webhook $webhook) |
|
127 | |||
128 | /** |
||
129 | * Updates the webhook |
||
130 | * |
||
131 | * @param Webhook $webhook |
||
132 | * @param array $payload |
||
133 | * @return Model |
||
134 | */ |
||
135 | View Code Duplication | public function update(Webhook $webhook, array $payload) |
|
152 | |||
153 | /** |
||
154 | * Cancels the payment intent |
||
155 | * |
||
156 | * @param PaymentIntent $intent |
||
157 | * @return Model |
||
158 | */ |
||
159 | public function cancel(PaymentIntent $intent) |
||
173 | |||
174 | /** |
||
175 | * Attach the payment method in the payment intent |
||
176 | * |
||
177 | * @param PaymentIntent $intent |
||
178 | * @param string $paymentMethodId |
||
179 | * @return Model |
||
180 | */ |
||
181 | View Code Duplication | public function attach(PaymentIntent $intent, $paymentMethodId) |
|
198 | |||
199 | /** |
||
200 | * Send request to API |
||
201 | * |
||
202 | * @return mixed|Throwable |
||
203 | */ |
||
204 | protected function request() |
||
235 | |||
236 | /** |
||
237 | * Sets the data to add data wrapper of the payload |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | protected function formRequestData() |
||
249 | |||
250 | protected function parseToArray($jsonString) |
||
254 | |||
255 | protected function setReturnModel($array) |
||
259 | |||
260 | protected function setOptions($options) |
||
264 | |||
265 | protected function convertPayloadAmountsToInteger($payload) |
||
274 | } |
||
275 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: