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_Seq_Donation_Number { |
||
|
|
|||
| 8 | /** |
||
| 9 | * Instance. |
||
| 10 | * |
||
| 11 | * @since 2.1.0 |
||
| 12 | * @access private |
||
| 13 | * @var |
||
| 14 | */ |
||
| 15 | static private $instance; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Singleton pattern. |
||
| 19 | * |
||
| 20 | * @since 2.1.0 |
||
| 21 | * @access private |
||
| 22 | */ |
||
| 23 | private function __construct() { |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Get instance. |
||
| 29 | * |
||
| 30 | * @since 2.1.0 |
||
| 31 | * @access static |
||
| 32 | * @return Give_Seq_Donation_Number |
||
| 33 | */ |
||
| 34 | View Code Duplication | public static function get_instance() { |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Initialize the plugin, bailing if any required conditions are not met, |
||
| 46 | * including minimum WooCommerce version |
||
| 47 | * |
||
| 48 | * @since 2.1.0 |
||
| 49 | */ |
||
| 50 | public function init() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set serialize donation number as donation title. |
||
| 58 | * |
||
| 59 | * @since 2.1.0 |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @param int $donation_id |
||
| 63 | * @param WP_Post $donation_post_data |
||
| 64 | * @param bool $existing_donation_updated |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function __save_donation_title( $donation_id, $donation_post_data, $existing_donation_updated ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Set donation number |
||
| 109 | * |
||
| 110 | * @since 2.1.0 |
||
| 111 | * @access public |
||
| 112 | * |
||
| 113 | * @param int $donation_id |
||
| 114 | * |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function set_donation_number( $donation_id ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set number padding in serial code. |
||
| 125 | * |
||
| 126 | * @since |
||
| 127 | * @access private |
||
| 128 | * |
||
| 129 | * @param $serial_code |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | private function __set_number_padding( $serial_code ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get donation number serial code |
||
| 146 | * |
||
| 147 | * @since 2.1.0 |
||
| 148 | * @access public |
||
| 149 | * |
||
| 150 | * @param int|Give_Payment $donation |
||
| 151 | * @param array $args |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function get_serial_code( $donation, $args = array() ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get serial number |
||
| 190 | * |
||
| 191 | * @since 2.1.0 |
||
| 192 | * @access public |
||
| 193 | * |
||
| 194 | * @param int $donation_id |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function get_serial_number( $donation_id ) { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Get donation id with donation number or serial code |
||
| 205 | * |
||
| 206 | * @since 2.1.0 |
||
| 207 | * @access public |
||
| 208 | * |
||
| 209 | * @param string $donation_number_or_serial_code\ |
||
| 210 | */ |
||
| 211 | public function get_donation_id( $donation_number_or_serial_code ) {} |
||
| 212 | } |
||
| 213 | |||
| 216 |