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