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 | */ |
||
36 | 9 | View Code Duplication | public function set_options( $options ) { |
45 | |||
46 | /** |
||
47 | * Add new options to this field. |
||
48 | * Accepts either array of data or a callback that returns the data. |
||
49 | * |
||
50 | * @param array|callable $options |
||
51 | */ |
||
52 | 13 | View Code Duplication | public function add_options( $options ) { |
53 | 13 | if ( ! is_callable( $options ) && ! is_array( $options ) ) { |
|
54 | 4 | Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>add_options()</code> method.' ); |
|
55 | return $this; |
||
56 | } |
||
57 | |||
58 | 9 | $this->option_collections[] = $options; |
|
59 | 9 | return $this; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get a populated array of options executing any callbacks in the process |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | protected function load_options() { |
||
94 | |||
95 | /** |
||
96 | * Retrieve the current options. |
||
97 | * |
||
98 | * @return array|callable $options |
||
99 | */ |
||
100 | 13 | public function get_options() { |
|
103 | |||
104 | /** |
||
105 | * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
||
106 | * Will also work with a callable that returns an array. |
||
107 | * |
||
108 | * @param array|callable $options |
||
109 | * @return array |
||
110 | */ |
||
111 | protected function parse_options( $options ) { |
||
127 | } |
||
128 |