We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 16 |
Paths | 97 |
Total Lines | 77 |
Code Lines | 38 |
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 |
||
40 | private function fetch($arg) |
||
41 | { |
||
42 | // get the actual words that were used to search for an item (the search term / search string) |
||
43 | $search_string = request()->input('q') ?? false; |
||
44 | |||
45 | // if the Class was passed as the sole argument, use that as the configured Model |
||
46 | // otherwise assume the arguments are actually the configuration array |
||
47 | $config = []; |
||
48 | |||
49 | if (! is_array($arg)) { |
||
50 | if (! class_exists($arg)) { |
||
51 | return response()->json(['error' => 'Class: '.$arg.' does not exists'], 500); |
||
52 | } |
||
53 | $config['model'] = $arg; |
||
54 | } else { |
||
55 | $config = $arg; |
||
56 | } |
||
57 | |||
58 | $model_instance = new $config['model']; |
||
59 | // set configuration defaults |
||
60 | $config['paginate'] = isset($config['paginate']) ? $config['paginate'] : 10; |
||
61 | $config['searchable_attributes'] = $config['searchable_attributes'] ?? $model_instance->identifiableAttribute(); |
||
62 | $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 |
||
63 | |||
64 | // FetchOperation sends an empty query to retrieve the default entry for select when field is not nullable. |
||
65 | // Also sends an empty query in case we want to load all entities to emulate non-ajax fields |
||
66 | // when using InlineCreate. |
||
67 | |||
68 | if ($search_string === false) { |
||
69 | return ($config['paginate'] !== false) ? |
||
70 | $config['query']->paginate($config['paginate']) : |
||
71 | $config['query']->get(); |
||
72 | } |
||
73 | |||
74 | $textColumnTypes = ['string', 'json_string', 'text', 'longText', 'json_array', 'json']; |
||
75 | |||
76 | // if the query builder brings any where clause already defined by the user we must |
||
77 | // ensure that the where prevails and we should only use our search as a complement to the query constraints. |
||
78 | // e.g user want only the active products, so in fetch they would return something like: |
||
79 | // .... 'query' => function($model) { return $model->where('active', 1); } |
||
80 | // 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; |
||
81 | |||
82 | if (! empty($config['query']->getQuery()->wheres)) { |
||
83 | $config['query'] = $config['query']->where(function ($query) use ($model_instance, $config, $search_string, $textColumnTypes) { |
||
84 | foreach ((array) $config['searchable_attributes'] as $k => $searchColumn) { |
||
85 | $operation = ($k == 0) ? 'where' : 'orWhere'; |
||
86 | $columnType = $model_instance->getColumnType($searchColumn); |
||
87 | |||
88 | if (in_array($columnType, $textColumnTypes)) { |
||
89 | $tempQuery = $query->{$operation}($searchColumn, 'LIKE', '%'.$search_string.'%'); |
||
90 | } else { |
||
91 | $tempQuery = $query->{$operation}($searchColumn, $search_string); |
||
92 | } |
||
93 | } |
||
94 | // If developer provide an empty searchable_attributes array it means they don't want us to search |
||
95 | // in any specific column, or try to guess the column from model identifiableAttribute. |
||
96 | // In that scenario we will not have any $tempQuery here, so we just return the query, is up to the developer |
||
97 | // to do their own search. |
||
98 | return $tempQuery ?? $query; |
||
99 | }); |
||
100 | } else { |
||
101 | foreach ((array) $config['searchable_attributes'] as $k => $searchColumn) { |
||
102 | $operation = ($k == 0) ? 'where' : 'orWhere'; |
||
103 | $columnType = $model_instance->getColumnType($searchColumn); |
||
104 | |||
105 | if (in_array($columnType, $textColumnTypes)) { |
||
106 | $config['query'] = $config['query']->{$operation}($searchColumn, 'LIKE', '%'.$search_string.'%'); |
||
107 | } else { |
||
108 | $config['query'] = $config['query']->{$operation}($searchColumn, $search_string); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // return the results with or without pagination |
||
114 | return ($config['paginate'] !== false) ? |
||
115 | $config['query']->paginate($config['paginate']) : |
||
116 | $config['query']->get(); |
||
117 | } |
||
119 |