| Conditions | 3 |
| Paths | 4 |
| Total Lines | 53 |
| Code Lines | 35 |
| 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 |
||
| 67 | public function search($params, $type, $user = null) |
||
| 68 | { |
||
| 69 | $query = PostModel::find(); |
||
| 70 | $query->innerJoinWith(['postAuthor'])->from(['post' => static::tableName()]); |
||
| 71 | $query->andWhere(['type' => $type]); |
||
| 72 | |||
| 73 | if ($user) { |
||
|
|
|||
| 74 | $query->andWhere(['author' => $user]); |
||
| 75 | } |
||
| 76 | |||
| 77 | $dataProvider = new ActiveDataProvider([ |
||
| 78 | 'query' => $query, |
||
| 79 | ]); |
||
| 80 | |||
| 81 | $dataProvider->setSort([ |
||
| 82 | 'attributes' => ArrayHelper::merge($dataProvider->sort->attributes, [ |
||
| 83 | 'username' => [ |
||
| 84 | 'asc' => ['username' => SORT_ASC], |
||
| 85 | 'desc' => ['username' => SORT_DESC], |
||
| 86 | 'label' => 'Author', |
||
| 87 | 'value' => 'username', |
||
| 88 | ], |
||
| 89 | ]), |
||
| 90 | 'defaultOrder' => ['id' => SORT_DESC], |
||
| 91 | ]); |
||
| 92 | |||
| 93 | $this->load($params); |
||
| 94 | |||
| 95 | if (!$this->validate()) { |
||
| 96 | return $dataProvider; |
||
| 97 | } |
||
| 98 | |||
| 99 | $query->andFilterWhere([ |
||
| 100 | 'post.id' => $this->id, |
||
| 101 | 'author' => $this->author, |
||
| 102 | 'type' => $this->type, |
||
| 103 | 'comment_count' => $this->comment_count, |
||
| 104 | ]); |
||
| 105 | |||
| 106 | $query->andFilterWhere(['like', 'title', $this->title]) |
||
| 107 | ->andFilterWhere(['like', 'excerpt', $this->excerpt]) |
||
| 108 | ->andFilterWhere(['like', 'content', $this->content]) |
||
| 109 | ->andFilterWhere(['like', 'post.status', $this->status]) |
||
| 110 | ->andFilterWhere(['like', 'password', $this->password]) |
||
| 111 | ->andFilterWhere(['like', 'slug', $this->slug]) |
||
| 112 | ->andFilterWhere(['like', 'date', $this->date]) |
||
| 113 | ->andFilterWhere(['like', 'modified', $this->modified]) |
||
| 114 | ->andFilterWhere(['like', 'comment_status', $this->comment_status]) |
||
| 115 | ->andFilterWhere(['like', 'username', $this->username]); |
||
| 116 | |||
| 117 | |||
| 118 | return $dataProvider; |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: