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 |
||
| 8 | class Group_Field { |
||
| 9 | /** |
||
| 10 | * Unique group identificator. Generated randomly. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $group_id; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Sanitized group name. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Group label, used during rendering. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $label; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Group label underscore template. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $label_template; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Group fields. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $fields = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * List of registered unique field names |
||
| 46 | * |
||
| 47 | * @see verify_unique_field_name() |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $registered_field_names = array(); |
||
|
1 ignored issue
–
show
|
|||
| 51 | |||
| 52 | /** |
||
| 53 | * Create a group field with the specified name and label. |
||
| 54 | * |
||
| 55 | * @param string $name |
||
| 56 | * @param string $label |
||
| 57 | * @param array $fields |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function __construct( $name, $label, $fields ) { |
|
| 60 | $this->set_name( $name ); |
||
| 61 | $this->set_label( $label ); |
||
| 62 | $this->add_fields( $fields ); |
||
| 63 | |||
| 64 | // Pick random ID |
||
| 65 | $random_string = md5( mt_rand() . $this->get_name() . $this->get_label() ); |
||
| 66 | $random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough |
||
| 67 | $this->group_id = 'carbon-group-' . $random_string; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Add a group of fields. |
||
| 72 | * |
||
| 73 | * @param array $fields |
||
| 74 | */ |
||
| 75 | public function add_fields( $fields ) { |
||
| 76 | foreach ( $fields as $field ) { |
||
| 77 | if ( ! is_a( $field, __NAMESPACE__ . '\\Field' ) ) { |
||
| 78 | Incorrect_Syntax_Exception::raise( 'Object must be of type ' . __NAMESPACE__ . '\\Field' ); |
||
| 79 | } |
||
| 80 | |||
| 81 | // verify name validity |
||
| 82 | if ( preg_match( '~_\d+~', $field->get_name() ) ) { |
||
| 83 | Incorrect_Syntax_Exception::raise( 'Subfield names cannot contain underscore followed by a digit(s). Replace "' . ltrim( $field->get_name(), '_' ) . '" with "' . ltrim( preg_replace( '~_+(\d+)~', '$1', $field->get_name() ), '_' ) . '"' ); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->verify_unique_field_name( $field->get_name() ); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->fields = array_merge( $this->fields, $fields ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Fields attribute getter. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function get_fields() { |
||
| 98 | return $this->fields; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Return the names of all fields. |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | public function get_field_names() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 118 | * This data will be available in the Underscore template and the Backbone Model. |
||
| 119 | * |
||
| 120 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function to_json( $load ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Group ID attribute getter. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function get_group_id() { |
||
| 153 | return $this->group_id; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Set the group label. |
||
| 158 | * |
||
| 159 | * @param string $label If null, the label will be generated from the group name |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function set_label( $label ) { |
|
|
1 ignored issue
–
show
|
|||
| 162 | // Try to guess field label from it's name |
||
| 163 | if ( is_null( $label ) ) { |
||
| 164 | // remove the leading underscore(if it's there) |
||
| 165 | $label = preg_replace( '~^_~', '', $this->name ); |
||
| 166 | |||
| 167 | // remove the leading "crb_"(if it's there) |
||
| 168 | $label = preg_replace( '~^crb_~', '', $label ); |
||
| 169 | |||
| 170 | // split the name into words and make them capitalized |
||
| 171 | $label = ucwords( str_replace( '_', ' ', $label ) ); |
||
| 172 | } |
||
| 173 | |||
| 174 | $this->label = $label; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Label attribute getter. |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function get_label() { |
||
| 183 | return $this->label; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set the Underscore label template. |
||
| 188 | * |
||
| 189 | * @param string $template |
||
| 190 | */ |
||
| 191 | public function set_label_template( $template ) { |
||
| 192 | $this->label_template = $template; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set the Underscore label template. |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function get_label_template() { |
||
| 201 | return $this->label_template; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Print the label Underscore template. |
||
| 206 | */ |
||
| 207 | public function template_label() { |
||
| 208 | echo $this->label_template; |
||
|
1 ignored issue
–
show
|
|||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the group name. |
||
| 213 | * |
||
| 214 | * @param string $name Group name, either sanitized or not |
||
| 215 | */ |
||
| 216 | public function set_name( $name ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return the group name. |
||
| 229 | * |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function get_name() { |
||
| 233 | return $this->name; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Assign a DataStore instance for all group fields. |
||
| 238 | * |
||
| 239 | * @param object $store |
||
| 240 | */ |
||
| 241 | public function set_datastore( Datastore_Interface $store ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set a prefix for all group fields. |
||
| 251 | * |
||
| 252 | * @param string $prefix |
||
| 253 | */ |
||
| 254 | public function set_prefix( $prefix ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Perform checks whether there is a field registered with the name $name. |
||
| 262 | * If not, the field name is recorded. |
||
| 263 | * |
||
| 264 | * @param string $name |
||
| 265 | */ |
||
| 266 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
|
1 ignored issue
–
show
|
|||
| 267 | if ( in_array( $name, $this->registered_field_names ) ) { |
||
| 268 | Incorrect_Syntax_Exception::raise( 'Field name "' . $name . '" already registered' ); |
||
| 273 | } |
||
| 274 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.