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 |
||
19 | abstract class WC_Payment_Gateway extends WC_Settings_API { |
||
20 | |||
21 | /** |
||
22 | * Set if the place order button should be renamed on selection. |
||
23 | * @var string |
||
24 | */ |
||
25 | public $order_button_text; |
||
26 | |||
27 | /** |
||
28 | * yes or no based on whether the method is enabled. |
||
29 | * @var string |
||
30 | */ |
||
31 | public $enabled = 'yes'; |
||
32 | |||
33 | /** |
||
34 | * Payment method title for the frontend. |
||
35 | * @var string |
||
36 | */ |
||
37 | public $title; |
||
38 | |||
39 | /** |
||
40 | * Payment method description for the frontend. |
||
41 | * @var string |
||
42 | */ |
||
43 | public $description; |
||
44 | |||
45 | /** |
||
46 | * Chosen payment method id. |
||
47 | * @var bool |
||
48 | */ |
||
49 | public $chosen; |
||
50 | |||
51 | /** |
||
52 | * Gateway title. |
||
53 | * @var string |
||
54 | */ |
||
55 | public $method_title = ''; |
||
56 | |||
57 | /** |
||
58 | * Gateway description. |
||
59 | * @var string |
||
60 | */ |
||
61 | public $method_description = ''; |
||
62 | |||
63 | /** |
||
64 | * True if the gateway shows fields on the checkout. |
||
65 | * @var bool |
||
66 | */ |
||
67 | public $has_fields; |
||
68 | |||
69 | /** |
||
70 | * Countries this gateway is allowed for. |
||
71 | * @var array |
||
72 | */ |
||
73 | public $countries; |
||
74 | |||
75 | /** |
||
76 | * Available for all counties or specific. |
||
77 | * @var string |
||
78 | */ |
||
79 | public $availability; |
||
80 | |||
81 | /** |
||
82 | * Icon for the gateway. |
||
83 | * @var string |
||
84 | */ |
||
85 | public $icon; |
||
86 | |||
87 | /** |
||
88 | * Supported features such as 'default_credit_card_form', 'refunds'. |
||
89 | * @var array |
||
90 | */ |
||
91 | public $supports = array( 'products' ); |
||
92 | |||
93 | /** |
||
94 | * Maximum transaction amount, zero does not define a maximum. |
||
95 | * @var int |
||
96 | */ |
||
97 | public $max_amount = 0; |
||
98 | |||
99 | /** |
||
100 | * Optional URL to view a transaction. |
||
101 | * @var string |
||
102 | */ |
||
103 | public $view_transaction_url = ''; |
||
104 | |||
105 | /** |
||
106 | * Return the title for admin screens. |
||
107 | * @return string |
||
108 | */ |
||
109 | public function get_method_title() { |
||
112 | |||
113 | /** |
||
114 | * Return the description for admin screens. |
||
115 | * @return string |
||
116 | */ |
||
117 | public function get_method_description() { |
||
120 | |||
121 | /** |
||
122 | * Output the gateway settings screen. |
||
123 | */ |
||
124 | public function admin_options() { |
||
129 | |||
130 | /** |
||
131 | * Get the return url (thank you page). |
||
132 | * |
||
133 | * @param WC_Order $order |
||
134 | * @return string |
||
135 | */ |
||
136 | public function get_return_url( $order = null ) { |
||
149 | |||
150 | /** |
||
151 | * Get a link to the transaction on the 3rd party gateway size (if applicable). |
||
152 | * |
||
153 | * @param WC_Order $order the order object. |
||
154 | * @return string transaction URL, or empty string. |
||
155 | */ |
||
156 | public function get_transaction_url( $order ) { |
||
167 | |||
168 | /** |
||
169 | * Get the order total in checkout and pay_for_order. |
||
170 | * |
||
171 | * @return float |
||
172 | */ |
||
173 | protected function get_order_total() { |
||
190 | |||
191 | /** |
||
192 | * Check if the gateway is available for use. |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function is_available() { |
||
206 | |||
207 | /** |
||
208 | * has_fields function. |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function has_fields() { |
||
215 | |||
216 | /** |
||
217 | * Return the gateway's title. |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function get_title() { |
||
224 | |||
225 | /** |
||
226 | * Return the gateway's description. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public function get_description() { |
||
233 | |||
234 | /** |
||
235 | * get_icon function. |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function get_icon() { |
||
245 | |||
246 | /** |
||
247 | * Set as current gateway. |
||
248 | * |
||
249 | * Set this as the current gateway. |
||
250 | */ |
||
251 | public function set_current() { |
||
254 | |||
255 | /** |
||
256 | * Process Payment. |
||
257 | * |
||
258 | * Process the payment. Override this in your gateway. When implemented, this should. |
||
259 | * return the success and redirect in an array. e.g: |
||
260 | * |
||
261 | * return array( |
||
262 | * 'result' => 'success', |
||
263 | * 'redirect' => $this->get_return_url( $order ) |
||
264 | * ); |
||
265 | * |
||
266 | * @param int $order_id |
||
267 | * @return array |
||
268 | */ |
||
269 | public function process_payment( $order_id ) { |
||
272 | |||
273 | /** |
||
274 | * Process refund. |
||
275 | * |
||
276 | * If the gateway declares 'refunds' support, this will allow it to refund. |
||
277 | * a passed in amount. |
||
278 | * |
||
279 | * @param int $order_id |
||
280 | * @param float $amount |
||
281 | * @param string $reason |
||
282 | * @return bool|WP_Error True or false based on success, or a WP_Error object. |
||
283 | */ |
||
284 | public function process_refund( $order_id, $amount = null, $reason = '' ) { |
||
287 | |||
288 | /** |
||
289 | * Validate frontend fields. |
||
290 | * |
||
291 | * Validate payment fields on the frontend. |
||
292 | */ |
||
293 | public function validate_fields() { return true; } |
||
294 | |||
295 | /** |
||
296 | * If There are no payment fields show the description if set. |
||
297 | * Override this in your gateway if you have some. |
||
298 | */ |
||
299 | public function payment_fields() { |
||
309 | |||
310 | /** |
||
311 | * Check if a gateway supports a given feature. |
||
312 | * |
||
313 | * Gateways should override this to declare support (or lack of support) for a feature. |
||
314 | * For backward compatibility, gateways support 'products' by default, but nothing else. |
||
315 | * |
||
316 | * @param string $feature string The name of a feature to test support for. |
||
317 | * @return bool True if the gateway supports the feature, false otherwise. |
||
318 | * @since 1.5.7 |
||
319 | */ |
||
320 | public function supports( $feature ) { |
||
323 | |||
324 | /** |
||
325 | * Core credit card form which gateways can used if needed. |
||
326 | * |
||
327 | * @param array $args |
||
328 | */ |
||
329 | public function credit_card_form( $args = array(), $fields = array() ) { |
||
368 | } |
||
369 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.