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 |
||
| 17 | class WPBO_Popup { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * ID of the popup to work with |
||
| 21 | * |
||
| 22 | * @var int |
||
| 23 | * @since 2.0 |
||
| 24 | */ |
||
| 25 | private $popup_id; |
||
| 26 | |||
| 27 | private $settings; |
||
| 28 | |||
| 29 | public function __construct( $popup_id = 0 ) { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check if a popup exists |
||
| 40 | * |
||
| 41 | * @since 2.0 |
||
| 42 | * |
||
| 43 | * @param $popup_id |
||
| 44 | * |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public static function popup_exists( $popup_id ) { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Check if the popup is currently active |
||
| 57 | * |
||
| 58 | * @since 2.0 |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | protected function is_popup_active() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the popup settings |
||
| 67 | * |
||
| 68 | * @since 2.0 |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | private function get_settings() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get popup option |
||
| 83 | * |
||
| 84 | * @since 2.0 |
||
| 85 | * |
||
| 86 | * @param string $option Popup option to get the value for |
||
| 87 | * @param mixed $default Default option to return if the value doesn't exist |
||
| 88 | * |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | public function option( $option, $default = '' ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the popup template |
||
| 97 | * |
||
| 98 | * @since 2.0 |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function get_template() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get a popup markup. |
||
| 134 | * |
||
| 135 | * Retrieve the markup for a specific popup. Check if the popup |
||
| 136 | * was customized first, otherwise just load the default HTML file. |
||
| 137 | * |
||
| 138 | * @since 1.0.0 |
||
| 139 | * @return string HTML markup of the popup to display |
||
| 140 | */ |
||
| 141 | public function get_markup() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get popup return URL |
||
| 195 | * |
||
| 196 | * @since 2.0 |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function get_return_url() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the rendered popup HTML markup |
||
| 223 | * |
||
| 224 | * @since 1.0.0 |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | private function get_popup() { |
||
| 266 | |||
| 267 | public function popup() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the number of impressions for this popup |
||
| 273 | * |
||
| 274 | * @since 2.0 |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | public function get_impressions() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Record popup impression. |
||
| 283 | * |
||
| 284 | * @since 1.0.0 |
||
| 285 | * @return int|WP_Error |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function new_impression() { |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Log a new popup conversion |
||
| 304 | * |
||
| 305 | * @since 2.0 |
||
| 306 | * @return int|WP_Error |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function new_conversion() { |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Clean the post. |
||
| 324 | * |
||
| 325 | * Filter the post data and only keep |
||
| 326 | * values that are actually supported |
||
| 327 | * by the API. |
||
| 328 | * |
||
| 329 | * @since 1.0.0 |
||
| 330 | * |
||
| 331 | * @param array $data Data to sanitize |
||
| 332 | * |
||
| 333 | * @return array Clean list of merge fields |
||
| 334 | */ |
||
| 335 | protected function get_clean_fields( $data = array() ) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Trigger form submission. |
||
| 363 | * |
||
| 364 | * @since 1.0.0 |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | public function submit() { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Shows a confirmation alert. |
||
| 408 | * |
||
| 409 | * This is only used if the used didn't set a custom |
||
| 410 | * thank you page. |
||
| 411 | * |
||
| 412 | * @since 1.0.0 |
||
| 413 | */ |
||
| 414 | public function submission_confirmation_fallback() { ?> |
||
| 419 | |||
| 420 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.