Conditions | 46 |
Paths | 360 |
Total Lines | 145 |
Code Lines | 98 |
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 |
||
38 | public function apply($model, RepositoryInterface $repository) |
||
39 | { |
||
40 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
41 | $search = $this->request->get(config('domains.criteria.params.search', 'search'), null); |
||
42 | $searchFields = $this->request->get(config('domains.criteria.params.searchFields', 'searchFields'), null); |
||
43 | $filter = $this->request->get(config('domains.criteria.params.filter', 'filter'), null); |
||
44 | $orderBy = $this->request->get(config('domains.criteria.params.orderBy', 'orderBy'), null); |
||
45 | $sortedBy = $this->request->get(config('domains.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
||
46 | $with = $this->request->get(config('domains.criteria.params.with', 'with'), null); |
||
47 | $withCount = $this->request->get(config('domains.criteria.params.withCount', 'withCount'), null); |
||
48 | $searchJoin = $this->request->get(config('domains.criteria.params.searchJoin', 'searchJoin'), null); |
||
49 | $sortedBy = !empty($sortedBy) ? $sortedBy : 'asc'; |
||
50 | |||
51 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
52 | |||
53 | $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', $searchFields); |
||
54 | $isFirstField = true; |
||
55 | $searchData = $this->parserSearchData($search); |
||
56 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields, array_keys($searchData)); |
||
57 | $search = $this->parserSearchValue($search); |
||
|
|||
58 | $modelForceAndWhere = strtolower($searchJoin) === 'and'; |
||
59 | |||
60 | $model = $model->where(function ($query) use ($fields, $search, $searchData, $isFirstField, $modelForceAndWhere) { |
||
61 | /** @var Builder $query */ |
||
62 | |||
63 | foreach ($fields as $field => $condition) { |
||
64 | |||
65 | if (is_numeric($field)) { |
||
66 | $field = $condition; |
||
67 | $condition = "="; |
||
68 | } |
||
69 | |||
70 | $value = null; |
||
71 | |||
72 | $condition = trim(strtolower($condition)); |
||
73 | |||
74 | if (isset($searchData[$field])) { |
||
75 | $value = ($condition == "like" || $condition == "ilike") ? "%{$searchData[$field]}%" : $searchData[$field]; |
||
76 | } else { |
||
77 | if (!is_null($search) && !in_array($condition,['in','between'])) { |
||
78 | $value = ($condition == "like" || $condition == "ilike") ? "%{$search}%" : $search; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | $relation = null; |
||
83 | if(stripos($field, '.')) { |
||
84 | $explode = explode('.', $field); |
||
85 | $field = array_pop($explode); |
||
86 | $relation = implode('.', $explode); |
||
87 | } |
||
88 | if($condition === 'in'){ |
||
89 | $value = explode(',',$value); |
||
90 | if( trim($value[0]) === "" || $field == $value[0]){ |
||
91 | $value = null; |
||
92 | } |
||
93 | } |
||
94 | if($condition === 'between'){ |
||
95 | $value = explode(',',$value); |
||
96 | if(count($value) < 2){ |
||
97 | $value = null; |
||
98 | } |
||
99 | } |
||
100 | $modelTableName = $query->getModel()->getTable(); |
||
101 | if ( $isFirstField || $modelForceAndWhere ) { |
||
102 | if (!is_null($value)) { |
||
103 | if(!is_null($relation)) { |
||
104 | $query->whereHas($relation, function($query) use($field,$condition,$value) { |
||
105 | if($condition === 'in'){ |
||
106 | $query->whereIn($field,$value); |
||
107 | }elseif($condition === 'between'){ |
||
108 | $query->whereBetween($field,$value); |
||
109 | }else{ |
||
110 | $query->where($field,$condition,$value); |
||
111 | } |
||
112 | }); |
||
113 | } else { |
||
114 | if($condition === 'in'){ |
||
115 | $query->whereIn($modelTableName.'.'.$field,$value); |
||
116 | }elseif($condition === 'between'){ |
||
117 | $query->whereBetween($modelTableName.'.'.$field,$value); |
||
118 | }else{ |
||
119 | $query->where($modelTableName.'.'.$field,$condition,$value); |
||
120 | } |
||
121 | } |
||
122 | $isFirstField = false; |
||
123 | } |
||
124 | } else { |
||
125 | if (!is_null($value)) { |
||
126 | if(!is_null($relation)) { |
||
127 | $query->orWhereHas($relation, function($query) use($field,$condition,$value) { |
||
128 | if($condition === 'in'){ |
||
129 | $query->whereIn($field,$value); |
||
130 | }elseif($condition === 'between'){ |
||
131 | $query->whereBetween($field, $value); |
||
132 | }else{ |
||
133 | $query->where($field,$condition,$value); |
||
134 | } |
||
135 | }); |
||
136 | } else { |
||
137 | if($condition === 'in'){ |
||
138 | $query->orWhereIn($modelTableName.'.'.$field, $value); |
||
139 | }elseif($condition === 'between'){ |
||
140 | $query->whereBetween($modelTableName.'.'.$field,$value); |
||
141 | }else{ |
||
142 | $query->orWhere($modelTableName.'.'.$field, $condition, $value); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | }); |
||
149 | } |
||
150 | |||
151 | if (isset($orderBy) && !empty($orderBy)) { |
||
152 | $orderBySplit = explode(';', $orderBy); |
||
153 | if(count($orderBySplit) > 1) { |
||
154 | $sortedBySplit = explode(';', $sortedBy); |
||
155 | foreach ($orderBySplit as $orderBySplitItemKey => $orderBySplitItem) { |
||
156 | $sortedBy = isset($sortedBySplit[$orderBySplitItemKey]) ? $sortedBySplit[$orderBySplitItemKey] : $sortedBySplit[0]; |
||
157 | $model = $this->parserFieldsOrderBy($model, $orderBySplitItem, $sortedBy); |
||
158 | } |
||
159 | } else { |
||
160 | $model = $this->parserFieldsOrderBy($model, $orderBySplit[0], $sortedBy); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | if (isset($filter) && !empty($filter)) { |
||
165 | if (is_string($filter)) { |
||
166 | $filter = explode(';', $filter); |
||
167 | } |
||
168 | |||
169 | $model = $model->select($filter); |
||
170 | } |
||
171 | |||
172 | if ($with) { |
||
173 | $with = explode(';', $with); |
||
174 | $model = $model->with($with); |
||
175 | } |
||
176 | |||
177 | if ($withCount) { |
||
178 | $withCount = explode(';', $withCount); |
||
179 | $model = $model->withCount($withCount); |
||
180 | } |
||
181 | |||
182 | return $model; |
||
183 | } |
||
334 | } |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.