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() { |
||
| 71 | $options = array(); |
||
| 72 | foreach ( $this->option_collections as $collection ) { |
||
| 73 | $collection_items = array(); |
||
| 74 | if ( is_callable( $collection ) ) { |
||
| 75 | $collection_items = call_user_func( $collection ); |
||
| 76 | if ( ! is_array( $collection_items ) ) { |
||
| 77 | continue; |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | $collection_items = $collection; |
||
| 81 | } |
||
| 82 | if ( $this->is_indexed_array( $options ) && $this->is_indexed_array( $collection_items ) ) { |
||
| 83 | $options = array_merge( $options, $collection_items ); |
||
| 84 | } else { |
||
| 85 | $options = array_replace( $options, $collection_items ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | return $options; |
||
| 90 | } |
||
| 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 | * @return array |
||
| 117 | */ |
||
| 118 | protected function parse_options( $options, $stringify_value = false ) { |
||
| 119 | $parsed = array(); |
||
| 120 | |||
| 121 | if ( is_callable( $options ) ) { |
||
| 122 | $options = call_user_func( $options ); |
||
| 123 | } |
||
| 124 | |||
| 125 | foreach ( $options as $key => $value ) { |
||
| 126 | $parsed[] = array( |
||
| 127 | 'value' => $stringify_value ? strval( $key ) : $key, |
||
| 128 | 'label' => strval( $value ), |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $parsed; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get an array of all values that are both in the passed array and the predefined list of options |
||
| 137 | * |
||
| 138 | * @param array $values |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | protected function get_values_from_options( $values ) { |
||
| 146 | } |
||
| 147 |