Conditions | 20 |
Paths | 1440 |
Total Lines | 83 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
18 | private function repository($callback = null) |
||
19 | { |
||
20 | $joins = columnSingleton()->getJoin(); |
||
21 | $columns = columnSingleton()->getColumns(); |
||
22 | |||
23 | $query = DB::table($this->data['table']); |
||
24 | |||
25 | $query->addSelect($this->data['table'].'.'.cb()->pk($this->data['table']).' as primary_key'); |
||
26 | |||
27 | $softDelete = isset($this->data['disable_soft_delete'])?$this->data['disable_soft_delete']:true; |
||
28 | if($softDelete === true && SchemaHelper::hasColumn($this->data['table'],'deleted_at')) { |
||
29 | $query->whereNull($this->data['table'].'.deleted_at'); |
||
30 | } |
||
31 | |||
32 | if(isset($joins)) { |
||
33 | foreach($joins as $join) |
||
34 | { |
||
35 | $query->join($join['target_table'], |
||
36 | $join['target_table_primary'], |
||
37 | $join['operator'], |
||
38 | $join['source_table_foreign'], |
||
39 | $join['type']); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | foreach($columns as $column) { |
||
44 | /** @var ColumnModel $column */ |
||
45 | if($column->getType() != "custom") { |
||
46 | if(strpos($column->getField(),".") === false) { |
||
47 | if(SchemaHelper::hasColumn($this->data['table'], $column->getField())) { |
||
48 | $query->addSelect($this->data['table'].'.'.$column->getField()); |
||
49 | } |
||
50 | }else{ |
||
51 | $query->addSelect($column->getField()); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | $query = getTypeHook($column->getType())->query($query, $column); |
||
56 | } |
||
57 | |||
58 | if(request()->has('q')) |
||
59 | { |
||
60 | if(isset($this->data['hook_search_query'])) { |
||
61 | $query = call_user_func($this->data['hook_search_query'], $query); |
||
62 | }else{ |
||
63 | $query->where(function ($where) use ($columns) { |
||
64 | /** |
||
65 | * @var $where Builder |
||
66 | */ |
||
67 | foreach($columns as $column) |
||
68 | { |
||
69 | if(strpos($column->getField(),".") === false) { |
||
70 | $field = $this->data['table'].'.'.$column->getField(); |
||
71 | }else{ |
||
72 | $field = $column->getField(); |
||
73 | } |
||
74 | $where->orWhere($field, 'like', '%'.request('q').'%'); |
||
|
|||
75 | } |
||
76 | }); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | |||
81 | // Callback From this Method |
||
82 | if(isset($callback) && is_callable($callback)) { |
||
83 | $query = call_user_func($callback, $query); |
||
84 | } |
||
85 | |||
86 | if(isset($this->data['hook_index_query']) && is_callable($this->data['hook_index_query'])) { |
||
87 | $query = call_user_func($this->data['hook_index_query'], $query); |
||
88 | } |
||
89 | |||
90 | |||
91 | if(request()->has(['order_by','order_sort'])) |
||
92 | { |
||
93 | if(in_array(request('order_by'),columnSingleton()->getColumnNameOnly())) { |
||
94 | $query->orderBy(request('order_by'), request('order_sort')); |
||
95 | } |
||
96 | }else{ |
||
97 | $query->orderBy($this->data['table'].'.'.cb()->findPrimaryKey($this->data['table']), "desc"); |
||
98 | } |
||
99 | |||
100 | return $query; |
||
101 | } |
||
103 | } |