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 |
||
| 7 | class Give_Sequential_Donation_Number { |
||
|
|
|||
| 8 | /** |
||
| 9 | * Instance. |
||
| 10 | * |
||
| 11 | * @since 2.1.0 |
||
| 12 | * @access private |
||
| 13 | * @var |
||
| 14 | */ |
||
| 15 | static private $instance; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Donation tile prefix |
||
| 19 | * |
||
| 20 | * @since 2.1.0 |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $donation_title_prefix = 'give-donation-'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Singleton pattern. |
||
| 27 | * |
||
| 28 | * @since 2.1.0 |
||
| 29 | * @access private |
||
| 30 | */ |
||
| 31 | private function __construct() { |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Get instance. |
||
| 37 | * |
||
| 38 | * @since 2.1.0 |
||
| 39 | * @access public |
||
| 40 | * @return Give_Sequential_Donation_Number |
||
| 41 | */ |
||
| 42 | View Code Duplication | public static function get_instance() { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Initialize the plugin, bailing if any required conditions are not met, |
||
| 54 | * including minimum WooCommerce version |
||
| 55 | * |
||
| 56 | * @since 2.1.0 |
||
| 57 | */ |
||
| 58 | public function init() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Set serialize donation number as donation title. |
||
| 65 | * Note: only for internal use |
||
| 66 | * |
||
| 67 | * @since 2.1.0 |
||
| 68 | * @access public |
||
| 69 | * |
||
| 70 | * @param int $donation_id |
||
| 71 | * @param WP_Post $post |
||
| 72 | * @param bool $existing_donation_updated |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function __save_donation_title( $donation_id, $post, $existing_donation_updated ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set donation number |
||
| 139 | * Note: only for internal use |
||
| 140 | * |
||
| 141 | * @since 2.1.0 |
||
| 142 | * @access public |
||
| 143 | * |
||
| 144 | * @param int $donation_id |
||
| 145 | * |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | public function __set_donation_number( $donation_id ) { |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Remove sequential donation data |
||
| 181 | * Note: only internal use. |
||
| 182 | * |
||
| 183 | * @since 2.1.0 |
||
| 184 | * @access public |
||
| 185 | * |
||
| 186 | * @param $donation_id |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function __remove_serial_number( $donation_id ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set number padding in serial code. |
||
| 196 | * |
||
| 197 | * @since |
||
| 198 | * @access public |
||
| 199 | * |
||
| 200 | * @param $serial_number |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function set_number_padding( $serial_number ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get donation number serial code |
||
| 214 | * |
||
| 215 | * @since 2.1.0 |
||
| 216 | * @access public |
||
| 217 | * |
||
| 218 | * @param int|Give_Payment|WP_Post $donation |
||
| 219 | * @param array $args |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function get_serial_code( $donation, $args = array() ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get serial number |
||
| 265 | * |
||
| 266 | * @since 2.1.0 |
||
| 267 | * @access public |
||
| 268 | * |
||
| 269 | * @param int $donation_id_or_serial_code |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function get_serial_number( $donation_id_or_serial_code ) { |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Get donation id with donation number or serial code |
||
| 284 | * |
||
| 285 | * @since 2.1.0 |
||
| 286 | * @access public |
||
| 287 | * |
||
| 288 | * @param string $donation_number_or_serial_code |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function get_donation_id( $donation_number_or_serial_code ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get maximum donation number |
||
| 317 | * |
||
| 318 | * @since 2.1.0 |
||
| 319 | * @access public |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | public function get_max_number() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get maximum donation id |
||
| 341 | * |
||
| 342 | * @since 2.1.0 |
||
| 343 | * @access public |
||
| 344 | * |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | public function get_max_donation_id() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get maximum donation number |
||
| 370 | * |
||
| 371 | * @since 2.1.0 |
||
| 372 | * @access public |
||
| 373 | * |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | public function get_next_number() { |
||
| 388 | } |
||
| 389 |