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 |
||
16 | class WC_Order_Refund extends WC_Abstract_Order { |
||
17 | |||
18 | /** @public string Order type */ |
||
19 | public $order_type = 'refund'; |
||
20 | |||
21 | /** @var string Date */ |
||
22 | public $date; |
||
23 | |||
24 | /** @var string Refund reason */ |
||
25 | public $reason; |
||
26 | |||
27 | /** |
||
28 | * Data array, with defaults. |
||
29 | * |
||
30 | * @todo when migrating to custom tables, these will be columns |
||
31 | * @since 2.6.0 |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $_refund_data = array( |
||
35 | 'refund_amount' => '', |
||
36 | 'refund_reason' => '', |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * Get the refund if ID is passed, otherwise the refund is new and empty. |
||
41 | * @param int|object|WC_Order_Refund $refund Refund to init. |
||
42 | */ |
||
43 | View Code Duplication | public function __construct( $refund = 0 ) { |
|
52 | |||
53 | /** |
||
54 | * Get refunded amount. |
||
55 | * |
||
56 | * @since 2.2 |
||
57 | * @return int|float |
||
58 | */ |
||
59 | public function get_refund_amount() { |
||
62 | |||
63 | /** |
||
64 | * Get formatted refunded amount. |
||
65 | * |
||
66 | * @since 2.4 |
||
67 | * @return string |
||
68 | */ |
||
69 | public function get_formatted_refund_amount() { |
||
72 | |||
73 | /** |
||
74 | * Get refunded amount. |
||
75 | * |
||
76 | * @since 2.2 |
||
77 | * @return int|float |
||
78 | */ |
||
79 | public function get_refund_reason() { |
||
82 | |||
83 | /** |
||
84 | * Gets an refund from the database. |
||
85 | * @deprecated 2.6 |
||
86 | * @param int $id (default: 0). |
||
87 | * @return bool |
||
88 | */ |
||
89 | View Code Duplication | public function get_refund( $id = 0 ) { |
|
100 | } |
||
101 |
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.