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 |
||
11 | class Set_Field extends Predefined_Options_Field { |
||
12 | /** |
||
13 | * The options limit. |
||
14 | * |
||
15 | * @var int |
||
16 | */ |
||
17 | protected $limit_options = 0; |
||
18 | |||
19 | /** |
||
20 | * Default field value |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $default_value = array(); |
||
25 | |||
26 | /** |
||
27 | * Create a field from a certain type with the specified label. |
||
28 | * @param string $name Field name |
||
29 | * @param string $label Field label |
||
30 | */ |
||
31 | protected function __construct( $name, $label ) { |
||
35 | |||
36 | /** |
||
37 | * Set the number of the options to be displayed at the initial field display. |
||
38 | * |
||
39 | * @param int $limit |
||
40 | */ |
||
41 | public function limit_options( $limit ) { |
||
45 | |||
46 | /** |
||
47 | * Load the field value from an input array based on it's name |
||
48 | * |
||
49 | * @param array $input (optional) Array of field names and values. Defaults to $_POST |
||
50 | **/ |
||
51 | public function set_value_from_input( $input = null ) { |
||
66 | |||
67 | /** |
||
68 | * Returns an array that holds the field data, suitable for JSON representation. |
||
69 | * This data will be available in the Underscore template and the Backbone Model. |
||
70 | * |
||
71 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
72 | * @return array |
||
73 | */ |
||
74 | View Code Duplication | public function to_json( $load ) { |
|
84 | |||
85 | /** |
||
86 | * The Underscore template of this field. |
||
87 | */ |
||
88 | public function template() { |
||
117 | } |
||
118 |