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 | abstract class Predefined_Options_Field extends Field { |
||
12 | |||
13 | /** |
||
14 | * Stores the raw, unprocessed field options |
||
15 | * |
||
16 | * @var array(array|callable) |
||
17 | */ |
||
18 | protected $option_collections = array(); |
||
19 | |||
20 | /** |
||
21 | * Check if an array is indexed |
||
22 | * |
||
23 | * @param array $array |
||
24 | * @return boolean |
||
25 | */ |
||
26 | protected function is_indexed_array( $array ) { |
||
29 | |||
30 | /** |
||
31 | * Set the options of this field. |
||
32 | * Accepts either array of data or a callback that returns the data. |
||
33 | * |
||
34 | * @param array|callable $options |
||
35 | * @return Field $this |
||
36 | */ |
||
37 | 9 | View Code Duplication | public function set_options( $options ) { |
46 | |||
47 | /** |
||
48 | * Add new options to this field. |
||
49 | * Accepts either array of data or a callback that returns the data. |
||
50 | * |
||
51 | * @param array|callable $options |
||
52 | * @return Field $this |
||
53 | */ |
||
54 | 13 | View Code Duplication | public function add_options( $options ) { |
63 | |||
64 | /** |
||
65 | * Get a populated array of options executing any callbacks in the process |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | protected function load_options() { |
||
96 | |||
97 | /** |
||
98 | * Retrieve the current options. |
||
99 | * |
||
100 | * @return array|callable $options |
||
101 | */ |
||
102 | 13 | public function get_options() { |
|
105 | |||
106 | /** |
||
107 | * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
||
108 | * Will also work with a callable that returns an array. |
||
109 | * |
||
110 | * @param array|callable $options |
||
111 | * @return array |
||
112 | */ |
||
113 | protected function parse_options( $options ) { |
||
129 | } |
||
130 |