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 | * Stores the raw, unprocessed field options |
||
| 14 | * |
||
| 15 | * @var array(array|callable) |
||
| 16 | **/ |
||
| 17 | protected $option_collections = array(); |
||
| 18 | |||
| 19 | protected function is_indexed_array( $array ) { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Set the options of this field. |
||
| 25 | * Accepts either array of data or a callback that returns the data. |
||
| 26 | * |
||
| 27 | * @param array|callable $options |
||
| 28 | */ |
||
| 29 | 9 | View Code Duplication | public function set_options( $options ) { |
| 37 | |||
| 38 | /** |
||
| 39 | * Add new options to this field. |
||
| 40 | * Accepts either array of data or a callback that returns the data. |
||
| 41 | * |
||
| 42 | * @param array|callable $options |
||
| 43 | */ |
||
| 44 | 13 | View Code Duplication | public function add_options( $options ) { |
| 52 | |||
| 53 | /** |
||
| 54 | * Check if there are callbacks and populate the options |
||
| 55 | */ |
||
| 56 | protected function load_options() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Retrieve the current options. |
||
| 86 | * |
||
| 87 | * @return array|callable $options |
||
| 88 | */ |
||
| 89 | 13 | public function get_options() { |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
||
| 95 | * Will also work with a callable that returns an array. |
||
| 96 | * |
||
| 97 | * @param array|callable $options |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public function parse_options( $options ) { |
||
| 116 | } // END Field |
||
| 117 |