Conditions | 21 |
Paths | 60 |
Total Lines | 72 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
16 | public function apply($builder, RepositoryInterface $repository) |
||
17 | { |
||
18 | $fieldsSearchable = $repository->getFieldsSearchable(); |
||
19 | $search = $this->request->get(config('repository.criteria.params.search', 'search'), null); |
||
20 | $searchFields = $this->request->get(config('repository.criteria.params.searchFields', 'searchFields'), null); |
||
21 | $filter = $this->request->get(config('repository.criteria.params.filter', 'filter'), null); |
||
22 | $orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null); |
||
23 | $sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc'); |
||
24 | $sortedBy = ! empty($sortedBy) ? $sortedBy : 'asc'; |
||
25 | |||
26 | if ($search && is_array($fieldsSearchable) && count($fieldsSearchable)) { |
||
27 | $searchFields = is_array($searchFields) || is_null($searchFields) ? $searchFields : explode(';', |
||
28 | $searchFields); |
||
29 | $fields = $this->parserFieldsSearch($fieldsSearchable, $searchFields); |
||
30 | $isFirstField = true; |
||
31 | $searchData = $this->parserSearchData($search); |
||
32 | $search = $this->parserSearchValue($search); |
||
|
|||
33 | $modelForceAndWhere = false; |
||
34 | |||
35 | $builder = $builder->where(function ($query) use ( |
||
36 | $fields, |
||
37 | $search, |
||
38 | $searchData, |
||
39 | $isFirstField, |
||
40 | $modelForceAndWhere |
||
41 | ) { |
||
42 | foreach ($fields as $field => $condition) { |
||
43 | if (is_numeric($field)) { |
||
44 | $field = $condition; |
||
45 | $condition = '='; |
||
46 | } |
||
47 | |||
48 | $value = null; |
||
49 | |||
50 | $condition = trim(strtolower($condition)); |
||
51 | |||
52 | if (isset($searchData[$field])) { |
||
53 | $value = $condition === 'like' ? "%{$searchData[$field]}%" : $searchData[$field]; |
||
54 | } else { |
||
55 | if (! is_null($search)) { |
||
56 | $value = $condition === 'like' ? "%{$search}%" : $search; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if ($isFirstField || $modelForceAndWhere) { |
||
61 | if (! is_null($value)) { |
||
62 | $query->where($field, $condition, $value); |
||
63 | $isFirstField = false; |
||
64 | } |
||
65 | } else { |
||
66 | if (! is_null($value)) { |
||
67 | $query->orWhere($field, $condition, $value); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | }); |
||
72 | } |
||
73 | |||
74 | if (isset($orderBy) && in_array(strtolower($sortedBy), ['asc', 'desc'])) { |
||
75 | $builder = $builder->orderBy($orderBy, $sortedBy); |
||
76 | } |
||
77 | |||
78 | foreach (FilterManager::get() as $filter) { |
||
79 | // eg. filter 'hot' 会调用方法 'filterHot' |
||
80 | $method_name = camel_case('filter_'.$filter); |
||
81 | if (method_exists($this, $method_name)) { |
||
82 | $builder = call_user_func([$this, $method_name], $builder); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return $builder; |
||
87 | } |
||
88 | } |
||
89 |
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.