| Conditions | 3 |
| Paths | 3 |
| Total Lines | 57 |
| Code Lines | 31 |
| 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 |
||
| 47 | public function search($params) |
||
| 48 | { |
||
| 49 | $userId = (int)\Yii::$app->user->id; |
||
| 50 | |||
| 51 | $query = Account::find() |
||
| 52 | ->select([ |
||
| 53 | 'account.*', |
||
| 54 | new Expression('GROUP_CONCAT(tag.name SEPARATOR \', \') as s_tags'), |
||
| 55 | ]) |
||
| 56 | ->leftJoin(AccountTag::tableName(), 'account.id=account_tag.account_id AND account_tag.user_id=' . $userId) |
||
| 57 | ->leftJoin(Tag::tableName(), 'account_tag.tag_id=tag.id') |
||
| 58 | ->groupBy('account.id'); |
||
| 59 | |||
| 60 | // add conditions that should always apply here |
||
| 61 | |||
| 62 | $dataProvider = new ActiveDataProvider([ |
||
| 63 | 'query' => $query, |
||
| 64 | ]); |
||
| 65 | |||
| 66 | $dataProvider->sort->defaultOrder = [ |
||
| 67 | 'is_valid' => SORT_ASC, |
||
| 68 | 'invalidation_type_id' => SORT_DESC, |
||
| 69 | 'invalidation_count' => SORT_DESC, |
||
| 70 | 'id' => SORT_DESC, |
||
| 71 | ]; |
||
| 72 | |||
| 73 | $dataProvider->sort->attributes['username'] = [ |
||
| 74 | 'asc' => ['name' => SORT_ASC, 'username' => SORT_ASC], |
||
| 75 | 'desc' => ['name' => SORT_DESC, 'username' => SORT_DESC], |
||
| 76 | ]; |
||
| 77 | |||
| 78 | $this->load($params); |
||
| 79 | |||
| 80 | if (!$this->validate()) { |
||
| 81 | // uncomment the following line if you do not want to return any records when validation fails |
||
| 82 | // $query->where('0=1'); |
||
| 83 | return $dataProvider; |
||
| 84 | } |
||
| 85 | |||
| 86 | // grid filtering conditions |
||
| 87 | $query->andFilterWhere([ |
||
| 88 | 'monitoring' => $this->monitoring, |
||
| 89 | ]); |
||
| 90 | |||
| 91 | $query->andFilterWhere(['or', |
||
| 92 | ['like', 'username', $this->username], |
||
| 93 | ['like', 'account.name', $this->username], |
||
| 94 | ]); |
||
| 95 | |||
| 96 | if ($this->s_tags) { |
||
| 97 | $manager = \Yii::createObject(AccountManager::class); |
||
| 98 | $accountIds = $manager->findByTags($this->s_tags, $userId); |
||
| 99 | |||
| 100 | $query->andWhere(['account.id' => $accountIds]); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $dataProvider; |
||
| 104 | } |
||
| 106 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: