| Conditions | 12 |
| Paths | 20 |
| Total Lines | 69 |
| Code Lines | 50 |
| 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 |
||
| 29 | public function index($name, $value = null) |
||
| 30 | { |
||
| 31 | $records = null; |
||
| 32 | // set current page and offset |
||
| 33 | $page = (int)$this->request->query->get('page', 0); |
||
|
|
|||
| 34 | $cfgs = $this->application->configs; |
||
| 35 | $userPerPage = (int)$cfgs['usersOnPage']; |
||
| 36 | if ($userPerPage < 1) { |
||
| 37 | $userPerPage = 1; |
||
| 38 | } |
||
| 39 | $offset = $page * $userPerPage; |
||
| 40 | |||
| 41 | switch ($name) { |
||
| 42 | case 'rating': // rating list, order by rating DESC |
||
| 43 | // check if rating is enabled |
||
| 44 | if ((int)$cfgs['rating'] !== 1) { |
||
| 45 | throw new NotFoundException(); |
||
| 46 | } |
||
| 47 | $records = (new ProfileRecords())->orderBy('rating', 'DESC'); |
||
| 48 | break; |
||
| 49 | case 'hobby': // search by hobby |
||
| 50 | if (Str::likeEmpty($value)) { |
||
| 51 | throw new NotFoundException(); |
||
| 52 | } |
||
| 53 | $records = (new ProfileRecords())->where('hobby', 'like', '%' . $value . '%'); |
||
| 54 | break; |
||
| 55 | case 'city': |
||
| 56 | if (Str::likeEmpty($value)) { |
||
| 57 | throw new NotFoundException(); |
||
| 58 | } |
||
| 59 | $records = (new ProfileRecords())->where('city', $value); |
||
| 60 | break; |
||
| 61 | case 'born': |
||
| 62 | if ($value === null || !Any::isInt($value)) { |
||
| 63 | throw new NotFoundException(); |
||
| 64 | } |
||
| 65 | $records = (new ProfileRecords())->where('birthday', 'like', $value . '-%'); |
||
| 66 | break; |
||
| 67 | case 'all': |
||
| 68 | $records = (new ProfileRecords())->orderBy('id', 'DESC'); |
||
| 69 | break; |
||
| 70 | default: |
||
| 71 | $this->response->redirect('profile/index/all'); |
||
| 72 | break; |
||
| 73 | } |
||
| 74 | |||
| 75 | // build pagination |
||
| 76 | $pagination = new SimplePagination([ |
||
| 77 | 'url' => ['profile/index', $name, $value], |
||
| 78 | 'page' => $page, |
||
| 79 | 'step' => $userPerPage, |
||
| 80 | 'total' => $records->count() |
||
| 81 | ]); |
||
| 82 | |||
| 83 | // get profile list with relation for user and role tables in 1 query |
||
| 84 | $profiles = $records->with(['user', 'user.role']) |
||
| 85 | ->skip($offset) |
||
| 86 | ->take($userPerPage) |
||
| 87 | ->get(); |
||
| 88 | |||
| 89 | // render output view |
||
| 90 | return $this->view->render('index', [ |
||
| 91 | 'records' => $profiles, |
||
| 92 | 'pagination' => $pagination, |
||
| 93 | 'id' => $name, |
||
| 94 | 'add' => $value, |
||
| 95 | 'ratingOn' => (int)$cfgs['rating'] |
||
| 96 | ]); |
||
| 97 | } |
||
| 98 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: