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 |
||
| 20 | class Value_Set { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Value type which saves a single value |
||
| 24 | */ |
||
| 25 | const TYPE_SINGLE_VALUE = 1; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Value type which saves multiple values with a single property |
||
| 29 | */ |
||
| 30 | const TYPE_MULTIPLE_VALUES = 2; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Value type which saves a single value with multiple proerties |
||
| 34 | */ |
||
| 35 | const TYPE_MULTIPLE_PROPERTIES = 3; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Value type which saves multiple values with multiple propertys |
||
| 39 | */ |
||
| 40 | const TYPE_VALUE_SET = 4; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Default value property required for every value set |
||
| 44 | */ |
||
| 45 | const VALUE_PROPERTY = 'value'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Value set type |
||
| 49 | * |
||
| 50 | * @var integer static::TYPE_* constant |
||
| 51 | */ |
||
| 52 | protected $type = self::TYPE_SINGLE_VALUE; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Array of valid value set types |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $valid_types = array( self::TYPE_SINGLE_VALUE, self::TYPE_MULTIPLE_VALUES, self::TYPE_MULTIPLE_PROPERTIES, self::TYPE_VALUE_SET ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Registered value set properties (properties) with their default value (when the property is missing in the passed raw_value_set) |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $properties = array( self::VALUE_PROPERTY=>'' ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Data the value set represents |
||
| 70 | * |
||
| 71 | * @var array Nullable array |
||
| 72 | */ |
||
| 73 | protected $value_set = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Value set constructor |
||
| 77 | * |
||
| 78 | * @param integer $type static::TYPE_* constant |
||
| 79 | * @param array $additional_properties |
||
| 80 | */ |
||
| 81 | 2 | public function __construct( $type = self::TYPE_SINGLE_VALUE, $additional_properties = array() ) { |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Format a raw value set into one which guarantees that only (and all) registered properties are present |
||
| 92 | * |
||
| 93 | * @param array $raw_value_set |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | protected function get_formatted_value_set( $raw_value_set ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return value set type |
||
| 110 | * |
||
| 111 | * @return int static::TYPE_* constant |
||
| 112 | */ |
||
| 113 | 1 | public function get_type() { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Return whether this value type requires a keepalive key |
||
| 119 | * |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | 2 | public function keepalive() { |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Return whether the data is empty |
||
| 128 | * |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | 5 | public function is_empty() { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Return data formatted according to the value set $type |
||
| 137 | * |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | 4 | public function get() { |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Return the full value set data regardless of type |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | 1 | public function get_set() { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Check if an array is flat |
||
| 185 | * |
||
| 186 | * @param arrat $array |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | protected function is_flat_array( $array ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Convert a flat array to a raw value set |
||
| 202 | * |
||
| 203 | * @param array $value_array |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function flat_array_to_raw_value_set( $value_array ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set the value set data |
||
| 237 | * Accepts: single value, array of values, array of key-values, array of arrays of key-values |
||
| 238 | * |
||
| 239 | * @param mixed $raw_value_set |
||
| 240 | */ |
||
| 241 | 12 | public function set( $raw_value_set ) { |
|
| 257 | } |