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 |
||
18 | class CustomSelects |
||
19 | { |
||
20 | const TYPE_SIMPLE = 'simple'; |
||
21 | const TYPE_COMPLEX = 'complex'; |
||
22 | const TYPE_STRUCTURED = 'structured'; |
||
23 | |||
24 | private $valid_operators = array('COUNT', 'SUM'); |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Original incoming select array |
||
29 | * @var array |
||
30 | */ |
||
31 | private $original_selects; |
||
32 | |||
33 | /** |
||
34 | * Select string that can be added to the query |
||
35 | * @var string |
||
36 | */ |
||
37 | private $columns_to_select_expression; |
||
38 | |||
39 | |||
40 | /** |
||
41 | * An array of aliases for the columns included in the incoming select array. |
||
42 | * @var array |
||
43 | */ |
||
44 | private $column_aliases_in_select; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Enum representation of the "type" of array coming into this value object. |
||
49 | * @var string |
||
50 | * |
||
51 | */ |
||
52 | private $type = ''; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * CustomSelects constructor. |
||
57 | * Incoming selects can be in one of the following formats: |
||
58 | * ---- self::TYPE_SIMPLE array ---- |
||
59 | * This is considered the "simple" type. In this case the array is an numerically indexed array with single or |
||
60 | * multiple columns to select as the values. |
||
61 | * eg. array( 'ATT_ID', 'REG_ID' ) |
||
62 | * eg. array( '*' ) |
||
63 | * If you want to use the columns in any WHERE, GROUP BY, or HAVING clauses, you must instead use the "complex" or |
||
64 | * "structured" method. |
||
65 | * ---- self::TYPE_COMPLEX array ---- |
||
66 | * This is considered the "complex" type. In this case the array is indexed by arbitrary strings that serve as |
||
67 | * column alias, and the value is an numerically indexed array where there are two values. The first value (0) is |
||
68 | * the selection and the second value (1) is the data type. Data types must be one of the types defined in |
||
69 | * EEM_Base::$_valid_wpdb_data_types. |
||
70 | * eg. array( 'count' => array('count(REG_ID)', '%d') ) |
||
71 | * Complex array configuration allows for using the column alias in any WHERE, GROUP BY, or HAVING clauses. |
||
72 | * ---- self::TYPE_STRUCTURED array --- |
||
73 | * This is considered the "structured" type. This type is similar to the complex type except that the array attached |
||
74 | * to the column alias contains three values. The first value is the qualified column name (which can include |
||
75 | * join syntax for models). The second value is the operator performed on the column (i.e. 'COUNT', 'SUM' etc)., |
||
76 | * the third value is the data type. Note, if the select does not have an operator, you can use an empty string for |
||
77 | * the second value. |
||
78 | * Note: for now SUM is only for simple single column expressions (i.e. SUM(Transaction.TXN_total)) |
||
79 | * eg. array( 'registration_count' => array('Registration.REG_ID', 'count', '%d') ); |
||
80 | * |
||
81 | * NOTE: mixing array types in the incoming $select will cause errors. |
||
82 | * |
||
83 | * @param array $selects |
||
84 | * @throws InvalidArgumentException |
||
85 | */ |
||
86 | public function __construct(array $selects) |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Derives what type of custom select has been sent in. |
||
96 | * @param array $selects |
||
97 | * @throws InvalidArgumentException |
||
98 | */ |
||
99 | private function deriveType(array $selects) |
||
145 | |||
146 | |||
147 | /** |
||
148 | * Sets up the various properties for the vo depending on type. |
||
149 | * @param array $selects |
||
150 | * @throws InvalidArgumentException |
||
151 | */ |
||
152 | private function deriveParts(array $selects) |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Validates self::TYPE_COMPLEX and self::TYPE_STRUCTURED select statement parts. |
||
183 | * @param array $select_parts |
||
184 | * @param string $alias |
||
185 | * @throws InvalidArgumentException |
||
186 | */ |
||
187 | private function validateSelectValueForType(array $select_parts, $alias) |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Each type will have an expected count of array elements, this returns what that expected count is. |
||
227 | * @param string $type |
||
228 | * @return int |
||
229 | */ |
||
230 | private function expectedSelectPartCountForType($type = '') { |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Prepares the select statement part for for structured type selects. |
||
242 | * @param array $select_parts |
||
243 | * @param string $alias |
||
244 | * @return string |
||
245 | * @throws InvalidArgumentException |
||
246 | */ |
||
247 | private function assembleSelectStringWithOperator(array $select_parts, $alias) |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Return the datatype from the given select part. |
||
270 | * Remember the select_part has already been validated on object instantiation. |
||
271 | * @param array $select_part |
||
272 | * @return string |
||
273 | */ |
||
274 | private function getDataTypeForSelectType(array $select_part) |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Returns the original select array sent into the VO. |
||
289 | * @return array |
||
290 | */ |
||
291 | public function originalSelects() |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Returns the final assembled select expression derived from the incoming select array. |
||
299 | * @return string |
||
300 | */ |
||
301 | public function columnsToSelectExpression() |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Returns all the column aliases derived from the incoming select array. |
||
309 | * @return array |
||
310 | */ |
||
311 | public function columnAliases() |
||
315 | |||
316 | |||
317 | /** |
||
318 | * Returns the enum type for the incoming select array. |
||
319 | * @return string |
||
320 | */ |
||
321 | public function type() |
||
325 | |||
326 | |||
327 | |||
328 | /** |
||
329 | * Return the datatype for the given column_alias |
||
330 | * @param string $column_alias |
||
331 | * @return string (if there's no data type we return string as the default). |
||
332 | */ |
||
333 | public function getDataTypeForAlias($column_alias) |
||
342 | } |
||
343 |