We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 18 |
Paths | 129 |
Total Lines | 84 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 13 | ||
Bugs | 6 | Features | 3 |
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 |
||
39 | private function fetch($arg) |
||
40 | { |
||
41 | // get the actual words that were used to search for an item (the search term / search string) |
||
42 | $search_string = request()->input('q') ?? false; |
||
43 | |||
44 | // if the Class was passed as the sole argument, use that as the configured Model |
||
45 | // otherwise assume the arguments are actually the configuration array |
||
46 | $config = []; |
||
47 | |||
48 | if (! is_array($arg)) { |
||
49 | if (! class_exists($arg)) { |
||
50 | return response()->json(['error' => 'Class: '.$arg.' does not exists'], 500); |
||
51 | } |
||
52 | $config['model'] = $arg; |
||
53 | } else { |
||
54 | $config = $arg; |
||
55 | } |
||
56 | |||
57 | $model_instance = new $config['model']; |
||
58 | // set configuration defaults |
||
59 | $config['paginate'] = isset($config['paginate']) ? $config['paginate'] : 10; |
||
60 | $config['searchable_attributes'] = $config['searchable_attributes'] ?? $model_instance->identifiableAttribute(); |
||
61 | $config['query'] = isset($config['query']) && is_callable($config['query']) ? $config['query']($model_instance) : $model_instance; // if a closure that has been passed as "query", use the closure - otherwise use the model |
||
62 | |||
63 | // FetchOperation is aware of an optional parameter 'keys' that will fetch you the entity/entities that match the provided keys |
||
64 | if (request()->has('keys')) { |
||
65 | $array_keys = request()->get('keys'); |
||
66 | if (is_array($array_keys)) { |
||
67 | return $model_instance->whereIn($model_instance->getKeyName(), $array_keys)->get(); |
||
68 | } else { |
||
69 | return $model_instance->where($model_instance->getKeyName(), $array_keys)->get(); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | // FetchOperation sends an empty query to retrieve the default entry for select when field is not nullable. |
||
74 | // Also sends an empty query in case we want to load all entities to emulate non-ajax fields |
||
75 | // when using InlineCreate. |
||
76 | |||
77 | if ($search_string === false) { |
||
78 | return ($config['paginate'] !== false) ? |
||
79 | $config['query']->paginate($config['paginate']) : |
||
80 | $config['query']->get(); |
||
81 | } |
||
82 | |||
83 | $textColumnTypes = ['string', 'json_string', 'text', 'longText', 'json_array']; |
||
84 | |||
85 | // if the query builder brings any where clause already defined by the user we must |
||
86 | // ensure that the where prevails and we should only use our search as a complement to the query constraints. |
||
87 | // e.g user want only the active products, so in fetch he would return something like: |
||
88 | // .... 'query' => function($model) { return $model->where('active', 1); } |
||
89 | // So it reads: SELECT ... WHERE active = 1 AND (XXX = x OR YYY = y) and not SELECT ... WHERE active = 1 AND XXX = x OR YYY = y; |
||
90 | |||
91 | if (! empty($config['query']->getQuery()->wheres)) { |
||
92 | $config['query'] = $config['query']->where(function ($query) use ($model_instance, $config, $search_string, $textColumnTypes) { |
||
93 | foreach ((array) $config['searchable_attributes'] as $k => $searchColumn) { |
||
94 | $operation = ($k == 0) ? 'where' : 'orWhere'; |
||
95 | $columnType = $model_instance->getColumnType($searchColumn); |
||
96 | |||
97 | if (in_array($columnType, $textColumnTypes)) { |
||
98 | $tempQuery = $query->{$operation}($searchColumn, 'LIKE', '%'.$search_string.'%'); |
||
99 | } else { |
||
100 | $tempQuery = $query->{$operation}($searchColumn, $search_string); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $tempQuery; |
||
105 | }); |
||
106 | } else { |
||
107 | foreach ((array) $config['searchable_attributes'] as $k => $searchColumn) { |
||
108 | $operation = ($k == 0) ? 'where' : 'orWhere'; |
||
109 | $columnType = $model_instance->getColumnType($searchColumn); |
||
110 | |||
111 | if (in_array($columnType, $textColumnTypes)) { |
||
112 | $config['query'] = $config['query']->{$operation}($searchColumn, 'LIKE', '%'.$search_string.'%'); |
||
113 | } else { |
||
114 | $config['query'] = $config['query']->{$operation}($searchColumn, $search_string); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // return the results with or without pagination |
||
120 | return ($config['paginate'] !== false) ? |
||
121 | $config['query']->paginate($config['paginate']) : |
||
122 | $config['query']->get(); |
||
123 | } |
||
125 |