| Conditions | 20 |
| Paths | 2592 |
| Total Lines | 63 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 17 | public function select($filters = [], $options = []): SelectInterface |
||
| 18 | { |
||
| 19 | $class = get_class($this->model); |
||
| 20 | $table = $class::table(); |
||
| 21 | |||
| 22 | $Query = $table->select(null, $class::table_alias()); |
||
| 23 | |||
| 24 | |||
| 25 | if(!isset($options['eager']) || $options['eager'] !== false) |
||
| 26 | { |
||
| 27 | $Query->eager(); |
||
| 28 | } |
||
| 29 | |||
| 30 | |||
| 31 | foreach($table->columns() as $column_name => $column) |
||
| 32 | { |
||
| 33 | if(isset($filters[$column_name]) && is_string($filters[$column_name])) |
||
| 34 | $Query->aw_eq($column_name, $filters[$column_name]); |
||
| 35 | } |
||
| 36 | |||
| 37 | if(is_subclass_of($event = new $class(), '\HexMakina\kadro\Models\Interfaces\EventInterface')) |
||
| 38 | { |
||
| 39 | if(!empty($filters['date_start'])) |
||
| 40 | $Query->aw_gte($event->event_field(), $filters['date_start'], $Query->table_label(), ':filter_date_start'); |
||
| 41 | |||
| 42 | if(!empty($filters['date_stop'])) |
||
| 43 | $Query->aw_lte($event->event_field(), $filters['date_stop'], $Query->table_label(), ':filter_date_stop'); |
||
| 44 | |||
| 45 | if(empty($options['order_by'])) |
||
| 46 | $Query->order_by([$event->event_field(), 'DESC']); |
||
| 47 | } |
||
| 48 | |||
| 49 | if(isset($filters['content'])) $Query->aw_filter_content($filters['content']); |
||
| 50 | |||
| 51 | if(isset($filters['ids'])) |
||
| 52 | { |
||
| 53 | if(empty($filters['ids'])) |
||
| 54 | $Query->and_where('1=0'); // TODO: this is a new low.. find another way to cancel query |
||
| 55 | else $Query->aw_numeric_in('id', $filters['ids'], $Query->table_label()); |
||
| 56 | } |
||
| 57 | |||
| 58 | if(isset($options['order_by'])) // TODO commenting required about the array situation |
||
| 59 | { |
||
| 60 | $order_by = $options['order_by']; |
||
| 61 | |||
| 62 | if(is_string($order_by)) |
||
| 63 | $Query->order_by($order_by); |
||
| 64 | |||
| 65 | elseif(is_array($order_by)) |
||
| 66 | foreach($options['order_by'] as $order_by) |
||
| 67 | { |
||
| 68 | if(!isset($order_by[2])) |
||
| 69 | array_unshift($order_by, ''); |
||
| 70 | |||
| 71 | list($order_table, $order_field, $order_direction) = $order_by; |
||
| 72 | $Query->order_by([$order_table ?? '', $order_field, $order_direction]); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | if(isset($options['limit']) && is_array($options['limit'])) |
||
| 77 | $Query->limit($options['limit'][1], $options['limit'][0]); |
||
| 78 | |||
| 79 | return $Query; |
||
| 80 | } |
||
| 82 |