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() { |
||
| 107 | $options = $this->parse_options( $this->get_options() ); |
||
| 108 | |||
| 109 | $values = array_map( |
||
| 110 | function( $value ) { |
||
| 111 | return isset( $value['options'] ) ? wp_list_pluck( $value['options'], 'value' ) : $value['value']; |
||
| 112 | }, |
||
| 113 | $options |
||
| 114 | ); |
||
| 115 | |||
| 116 | $values = new \RecursiveIteratorIterator( new \RecursiveArrayIterator( $values ) ); |
||
| 117 | $extracted_values = array(); |
||
| 118 | |||
| 119 | foreach ( $values as $key => $value ) { |
||
| 120 | $extracted_values[] = $value; |
||
| 121 | } |
||
| 122 | |||
| 123 | return array_values( $extracted_values ); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
||
| 128 | * Will also work with a callable that returns an array. |
||
| 129 | * |
||
| 130 | * @param array|callable $options |
||
| 131 | * @param boolean $stringify_value |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | protected function parse_options( $options, $stringify_value = false ) { |
||
| 135 | $parsed = array(); |
||
| 136 | |||
| 137 | if ( is_callable( $options ) ) { |
||
| 138 | $options = call_user_func( $options ); |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach ( $options as $key => $value ) { |
||
| 142 | if ( is_array( $value ) ) { |
||
| 143 | $parsed[] = array( |
||
| 144 | 'options' => $this->parse_options( $value, $stringify_value ), |
||
| 145 | 'label' => strval( $key ), |
||
| 146 | ); |
||
| 147 | } else { |
||
| 148 | $parsed[] = array( |
||
| 149 | 'value' => $stringify_value ? strval( $key ) : $key, |
||
| 150 | 'label' => strval( $value ), |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $parsed; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get an array of all values that are both in the passed array and the predefined list of options |
||
| 160 | * |
||
| 161 | * @param array $values |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected function get_values_from_options( $values ) { |
||
| 169 | } |
||
| 170 |