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 null|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 null|string|array String, array of key-value pairs or array of arrays of key-value pairs |
||
139 | */ |
||
140 | 6 | public function get() { |
|
141 | 6 | if ( $this->value_set === null ) { |
|
142 | 2 | return null; |
|
143 | } |
||
144 | 4 | $value = ''; |
|
145 | 4 | $value_property = static::VALUE_PROPERTY; |
|
146 | |||
147 | 4 | switch ( $this->type ) { |
|
148 | 4 | case static::TYPE_MULTIPLE_VALUES: |
|
149 | $value = array_map( function( $set ) use ( $value_property ) { |
||
150 | 1 | return $set[ $value_property ]; |
|
151 | 1 | }, $this->value_set ); |
|
152 | 1 | break; |
|
153 | 3 | View Code Duplication | case static::TYPE_MULTIPLE_PROPERTIES: |
154 | 1 | $value = array(); |
|
155 | 1 | if ( ! empty( $this->value_set ) ) { |
|
156 | 1 | $value = $this->value_set[0]; |
|
157 | 1 | } |
|
158 | 1 | break; |
|
159 | 2 | case static::TYPE_VALUE_SET: |
|
160 | 1 | $value = $this->value_set; |
|
161 | 1 | break; |
|
162 | |||
163 | 1 | case static::TYPE_SINGLE_VALUE: |
|
164 | 1 | View Code Duplication | default: |
165 | 1 | if ( ! empty( $this->value_set ) ) { |
|
166 | 1 | $value = $this->value_set[0][ static::VALUE_PROPERTY ]; |
|
167 | 1 | } |
|
168 | 1 | break; |
|
169 | 4 | } |
|
170 | |||
171 | 4 | return $value; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Return the full value set data regardless of type |
||
176 | * |
||
177 | * @return array<array> |
||
178 | */ |
||
179 | 1 | public function get_set() { |
|
182 | |||
183 | /** |
||
184 | * Check if an array is flat |
||
185 | * |
||
186 | * @param array $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<array> |
||
205 | */ |
||
206 | protected function flat_array_to_raw_value_set( $value_array ) { |
||
231 | |||
232 | /** |
||
233 | * Set the value set data |
||
234 | * Accepts: single value, array of values, array of key-values, array of arrays of key-values |
||
235 | * |
||
236 | * @param null|string|array $raw_value_set String, indexed array, array of key-value pairs or array of arrays of key-value pairs |
||
237 | */ |
||
238 | 17 | public function set( $raw_value_set ) { |
|
254 | } |