| Conditions | 23 |
| Paths | 40 |
| Total Lines | 64 |
| Code Lines | 45 |
| 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 |
||
| 20 | public function actionIndex(Request $request, Response $response, $args) |
||
| 21 | { |
||
| 22 | $modelName = 'App\Model\\'.Helper::dashesToCamelCase($args['entity'], true); |
||
| 23 | $params = $request->getQueryParams(); |
||
| 24 | $query = $modelName::CurrentUser(); |
||
| 25 | |||
| 26 | if (isset($params['withTrashed']) && $params['withTrashed'] == 1) { |
||
| 27 | $query = $modelName::withTrashed(); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (isset($params['filters'])) { |
||
| 31 | $filters = json_decode($params['filters'], true); |
||
| 32 | |||
| 33 | foreach ($filters as $filter) { |
||
| 34 | $filter['operator'] = trim(strtolower($filter['operator'])); |
||
| 35 | $filter['attribute'] = trim($filter['attribute']); |
||
| 36 | |||
| 37 | if (empty($filter['operator']) || empty($filter['attribute']) || empty($filter['value'])) continue; |
||
| 38 | |||
| 39 | switch ($filter['operator']) { |
||
| 40 | case 'in': |
||
| 41 | $query = $query->whereIn($filter['attribute'], $filter['value']); |
||
| 42 | break; |
||
| 43 | case 'not in': |
||
| 44 | $query = $query->whereNotIn($filter['attribute'], $filter['value']); |
||
| 45 | break; |
||
| 46 | case 'like': |
||
| 47 | $query = $query->where($filter['attribute'], 'like', '%' . $filter['value'] . '%'); |
||
| 48 | break; |
||
| 49 | case '=': |
||
| 50 | case '!=': |
||
| 51 | case '>': |
||
| 52 | case '>=': |
||
| 53 | case '<': |
||
| 54 | case '<=': |
||
| 55 | $query = $query->where($filter['attribute'], $filter['operator'], $filter['value']); |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if (isset($params['sort'])) { |
||
| 62 | $sorters = json_decode($params['sort'], true); |
||
| 63 | |||
| 64 | foreach ($sorters as $sorter) { |
||
| 65 | $sorter['direction'] = trim(strtolower($sorter['direction'])) == 'asc' ? 'asc' : 'desc'; |
||
| 66 | $query->orderBy(trim($sorter['attribute']), $sorter['direction']); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $pageNumber = null; |
||
| 71 | $pageSize = null; |
||
| 72 | if (isset($params['page']['number'])) { |
||
| 73 | $pageNumber = $params['page']['number']; |
||
| 74 | $pageSize = (isset($params['page']['size']) && $params['page']['size'] <= 100) ? $params['page']['size'] : 15; |
||
| 75 | $entities = $query->withoutGlobalScopes([MaxPerPageScope::class])->paginate($pageSize, ['*'], 'page', $pageNumber); |
||
| 76 | } else { |
||
| 77 | $entities = $query->get(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $result = $this->encode($request, $entities, $pageNumber, $pageSize); |
||
| 81 | |||
| 82 | return $this->renderer->jsonApiRender($response, 200, $result); |
||
| 83 | } |
||
| 84 | |||
| 183 | } |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.