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 |
||
23 | class Give_Tools_Delete_Test_Transactions extends Give_Batch_Export { |
||
24 | |||
25 | /** |
||
26 | * Our export type. Used for export-type specific filters/actions |
||
27 | * @var string |
||
28 | * @since 1.5 |
||
29 | */ |
||
30 | public $export_type = ''; |
||
31 | |||
32 | /** |
||
33 | * Allows for a non-form batch processing to be run. |
||
34 | * @since 1.5 |
||
35 | * @var boolean |
||
36 | */ |
||
37 | public $is_void = true; |
||
38 | |||
39 | /** |
||
40 | * Sets the number of items to pull on each step |
||
41 | * @since 1.5 |
||
42 | * @var integer |
||
43 | */ |
||
44 | public $per_step = 30; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | */ |
||
49 | public function __construct( $_step = 1 ) { |
||
54 | |||
55 | /** |
||
56 | * Get the Export Data |
||
57 | * |
||
58 | * @access public |
||
59 | * @since 1.5 |
||
60 | * @global object $wpdb Used to query the database using the WordPress Database API |
||
61 | * |
||
62 | * @return array|bool $data The data for the CSV file |
||
63 | */ |
||
64 | public function get_data() { |
||
85 | |||
86 | /** |
||
87 | * Return the calculated completion percentage |
||
88 | * |
||
89 | * @since 1.5 |
||
90 | * @return int |
||
91 | */ |
||
92 | public function get_percentage_complete() { |
||
109 | |||
110 | /** |
||
111 | * Set the properties specific to the payments export |
||
112 | * |
||
113 | * @since 1.5 |
||
114 | * |
||
115 | * @param array $request The Form Data passed into the batch processing |
||
116 | */ |
||
117 | public function set_properties( $request ) { |
||
119 | |||
120 | /** |
||
121 | * Process a step |
||
122 | * |
||
123 | * @since 1.5 |
||
124 | * @return bool |
||
125 | */ |
||
126 | View Code Duplication | public function process_step() { |
|
155 | |||
156 | /** |
||
157 | * Headers |
||
158 | */ |
||
159 | public function headers() { |
||
162 | |||
163 | /** |
||
164 | * Perform the export |
||
165 | * |
||
166 | * @access public |
||
167 | * @since 1.5 |
||
168 | * @return void |
||
169 | */ |
||
170 | public function export() { |
||
177 | |||
178 | /** |
||
179 | * Pre Fetch |
||
180 | */ |
||
181 | public function pre_fetch() { |
||
218 | |||
219 | /** |
||
220 | * Given a key, get the information from the Database Directly |
||
221 | * |
||
222 | * @since 1.5 |
||
223 | * |
||
224 | * @param string $key The option_name |
||
225 | * |
||
226 | * @return mixed Returns the data from the database |
||
227 | */ |
||
228 | View Code Duplication | private function get_stored_data( $key ) { |
|
243 | |||
244 | /** |
||
245 | * Give a key, store the value |
||
246 | * |
||
247 | * @since 1.5 |
||
248 | * |
||
249 | * @param string $key The option_name |
||
250 | * @param mixed $value The value to store |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | View Code Duplication | private function store_data( $key, $value ) { |
|
273 | |||
274 | /** |
||
275 | * Delete an option |
||
276 | * |
||
277 | * @since 1.5 |
||
278 | * |
||
279 | * @param string $key The option_name to delete |
||
280 | * |
||
281 | * @return void |
||
282 | */ |
||
283 | private function delete_data( $key ) { |
||
287 | |||
288 | } |
||
289 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.