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 |
||
| 10 | class Query extends QueryHandler implements QueryInterface |
||
| 11 | { |
||
| 12 | /** @var string Class name for interacting with database */ |
||
| 13 | protected $class_name; |
||
| 14 | |||
| 15 | /** @var array Collection of entity field names for sorting order */ |
||
| 16 | protected $sorting = array(); |
||
| 17 | |||
| 18 | /** @var array Collection of entity field names for grouping query results */ |
||
| 19 | protected $grouping = array(); |
||
| 20 | |||
| 21 | /** @var array Collection of query results limitations */ |
||
| 22 | protected $limitation = array(); |
||
| 23 | |||
| 24 | /** @var Condition Query base entity condition group */ |
||
| 25 | protected $own_condition; |
||
| 26 | |||
| 27 | /** @var Condition Query entity condition group */ |
||
| 28 | protected $cConditionGroup; |
||
| 29 | |||
| 30 | /** @var Database Database instance */ |
||
| 31 | protected $database; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Calling this class will result as changing entity. |
||
| 35 | * |
||
| 36 | * @param String $entity |
||
| 37 | * @see self::entity() |
||
| 38 | * @return self Chaining |
||
| 39 | * @throws EntityNotFound |
||
| 40 | */ |
||
| 41 | public function __invoke($entity) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Query constructor. |
||
| 48 | * @param string|null $entity Entity identifier |
||
| 49 | * @param Database Database instance |
||
| 50 | * @throws EntityNotFound |
||
| 51 | */ |
||
| 52 | public function __construct($entity, Database &$database) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Reset all query parameters |
||
| 61 | * @return self Chaining |
||
| 62 | */ |
||
| 63 | public function flush() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Perform database request and get collection of database record objects |
||
| 77 | * @see \samson\activerecord\Query::execute() |
||
| 78 | * @param mixed $return External variable to store query results |
||
| 79 | * @return mixed If no arguments passed returns query results collection, otherwise query success status |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function exec(&$return = null) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Perform database request and get first record from results collection |
||
| 98 | * @see \samson\activerecord\Query::execute() |
||
| 99 | * @param mixed $return External variable to store query results |
||
| 100 | * @return mixed If no arguments passed returns query results first database record object, |
||
| 101 | * otherwise query success status |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function first(& $return = null) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Perform database request and get array of record field values |
||
| 120 | * @see \samson\activerecord\Query::execute() |
||
| 121 | * @param string $fieldName Record field name to get value from |
||
| 122 | * @param string $return External variable to store query results |
||
| 123 | * @return Ambigous <boolean, NULL, mixed> |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function fields($fieldName, &$return = null) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Set query entity to work with. |
||
| 139 | * |
||
| 140 | * @param string $entity Entity identifier |
||
| 141 | * @return Query|string Chaining or current entity identifier if nothing is passed |
||
| 142 | * @throws EntityNotFound |
||
| 143 | */ |
||
| 144 | public function entity($entity = null) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get correct query condition depending on entity field name. |
||
| 162 | * If base entity has field with this name - use base entity condition |
||
| 163 | * group, otherwise default condition group. |
||
| 164 | * |
||
| 165 | * @param string $fieldName Entity field name |
||
| 166 | * @return Condition Correct query condition group |
||
| 167 | */ |
||
| 168 | protected function &getConditionGroup($fieldName) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Add query condition as prepared Condition instance. |
||
| 180 | * |
||
| 181 | * @param ConditionInterface $condition Condition to be added |
||
| 182 | * @return self Chaining |
||
| 183 | */ |
||
| 184 | public function whereCondition(ConditionInterface $condition) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add condition to current query. |
||
| 202 | * |
||
| 203 | * @param string|Condition|Argument $fieldName Entity field name |
||
| 204 | * @param string $fieldValue Value |
||
| 205 | * @param string $relation Relation between field name and its value |
||
| 206 | * @return self Chaining |
||
| 207 | */ |
||
| 208 | public function where($fieldName, $fieldValue = null, $relation = '=') |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Join entity to query. |
||
| 229 | * |
||
| 230 | * @param string $entityName Entity identifier |
||
| 231 | * @return self Chaining |
||
| 232 | */ |
||
| 233 | public function join($entityName) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Add query result grouping. |
||
| 244 | * |
||
| 245 | * @param string $fieldName Entity field identifier for grouping |
||
| 246 | * @return self Chaining |
||
| 247 | */ |
||
| 248 | public function groupBy($fieldName) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Add query result quantity limitation. |
||
| 258 | * |
||
| 259 | * @param int $offset Resulting offset |
||
| 260 | * @param null|int $quantity Amount of RecordInterface object to return |
||
| 261 | * @return self Chaining |
||
| 262 | */ |
||
| 263 | public function limit($offset, $quantity = null) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Add query result sorting. |
||
| 273 | * |
||
| 274 | * @param string $fieldName Entity field identifier for worting |
||
| 275 | * @param string $order Sorting order |
||
| 276 | * @return self Chaining |
||
| 277 | */ |
||
| 278 | public function orderBy($fieldName, $order = 'ASC') |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Add condition by primary field |
||
| 288 | * |
||
| 289 | * @param string $value Primary field value |
||
| 290 | * @return self Chaining |
||
| 291 | */ |
||
| 292 | public function id($value) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Add condition to current query. |
||
| 304 | * This method supports receives three possible types for $fieldName, |
||
| 305 | * this is deprecated logic and this should be changed to use separate methods |
||
| 306 | * for each argument type. |
||
| 307 | * |
||
| 308 | * @param string|Condition|Argument $fieldName Entity field name |
||
| 309 | * @param string $fieldValue Value |
||
| 310 | * @param string $relation Relation between field name and its value |
||
| 311 | * @deprecated @see self::where() |
||
| 312 | * @return self Chaining |
||
| 313 | */ |
||
| 314 | public function cond($fieldName, $fieldValue = null, $relation = '=') |
||
| 330 | } |
||
| 331 |