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 |
||
29 | class Give_API_Keys_Table extends WP_List_Table { |
||
30 | |||
31 | /** |
||
32 | * @var int Number of items per page |
||
33 | * @since 1.1 |
||
34 | */ |
||
35 | public $per_page = 30; |
||
36 | |||
37 | /** |
||
38 | * @var object Query results |
||
39 | * @since 1.1 |
||
40 | */ |
||
41 | private $keys; |
||
42 | |||
43 | /** |
||
44 | * Get things started |
||
45 | * |
||
46 | * @since 1.1 |
||
47 | * @see WP_List_Table::__construct() |
||
48 | * |
||
49 | * @global $status |
||
50 | * @global $page |
||
51 | */ |
||
52 | public function __construct() { |
||
64 | |||
65 | /** |
||
66 | * This function renders most of the columns in the list table. |
||
67 | * |
||
68 | * @access public |
||
69 | * @since 1.1 |
||
70 | * |
||
71 | * @param array $item Contains all the data of the keys |
||
72 | * @param string $column_name The name of the column |
||
73 | * |
||
74 | * @return string Column Name |
||
75 | */ |
||
76 | public function column_default( $item, $column_name ) { |
||
79 | |||
80 | /** |
||
81 | * Displays the public key rows |
||
82 | * |
||
83 | * @access public |
||
84 | * @since 1.1 |
||
85 | * |
||
86 | * @param array $item Contains all the data of the keys |
||
87 | * |
||
88 | * @return string Column Name |
||
89 | */ |
||
90 | public function column_key( $item ) { |
||
93 | |||
94 | /** |
||
95 | * Displays the token rows |
||
96 | * |
||
97 | * @access public |
||
98 | * @since 1.1 |
||
99 | * |
||
100 | * @param array $item Contains all the data of the keys |
||
101 | * |
||
102 | * @return string Column Name |
||
103 | */ |
||
104 | public function column_token( $item ) { |
||
107 | |||
108 | /** |
||
109 | * Displays the secret key rows |
||
110 | * |
||
111 | * @access public |
||
112 | * @since 1.1 |
||
113 | * |
||
114 | * @param array $item Contains all the data of the keys |
||
115 | * |
||
116 | * @return string Column Name |
||
117 | */ |
||
118 | public function column_secret( $item ) { |
||
121 | |||
122 | /** |
||
123 | * Renders the column for the user field |
||
124 | * |
||
125 | * @access public |
||
126 | * @since 1.1 |
||
127 | * @return string |
||
128 | */ |
||
129 | public function column_user( $item ) { |
||
170 | |||
171 | /** |
||
172 | * Gets the name of the primary column. |
||
173 | * |
||
174 | * @since 1.5 |
||
175 | * @access protected |
||
176 | * |
||
177 | * @return string Name of the primary column. |
||
178 | */ |
||
179 | protected function get_primary_column_name() { |
||
182 | |||
183 | /** |
||
184 | * Retrieve the table columns |
||
185 | * |
||
186 | * @access public |
||
187 | * @since 1.1 |
||
188 | * @return array $columns Array of all the list table columns |
||
189 | */ |
||
190 | public function get_columns() { |
||
200 | |||
201 | /** |
||
202 | * Generate the table navigation above or below the table |
||
203 | * |
||
204 | * @since 3.1.0 |
||
205 | * @access protected |
||
206 | * |
||
207 | * @param string $which |
||
208 | */ |
||
209 | View Code Duplication | protected function display_tablenav( $which ) { |
|
229 | |||
230 | /** |
||
231 | * Display the key generation form |
||
232 | * |
||
233 | * @access public |
||
234 | * @since 1.1 |
||
235 | * |
||
236 | * @param string $which |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | function bulk_actions( $which = '' ) { |
||
266 | |||
267 | /** |
||
268 | * Retrieve the current page number |
||
269 | * |
||
270 | * @access public |
||
271 | * @since 1.1 |
||
272 | * @return int Current page number |
||
273 | */ |
||
274 | public function get_paged() { |
||
277 | |||
278 | /** |
||
279 | * Performs the key query |
||
280 | * |
||
281 | * @access public |
||
282 | * @since 1.1 |
||
283 | * @return array |
||
284 | */ |
||
285 | public function query() { |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Retrieve count of total users with keys |
||
309 | * |
||
310 | * @access public |
||
311 | * @since 1.1 |
||
312 | * @return int |
||
313 | */ |
||
314 | public function total_items() { |
||
333 | |||
334 | /** |
||
335 | * Setup the final data for the table |
||
336 | * |
||
337 | * @access public |
||
338 | * @since 1.1 |
||
339 | * @return void |
||
340 | */ |
||
341 | public function prepare_items() { |
||
361 | } |
||
362 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.