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 | * 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 public |
||
| 32 | * @return Give_Sequential_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() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Set serialize donation number as donation title. |
||
| 59 | * Note: only for internal use |
||
| 60 | * |
||
| 61 | * @since 2.1.0 |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @param int $donation_id |
||
| 65 | * @param WP_Post $post |
||
| 66 | * @param bool $existing_donation_updated |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | public function __save_donation_title( $donation_id, $post, $existing_donation_updated ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set donation number |
||
| 113 | * Note: only for internal use |
||
| 114 | * |
||
| 115 | * @since 2.1.0 |
||
| 116 | * @access public |
||
| 117 | * |
||
| 118 | * @param int $donation_id |
||
| 119 | * |
||
| 120 | * @return int |
||
| 121 | */ |
||
| 122 | public function __set_donation_number( $donation_id ) { |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Remove sequential donation data |
||
| 144 | * Note: only internal use. |
||
| 145 | * |
||
| 146 | * @since 2.1.0 |
||
| 147 | * @access public |
||
| 148 | * |
||
| 149 | * @param $donation_id |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function __remove_serial_number( $donation_id ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set number padding in serial code. |
||
| 159 | * |
||
| 160 | * @since |
||
| 161 | * @access private |
||
| 162 | * |
||
| 163 | * @param $serial_number |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | private function __set_number_padding( $serial_number ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get donation number serial code |
||
| 177 | * |
||
| 178 | * @since 2.1.0 |
||
| 179 | * @access public |
||
| 180 | * |
||
| 181 | * @param int|Give_Payment $donation |
||
| 182 | * @param array $args |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function get_serial_code( $donation, $args = array() ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get serial number |
||
| 224 | * |
||
| 225 | * @since 2.1.0 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param int $donation_id_or_serial_code |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function get_serial_number( $donation_id_or_serial_code ) { |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Get donation id with donation number or serial code |
||
| 243 | * |
||
| 244 | * @since 2.1.0 |
||
| 245 | * @access public |
||
| 246 | * |
||
| 247 | * @param string $donation_number_or_serial_code |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function get_donation_id( $donation_number_or_serial_code ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get maximum donation number |
||
| 276 | * |
||
| 277 | * @since 2.1.0 |
||
| 278 | * @access public |
||
| 279 | * |
||
| 280 | * @return int |
||
| 281 | */ |
||
| 282 | public function get_max_number() { |
||
| 297 | } |
||
| 298 |