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 |
||
12 | abstract class Predefined_Options_Field extends Field { |
||
13 | |||
14 | /** |
||
15 | * Stores the raw, unprocessed field options |
||
16 | * |
||
17 | * @var array(array|callable) |
||
18 | */ |
||
19 | protected $option_collections = array(); |
||
20 | |||
21 | /** |
||
22 | * Check if an array is indexed |
||
23 | * |
||
24 | * @param array $array |
||
25 | * @return boolean |
||
26 | */ |
||
27 | protected function is_indexed_array( $array ) { |
||
30 | |||
31 | /** |
||
32 | * Set the options of this field. |
||
33 | * Accepts either array of data or a callback that returns the data. |
||
34 | * |
||
35 | * @param array|callable $options |
||
36 | * @return self $this |
||
37 | */ |
||
38 | 9 | View Code Duplication | public function set_options( $options ) { |
47 | |||
48 | /** |
||
49 | * Add new options to this field. |
||
50 | * Accepts either array of data or a callback that returns the data. |
||
51 | * |
||
52 | * @param array|callable $options |
||
53 | * @return self $this |
||
54 | */ |
||
55 | 13 | View Code Duplication | public function add_options( $options ) { |
64 | |||
65 | /** |
||
66 | * Get a populated array of options executing any callbacks in the process |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | protected function load_options() { |
||
91 | |||
92 | /** |
||
93 | * Retrieve the current options. |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | 13 | public function get_options() { |
|
100 | |||
101 | /** |
||
102 | * Retrieve the current options' values only. |
||
103 | * |
||
104 | * @return array $options |
||
105 | */ |
||
106 | protected function get_options_values() { |
||
110 | |||
111 | /** |
||
112 | * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
||
113 | * Will also work with a callable that returns an array. |
||
114 | * |
||
115 | * @param array|callable $options |
||
116 | * @param bool $stringify_value (optional) |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function parse_options( $options, $stringify_value = false ) { |
||
135 | |||
136 | /** |
||
137 | * Get an array of all values that are both in the passed array and the predefined list of options |
||
138 | * |
||
139 | * @param array $values |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function get_values_from_options( $values ) { |
||
147 | } |
||
148 |