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 | class WC_Order_Item_Fee extends WC_Order_Item { |
||
15 | |||
16 | /** |
||
17 | * Data properties of this order item object. |
||
18 | * @since 2.6.0 |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $_data = array( |
||
22 | 'order_id' => 0, |
||
23 | 'order_item_id' => 0, |
||
24 | 'name' => '', |
||
25 | 'tax_class' => '', |
||
26 | 'tax_status' => 'taxable', |
||
27 | 'total' => '', |
||
28 | 'total_tax' => '', |
||
29 | 'taxes' => array( |
||
30 | 'total' => array() |
||
31 | ) |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * offsetGet for ArrayAccess/Backwards compatibility. |
||
36 | * @deprecated Add deprecation notices in future release. |
||
37 | * @param string $offset |
||
38 | * @return mixed |
||
39 | */ |
||
40 | public function offsetGet( $offset ) { |
||
50 | |||
51 | /** |
||
52 | * offsetExists for ArrayAccess |
||
53 | * @param string $offset |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function offsetExists( $offset ) { |
||
62 | |||
63 | /** |
||
64 | * Read/populate data properties specific to this order item. |
||
65 | */ |
||
66 | public function read( $id ) { |
||
76 | |||
77 | /** |
||
78 | * Save properties specific to this order item. |
||
79 | * @return int Item ID |
||
80 | */ |
||
81 | public function save() { |
||
93 | |||
94 | /* |
||
95 | |-------------------------------------------------------------------------- |
||
96 | | Setters |
||
97 | |-------------------------------------------------------------------------- |
||
98 | */ |
||
99 | |||
100 | /** |
||
101 | * Set tax class. |
||
102 | * @param string $value |
||
103 | */ |
||
104 | public function set_tax_class( $value ) { |
||
107 | |||
108 | /** |
||
109 | * Set tax_status. |
||
110 | * @param string $value |
||
111 | */ |
||
112 | public function set_tax_status( $value ) { |
||
119 | |||
120 | /** |
||
121 | * Set total. |
||
122 | * @param string $value |
||
123 | */ |
||
124 | public function set_total( $value ) { |
||
127 | |||
128 | /** |
||
129 | * Set total tax. |
||
130 | * @param string $value |
||
131 | */ |
||
132 | public function set_total_tax( $value ) { |
||
135 | |||
136 | /** |
||
137 | * Set taxes. |
||
138 | * |
||
139 | * This is an array of tax ID keys with total amount values. |
||
140 | * @param array $raw_tax_data |
||
141 | */ |
||
142 | View Code Duplication | public function set_taxes( $raw_tax_data ) { |
|
152 | |||
153 | /* |
||
154 | |-------------------------------------------------------------------------- |
||
155 | | Getters |
||
156 | |-------------------------------------------------------------------------- |
||
157 | */ |
||
158 | |||
159 | /** |
||
160 | * Get order item name. |
||
161 | * @return string |
||
162 | */ |
||
163 | public function get_name() { |
||
166 | |||
167 | /** |
||
168 | * Get order item type. |
||
169 | * @return string |
||
170 | */ |
||
171 | public function get_type() { |
||
174 | |||
175 | /** |
||
176 | * Get tax class. |
||
177 | * @return string |
||
178 | */ |
||
179 | public function get_tax_class() { |
||
182 | |||
183 | /** |
||
184 | * Get tax status. |
||
185 | * @return string |
||
186 | */ |
||
187 | public function get_tax_status() { |
||
190 | |||
191 | /** |
||
192 | * Get total fee. |
||
193 | * @return string |
||
194 | */ |
||
195 | public function get_total() { |
||
198 | |||
199 | /** |
||
200 | * Get total tax. |
||
201 | * @return string |
||
202 | */ |
||
203 | public function get_total_tax() { |
||
206 | |||
207 | /** |
||
208 | * Get fee taxes. |
||
209 | * @return array |
||
210 | */ |
||
211 | public function get_taxes() { |
||
214 | } |
||
215 |
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.