Conditions | 2 |
Paths | 2 |
Total Lines | 54 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
65 | public function search($params) |
||
66 | { |
||
67 | $query = MediaModel::find(); |
||
68 | $query->innerJoinWith([ |
||
69 | 'mediaAuthor' => function ($query) { |
||
70 | /* @var $query \yii\db\ActiveQuery */ |
||
71 | return $query->from(['user' => User::tableName()]); |
||
72 | }, |
||
73 | ])->from(['media' => $this->tableName()]); |
||
74 | $query->leftJoin(['post' => Post::tableName()], 'media.post_id = post.id'); |
||
75 | |||
76 | $dataProvider = new ActiveDataProvider([ |
||
77 | 'query' => $query, |
||
78 | ]); |
||
79 | |||
80 | $dataProvider->setSort([ |
||
81 | 'attributes' => ArrayHelper::merge($dataProvider->sort->attributes, [ |
||
82 | 'username' => [ |
||
83 | 'asc' => ['username' => SORT_ASC], |
||
84 | 'desc' => ['username' => SORT_DESC], |
||
85 | 'label' => 'Author', |
||
86 | 'value' => 'username', |
||
87 | ], |
||
88 | ]), |
||
89 | 'defaultOrder' => ['id' => SORT_DESC], |
||
90 | ]); |
||
91 | |||
92 | $this->load($params); |
||
93 | |||
94 | if (!$this->validate()) { |
||
95 | return $dataProvider; |
||
96 | } |
||
97 | |||
98 | $query->andFilterWhere([ |
||
99 | 'media.id' => $this->id, |
||
100 | 'media.author' => $this->author, |
||
101 | 'post_id' => $this->post_id, |
||
102 | 'media.comment_count' => $this->comment_count, |
||
103 | ]); |
||
104 | |||
105 | $query->andFilterWhere(['like', 'media.title', $this->title]) |
||
106 | ->andFilterWhere(['like', 'media.excerpt', $this->excerpt]) |
||
107 | ->andFilterWhere(['like', 'media.content', $this->content]) |
||
108 | ->andFilterWhere(['like', 'media.password', $this->password]) |
||
109 | ->andFilterWhere(['like', 'media.slug', $this->slug]) |
||
110 | ->andFilterWhere(['like', 'mime_type', $this->mime_type]) |
||
111 | ->andFilterWhere(['like', 'media.comment_status', $this->comment_status]) |
||
112 | ->andFilterWhere(['like', 'media.date', $this->date]) |
||
113 | ->andFilterWhere(['like', 'media.modified', $this->modified]) |
||
114 | ->andFilterWhere(['like', 'post.title', $this->post_title]) |
||
115 | ->andFilterWhere(['like', 'user.username', $this->username]); |
||
116 | |||
117 | return $dataProvider; |
||
118 | } |
||
119 | } |
||
120 |