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 |
||
| 2 | class WPMC_MailChimp_Groups { |
||
| 3 | |||
| 4 | /** |
||
| 5 | * @var string ID of the current list group |
||
| 6 | * @since 1.1 |
||
| 7 | */ |
||
| 8 | protected $group_id; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var array Group values |
||
| 12 | * @since 1.1 |
||
| 13 | */ |
||
| 14 | protected $options; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var int Current popup (post) ID |
||
| 18 | * @since 1.1 |
||
| 19 | */ |
||
| 20 | protected $post_id; |
||
| 21 | |||
| 22 | public function __construct( $group_id = '', $options = array(), $post_id = 0 ) { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get the value. |
||
| 36 | * |
||
| 37 | * Get the value for a specific group. |
||
| 38 | * |
||
| 39 | * @since 1.1.0 |
||
| 40 | * @param integer $group The group ID |
||
| 41 | * @param boolean $post_id Post ID |
||
| 42 | * @return array Array of values for this group (empty array if no values) |
||
| 43 | */ |
||
| 44 | public function get_value( $group, $post_id = false ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Display group options as checkboxes. |
||
| 64 | * |
||
| 65 | * @since 1.1.0 |
||
| 66 | */ |
||
| 67 | public function show_group_type_checkboxes() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Display group options as radio. |
||
| 92 | * |
||
| 93 | * @since 1.1.0 |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function show_group_type_radio() { |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Display group options as dropdown. |
||
| 121 | * |
||
| 122 | * @since 1.1.0 |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function show_group_type_dropdown() { |
|
| 145 | |||
| 146 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: