We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 12 |
Paths | 193 |
Total Lines | 51 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
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']($config['model']) : $model_instance; // if a closure that has been passed as "query", use the closure - otherwise use the model |
||
62 | |||
63 | // FetchOperation sends an empty query to retrieve the default entry for select when field is not nullable. |
||
64 | // Also sends an empty query in case we want to load all entities to emulate non-ajax fields |
||
65 | // when using InlineCreate. |
||
66 | |||
67 | if ($search_string === false) { |
||
68 | return ($config['paginate'] !== false) ? |
||
69 | $config['query']->paginate($config['paginate']) : |
||
70 | $config['query']->get(); |
||
71 | } |
||
72 | |||
73 | $textColumnTypes = ['string', 'json_string', 'text']; |
||
74 | // for each searchable attribute, add a WHERE clause |
||
75 | foreach ((array) $config['searchable_attributes'] as $k => $searchColumn) { |
||
76 | $operation = ($k == 0) ? 'where' : 'orWhere'; |
||
77 | $columnType = $config['query']->getColumnType($searchColumn); |
||
78 | |||
79 | if (in_array($columnType, $textColumnTypes)) { |
||
80 | $config['query'] = $config['query']->{$operation}($searchColumn, 'LIKE', '%'.$search_string.'%'); |
||
81 | } else { |
||
82 | $config['query'] = $config['query']->{$operation}($searchColumn, $search_string); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // return the results with or without pagination |
||
87 | return ($config['paginate'] !== false) ? |
||
88 | $config['query']->paginate($config['paginate']) : |
||
89 | $config['query']->get(); |
||
90 | } |
||
92 |