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 |
||
| 16 | class Jetpack_EU_Cookie_Law_Widget extends WP_Widget { |
||
| 17 | /** |
||
| 18 | * EU Cookie Law cookie name. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public static $cookie_name = 'eucookielaw'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Array of two-letter country codes where GDPR applies. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | public static $gdpr_zone = array( |
||
| 30 | // European Member countries |
||
| 31 | 'AT', // Austria |
||
| 32 | 'BE', // Belgium |
||
| 33 | 'BG', // Bulgaria |
||
| 34 | 'CY', // Cyprus |
||
| 35 | 'CZ', // Czech Republic |
||
| 36 | 'DE', // Germany |
||
| 37 | 'DK', // Denmark |
||
| 38 | 'EE', // Estonia |
||
| 39 | 'ES', // Spain |
||
| 40 | 'FI', // Finland |
||
| 41 | 'FR', // France |
||
| 42 | 'GR', // Greece |
||
| 43 | 'HR', // Croatia |
||
| 44 | 'HU', // Hungary |
||
| 45 | 'IE', // Ireland |
||
| 46 | 'IT', // Italy |
||
| 47 | 'LT', // Lithuania |
||
| 48 | 'LU', // Luxembourg |
||
| 49 | 'LV', // Latvia |
||
| 50 | 'MT', // Malta |
||
| 51 | 'NL', // Netherlands |
||
| 52 | 'PL', // Poland |
||
| 53 | 'PT', // Portugal |
||
| 54 | 'RO', // Romania |
||
| 55 | 'SE', // Sweden |
||
| 56 | 'SI', // Slovenia |
||
| 57 | 'SK', // Slovakia |
||
| 58 | 'GB', // United Kingdom |
||
| 59 | // Single Market Countries that GDPR applies to |
||
| 60 | 'CH', // Switzerland |
||
| 61 | 'IS', // Iceland |
||
| 62 | 'LI', // Liechtenstein |
||
| 63 | 'NO', // Norway |
||
| 64 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Default display options. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $display_options = array( |
||
| 72 | 'all', |
||
| 73 | 'eu', |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Default hide options. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private $hide_options = array( |
||
| 82 | 'button', |
||
| 83 | 'scroll', |
||
| 84 | 'time', |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Default text options. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $text_options = array( |
||
| 93 | 'default', |
||
| 94 | 'custom', |
||
| 95 | ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Default color scheme options. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private $color_scheme_options = array( |
||
| 103 | 'default', |
||
| 104 | 'negative', |
||
| 105 | ); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Default policy URL options. |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $policy_url_options = array( |
||
| 113 | 'default', |
||
| 114 | 'custom', |
||
| 115 | ); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Constructor. |
||
| 119 | */ |
||
| 120 | function __construct() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Enqueue scripts and styles. |
||
| 139 | */ |
||
| 140 | function enqueue_frontend_scripts() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return an associative array of default values. |
||
| 156 | * |
||
| 157 | * These values are used in new widgets. |
||
| 158 | * |
||
| 159 | * @return array Default values for the widget options. |
||
| 160 | */ |
||
| 161 | public function defaults() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Front-end display of the widget. |
||
| 181 | * |
||
| 182 | * @param array $args Widget arguments. |
||
| 183 | * @param array $instance Saved values from database. |
||
| 184 | */ |
||
| 185 | public function widget( $args, $instance ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Back-end widget form. |
||
| 216 | * |
||
| 217 | * @param array $instance Previously saved values from database. |
||
| 218 | */ |
||
| 219 | public function form( $instance ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Sanitize widget form values as they are saved. |
||
| 226 | * |
||
| 227 | * @param array $new_instance Values just sent to be saved. |
||
| 228 | * @param array $old_instance Previously saved values from database. |
||
| 229 | * @return array Updated safe values to be saved. |
||
| 230 | */ |
||
| 231 | public function update( $new_instance, $old_instance ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Check if the value is allowed and not empty. |
||
| 301 | * |
||
| 302 | * @param string $value Value to check. |
||
| 303 | * @param array $allowed Array of allowed values. |
||
| 304 | * |
||
| 305 | * @return string $value if pass the check or first value from allowed values. |
||
| 306 | */ |
||
| 307 | function filter_value( $value, $allowed = array() ) { |
||
| 314 | } |
||
| 315 | |||
| 323 |