| Conditions | 5 |
| Paths | 3 |
| Total Lines | 57 |
| Code Lines | 35 |
| 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 |
||
| 53 | public function search($params) |
||
| 54 | { |
||
| 55 | $query = User::find(); |
||
| 56 | |||
| 57 | // add conditions that should always apply here |
||
| 58 | $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id'); |
||
| 59 | |||
| 60 | $dataProvider = new ActiveDataProvider([ |
||
| 61 | 'query' => $query, |
||
| 62 | 'sort' => [ |
||
| 63 | 'defaultOrder' => ['id' => SORT_ASC], |
||
| 64 | ], |
||
| 65 | ]); |
||
| 66 | |||
| 67 | $dataProvider->setSort([ |
||
| 68 | 'attributes' => [ |
||
| 69 | 'id', |
||
| 70 | 'username', |
||
| 71 | 'email', |
||
| 72 | 'status', |
||
| 73 | 'userRoleName' => [ |
||
| 74 | 'asc' => ['item_name' => SORT_ASC], |
||
| 75 | 'desc' => ['item_name' => SORT_DESC], |
||
| 76 | 'default' => SORT_ASC, |
||
| 77 | 'label' => 'Role Name', |
||
| 78 | ], |
||
| 79 | 'last_visit' |
||
| 80 | ] |
||
| 81 | ]); |
||
| 82 | |||
| 83 | $this->load($params); |
||
| 84 | |||
| 85 | if (!$this->validate()) { |
||
| 86 | // uncomment the following line if you do not want to return any records when validation fails |
||
| 87 | $query->where('0=1'); |
||
| 88 | return $dataProvider; |
||
| 89 | } |
||
| 90 | |||
| 91 | // grid filtering conditions |
||
| 92 | $query->andFilterWhere([ |
||
| 93 | 'id' => $this->id, |
||
| 94 | 'status' => $this->status, |
||
| 95 | ]); |
||
| 96 | |||
| 97 | $query->andFilterWhere(['like', 'username', $this->username]) |
||
| 98 | ->andFilterWhere(['like', 'email', $this->email]) |
||
| 99 | ->andFilterWhere(['like', 'item_name', $this->userRoleName]) |
||
| 100 | ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null]) |
||
| 101 | ->andFilterWhere(['<=', 'last_visit', $this->date_to ? strtotime($this->date_to . ' 23:59:59') : null]); |
||
| 102 | |||
| 103 | if ($this->pageSize) { |
||
| 104 | $dataProvider->pagination->pageSize = $this->pageSize; |
||
| 105 | } |
||
| 106 | |||
| 107 | $dataProvider->pagination->totalCount = $query->count(); |
||
| 108 | return $dataProvider; |
||
| 109 | } |
||
| 110 | } |
||
| 111 |